Arduino Coding Computer Science Task
Description
Predict the outcomes of programs in Arduino involving conditional and looping statements
Draw an activity diagram to accomplish a given task
- Write a program (in Arduino) corresponding to a given activity diagram
- Here are some Arduino coding questions, along with their answers, to help you practice more: Extra Arduino Coding help.pdf
- Actions
Submission format
Centered at the top write the name of this activity. Centered in a line below it write your name. Below these, the rest of the activity starts, aligned to the left.
Be very organized, neat, and professional. You should use consistent spacing, formatting, font, and style, as well as correct grammar and spelling.
Please type the answers to various parts as instructed below. Neatly organize all your answers in a numbered list (with sub lists) corresponding to various parts (and subparts).
You want the grader to find all your work in a convenient way lest you may lose points.
In the end, you will submit everything as a single, typed, PDF file.
Instructions
Complete the following parts
Part 1: Predict the result of an Arduino program
What will the following Arduino code print on the serial monitor? Try not to paste this code into Arduino, infer it by yourself and type the answer.
int x = 1;
void setup() {
Serial.begin(9600);
}
void loop() {
if (x < 10) {
Serial.println(x);
x = x+1; }
else {
Serial.println(x);
}
}
Part 2: Predict the result of an Arduino program
What will the following Arduino code make the motor do? Explain in less than 3 sentences, no “essays” please.
You may want to revisit the Arduino coding from previous week’s activity and videos for this question. In this setup, a motor is connected to the output pins of an L298n or L293d board, and the input pins of this board are connected to an Arduino’s pin 11 and pin 10. Thus, these two pins control the motor. Assume the board is already enabled. A distance sensor (HCSR04) is also connected to Arduino. Its trigger is connected to pin 2, and echo is connected to pin 3.
Here is the code:
int motorPin1 = 11;
int motorPin2 = 10;
int trigger_pin = 2;
int echo_pin = 3;
long pulse_duration, distance_in_inches;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(trigger_pin, OUTPUT);
pinMode(echo_pin, INPUT);
Serial.begin(9600);
}
void loop() {
// read the distance
digitalWrite(trigger_pin, LOW);
delayMicroseconds(2);
digitalWrite(trigger_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trigger_pin, LOW);
pulse_duration = pulseIn(echo_pin, HIGH);
distance_in_inches = pulse_duration / 147.82;
Serial.println(distance_in_inches);
delay(10);
// do stuff
if (distance_in_inches > 10) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
} else {
if (distance_in_inches < 2) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
} else {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
}
}
Part 3: Arduino program from a given flowchart
Here is a flow chart/activity diagram of some algorithm. Please write a complete Arduino code which will accomplish the same.
Hint: you can always make the circuit in TinkerCAD and test your code to see if it works!
Part 4: Flowchart for a given algorithm
There is a straight road many miles long. The road is black in color, but it has a white line/strip running in its middle. A car (shown as a red rectangle) is supposed to travel on the white line. The car is driven by a motor system, and there are two color sensors in front of the car. The color sensors are marked by two black dots on the red car as shown in the figure below, and each color sensor can see the color of the road directly below where they are mounted.
Draw a flowchart/activity diagram (not the Arduino code) which can eventually be run on some Arduino like system, which will drive the car forward on the white line/strip.
Initially the car starts in the position as shown above. The two sensors are on the white line, and car is moving forward (i.e. no steering). The car would have moved forward perfectly, but it can veer off due to random effects beyond your control. If the car veers off the white line, then the flowchart algorithm should direct it to steer back onto the white line again.
In the boxes of the flowchart, you are only allowed to use the following phrases/words or their combinations. No regular English sentences are allowed in this flowchart. The allowed phrases/words are:
start
turn left
turn right
move forward (no steer)
is the left sensor seeing white?
is the left sensor seeing black?
is the right sensor seeing white?
is the right sensor seeing black?
yes
and
no
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."