Commit e336bc0e authored by phs's avatar phs

Added first draft for Elegoo robot worksheets

parent 9f856fe9
Pipeline #514 failed with stages
\documentclass[]{bilingualworkshop}
\usepackage{verbatim}
% latex file for the aberystwyth university magician chassis programming workshop
%
% set engtrue to print the english version
% set cymtrue to print the welsh version
\engtrue %english version
%\cymtrue %cymraeg version
\wsversion{0.1} % version
\aulogo{\includegraphics[width=0.3\textwidth]{logo.png}} % Institution logo at the top right of the memo, comment out this line for no logo
\footerLogoOne{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoTwo{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoThree{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoFour{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoFive{\includegraphics[height=0.08\textwidth]{img/arc.png}}
\title{\en{Aberystwyth Robotics Club - Elegoo Robot\\Driving the Robot}\cy{Teitl Cymraeg}}
\usepackage{listings}
\usepackage{wrapfig}
\usepackage{caption}
\input{arduinoLanguage.tex}
\begin{document}
\maketitle
%\section{Intro to Arduino IDE}
% 1. Opening IDE
% 2. Select Arduino type
% 3. Select Port
% 4. Compile program
% 5. Upload program
% 6. Serial monitor
\section*{%
\cy{Shwmae!}
\en{Introducing Arduino Programming}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{The instructions for our robot are always written inside Functions. Some values are defined above the functions. There are two essential functions for every program \texttt{void setup()} and \texttt{void loop()}. When we create a new program it will look like this:}
\begin{lstlisting}[language=arduino,numbers=none]
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
\end{lstlisting}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{All the instructions in \texttt{void setup()} are run when we first turn on the robot. This is good for any initialisation we need to do. After that, all the instructions in \texttt{void loop()} are run in a continuous loop until we turn off the robot. We can define some of our own functions to help make the code more readable. Text that comes after \texttt{//} is comments for us that will be ignored by the robot. Use these to remind yourself what the code is doing.}
\section*{%
\cy{Shwmae!}
\en{Driving the Robot}} %Driving the robot
\cy{Mae'r testun hwn yn Gymraeg.}
\en{The Elegoo is a 4-wheel robot, but the wheels are controlled in pairs. The motors are connected to numbered pins on the Arduino, so to start with, we need to define which pins are used.}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{For each pair of wheels we have 3 pins. Digital signals \texttt{HIGH} or \texttt{LOW} are sent to the pins to perform actions. Let's look at the left wheels.}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Pin 5 enables the left motors. If this is set to \texttt{LOW} (off) the motors are off. If it is set to \texttt{HIGH} (on) then we can drive the motors. The driving is then done using pins 7 and 8. For the wheels to move, one pin needs to be set to HIGH and the other to LOW. If they are both the same the wheels will not move.}
\begin{enumerate}
\item { \label{defLeft}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{First define the pins above \texttt{void setup()}, and enable the left motors}
\begin{lstlisting}[language=arduino,numbers=none]
#define LeftEnable 5
#define LeftForward 7
#define LeftReverse 8
\end{lstlisting}
}
\item { \label{pinLeft}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Next initialise the pins to expect output inside \texttt{void setup()} }
\begin{lstlisting}[language=arduino,numbers=none]
pinMode(LeftEnable, OUTPUT);
pinMode(LeftForward, OUTPUT);
pinMode(LeftReverse, OUTPUT);
digitalWrite(LeftEnable, HIGH); //Enable left motor
\end{lstlisting}
}
\item { \label{funcLeft}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Below the loop function, we're going to create three functions to turn the left wheels forward, reverse and stop. The first function is given as an example, replace the \texttt{?????}s in the second and third to match the function names.}
\begin{lstlisting}[language=arduino,numbers=none]
void setLeftForward() {
digitalWrite(LeftForward, HIGH);
digitalWrite(LeftReverse, LOW); //Left wheel turning forwards
}
void setLeftReverse() {
digitalWrite(LeftForward, ?????);
digitalWrite(LeftReverse, ?????);//Left wheel turning backwards
}
void setLeftStop() {
digitalWrite(LeftForward, ?????);
digitalWrite(LeftReverse, ?????); //Left wheel stopped
}
\end{lstlisting}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{digitalWrite means we want to send a digital signal to whatever is inside this function. LeftForward is the pin we would like the digitalWrite to act on.}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{HIGH is what we would like the pin to do. Digital signals work on 1s and 0s, 1 being on (HIGH) and 0 being off (LOW). In our case, 1 will be go and 0 will be stop.}
}
\item { \label{leftLoop}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Inside the loop function, we are going to call our new functions to drive forward, stop, drive back and stop. Between each action, we need to tell the program to pause so we can see the changes, otherwise they would happen too fast. The pause duration is defined in milliseconds (ms), and there are 1000 ms in a second.}
\begin{lstlisting}[language=arduino,numbers=none]
setLeftForward();
delay(500); //pause 500ms
setLeftStop();
delay(500);
setLeftReverse();
delay(500);
setLeftStop();
delay(500);
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Upload the program to your robot. Whilst holding the robot off the ground, turn on the battery and check the left wheels move in the correct directions.}
}
\item { \label{defRight}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Given the pins defined below, work with your partner to drive the right wheels, based on steps \ref{pinLeft}--\ref{leftLoop}.}
\begin{lstlisting}[language=arduino,numbers=none]
#define RightEnable 6
#define RightForward 11
#define RightReverse 9
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Currently we are just driving one pair of wheels at a time. If we want to go straight, we need to control both pairs of wheels. Calling the functions like we did in step~\ref{leftLoop}, work together to fill in the functions below.}
\begin{lstlisting}[language=arduino,numbers=none]
void setForward() {
?????
}
void setReverse() {
?????
}
void setStop() {
?????
}
\end{lstlisting}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Hint: setting left forward and right forward without a pause between will enable the robot to drive straight.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Test out these new functions by calling them in the loop function. Can you get your robot to drive in a square? What other shapes can you produce? Alternatively, get your robot to dance.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Discuss with your partner how you could use this to navigate a maze. What sort of problems might you encounter?}
}
\end{enumerate}
\end{document}
\ No newline at end of file
\documentclass[]{bilingualworkshop}
\usepackage{verbatim}
\usepackage[inline]{enumitem}
% latex file for the aberystwyth university magician chassis programming workshop
%
% set engtrue to print the english version
% set cymtrue to print the welsh version
\engtrue %english version
%\cymtrue %cymraeg version
\wsversion{0.1} % version
\aulogo{\includegraphics[width=0.3\textwidth]{logo.png}} % Institution logo at the top right of the memo, comment out this line for no logo
\footerLogoOne{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoTwo{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoThree{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoFour{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoFive{\includegraphics[height=0.08\textwidth]{img/arc.png}}
\title{\en{Aberystwyth Robotics Club - Elegoo Robot\\Line Following}\cy{Teitl Cymraeg}}
\usepackage{listings}
\usepackage{wrapfig}
\usepackage{caption}
\input{arduinoLanguage.tex}
\begin{document}
\maketitle
\section*{%
\cy{Shwmae!}
\en{Introduction}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{In this worksheet we'll learn how to adjust how fast our robot moves and how to change behaviour based on conditions in order to follow a line.}
\section*{%
\cy{Shwmae!}
\en{Changing the speed}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Previously we just used \texttt{digitalWrite()} to change the signals going to pins. Some of the pins on the arduino allow you to send Analog signals for more control. In these cases, instead of sending \texttt{HIGH} or \texttt{LOW}, we can send a number between 255 (HIGH) and 0 (LOW). We can use this on the pins that enable the motors to set how fast the motors turn.}
\begin{enumerate}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Just above the function \texttt{void setup()} we are going to specify the initial speed to use}
\begin{lstlisting}[language=arduino,numbers=none]
int carSpeed=150;
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Inside the function \texttt{void setup()} we are going to change the \texttt{digitalWrite}s to \texttt{analogWrite}s and set the analog value to our speed.}
\begin{lstlisting}[language=arduino,numbers=none]
analogWrite(LeftEnable, carSpeed); //Enable left motor
analogWrite(RightEnable, carSpeed); //Enable right motor
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Run the updated code on your robot, trying out different values for carSpeed between 0-255.}
}
\end{enumerate}
\section*{%
\cy{Shwmae!}
\en{Reading from the line sensor}}
\begin{enumerate}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{As with the motors, we need to tell the Ardunio which pins are connected to the line tracking sensor on the underside of the robot. The line tracking sensor uses 3 pins for left, middle and right. These should be defined just below the motor pin defines at the top of our program:}
\begin{lstlisting}[language=arduino,numbers=none]
#define LineSensorRight 10
#define LineSensorMiddle 4
#define LineSensorLeft 2
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{In the setup function, we initialised the motor pins as output pins. For the line sensors, we want to define these as input pins instead. We also want to set a serial rate for reporting the values read. Add the following to the setup method:}
\begin{lstlisting}[language=arduino,numbers=none]
Serial.begin(9600);
pinMode(LineSensorRight, INPUT);
pinMode(LineSensorMiddle, INPUT);
pinMode(LineSensorLeft, INPUT);
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Below is an example function that will read the value from the left line tracking sensor and print the value in the Serial Monitor. Add this function at the end of your program, and then add two more for the middle and right line sensors.}
\begin{lstlisting}[language=arduino,numbers=none]
bool readLeft() { // define a function which returns a value
bool reading; // temporary variable
Serial.print("Left "); // print a message to the serial monitor so we know which sensor it is
reading=digitalRead(LineSensorLeft); // obtain a reading from the sensor, either 0 ir 1
Serial.println(reading); // print the reading alongside the message above, followed by a line break (println)
return reading; // return the reading for use elsewhere
}
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{In the loop, we now need to call our new methods. You may want to comment out or remove the previous motor commands for this.}
\begin{lstlisting}[language=arduino,numbers=none]
readRight();
readMiddle();
readLeft();
Serial.println(); // add a blank line between sensor readings
delay(500); // pause before taking the next reading
\end{lstlisting}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Upload the program onto the robot and whilst still connected to computer, and the serial monitor open, move the line tracking sensor over the black line below to see the sensor values change. If it is not sensitive enough, you may need to adjust the potentiometer highlighted below.}
}
\begin{figure}[h]
\centering
\includegraphics[width=0.5\linewidth]{img/potentiometer.png}
\hspace{0.1\linewidth}
\includegraphics[width=0.3\linewidth]{img/line.png}
\end{figure}
\end{enumerate}
\newpage
\section*{%
\cy{Shwmae!}
\en{Following a line}
}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{To follow the line, we will need to read the values from the line tracking sensor and make a decision about what action to perform. This can be done with an \textit{if-then-else} statement. The typical structure of this is as follows:}
\begin{lstlisting}[language=arduino,numbers=none]
if(CONDITION) {
//THEN perform actions here
}
else if(CONDITION 2) {
//THEN perform actions here
}
else {
//If none of the above conditions are true, do this
}
\end{lstlisting}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Where the condition should evaluate to true (1) or false (0). In our case, the values from the line tracking sensors can be used directly as conditions.}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{With your partner, discuss a strategy for following a line using the line tracking sensor and the if-then-else statements. Some hints are given below, but try out different configurations first.}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Have a race to see you can navigate the line the fastest. Penalties should be added for leaving the line.}
\begin{enumerate}[label=Hint~\arabic*]
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Each time we go through the loop, we want to check the sensor readings and change direction if necessary.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Adjusting the speed of the robot may make it more accurate.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{The order in which the checks are done can make a big difference in the if-then-else statement.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{If a side sensor is returning \texttt{true}, we probably want to turn in that direction.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Changing direction too fast could result in the robot not moving at all. You may need to add a short pause in the loop after setting the direction.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{When setting the direction as left or right, the settings for the other side are not changed. You may wish to completely stop the robot at the start of each loop, or stop the other side when changing direction.}
}
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{An example structure might look like:}
\begin{lstlisting}[language=arduino,numbers=none]
setStop();
if(readLeft()){
????
}
else if(readRight()){
????
}
else if(readMiddle()){
????
}
delay(20);
\end{lstlisting}
}
\end{enumerate}
\end{document}
\ No newline at end of file
\documentclass[]{bilingualworkshop}
\usepackage{verbatim}
\usepackage[inline]{enumitem}
\usepackage{gensymb}
% latex file for the aberystwyth university magician chassis programming workshop
%
% set engtrue to print the english version
% set cymtrue to print the welsh version
\engtrue %english version
%\cymtrue %cymraeg version
\wsversion{0.1} % version
\aulogo{\includegraphics[width=0.3\textwidth]{logo.png}} % Institution logo at the top right of the memo, comment out this line for no logo
\footerLogoOne{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoTwo{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoThree{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoFour{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoFive{\includegraphics[height=0.08\textwidth]{img/arc.png}}
\title{\en{Aberystwyth Robotics Club - Elegoo Robot\\Maze Navigation}\cy{Teitl Cymraeg}}
\usepackage{listings}
\usepackage{wrapfig}
\usepackage{caption}
\input{arduinoLanguage.tex}
\begin{document}
\maketitle
\section*{%
\cy{Shwmae!}
\en{Introduction}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{This worksheet looks at how our obstacle avoidance can be applied to maze navigation and various adaptations that may need to be made.}
\section*{%
\cy{Shwmae!}
\en{Obstacle avoidance in a maze}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{What would happen if our robot tried to navigate this maze with our obstacle avoidance?\\ S marks the start, and E marks the end.}
\begin{figure}[h]
\centering
\includegraphics[width=0.6\linewidth]{img/maze1.png}
\end{figure}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Discuss how you might make use of the ultrasonic sensor to navigate the maze. Some ideas and points to consider are given below.}
\newpage
\begin{enumerate}
\item You could follow the wall on your left. Set the servo to 0 or 180, and try to maintain a constant distance.
\begin{itemize}
\item What path would the robot take around the maze?
\item What would happen if the maze had a loop in it?
\begin{figure}[h]
\centering
\includegraphics[width=0.6\linewidth]{img/maze2.png}
\end{figure}
\end{itemize}
\newpage
\item Consider the maze as individual cells, move one cell at a time and decide which direction to move. Use the scenarios below to help think about the different conditions you might encounter.
\begin{figure}[h]
\centering
\includegraphics[width=0.305\linewidth]{img/maze3.png}
\includegraphics[width=0.3\linewidth]{img/maze4.png}
\includegraphics[width=0.3\linewidth]{img/maze5.png}
\end{figure}
\begin{enumerate}
\item Measure how long it takes to move forward one cell
\item Measure how long it takes to turn the robot 90\degree on the spot
\item In each cell, use the servo to turn the ultrasonic sensor to detect walls and gaps
\item Use the if-then-else statements to change the behaviour based on what is detected
\end{enumerate}
\end{enumerate}
\section*{%
\cy{Shwmae!}
\en{Challenges}}
\begin{enumerate}
\item Detect when you drive over a black line to mark the end of the maze and perform a celebratory dance at the finish line.
\item Advanced: Can your robot learn its way through the maze?
You may find a data structure called an array useful here. This is a single variable that allows you to store a set of values, that can be referenced by an index number. You specify the length (L) of the array when you create it, then use indexes 0 to L-1 to access them:
\begin{lstlisting}[language=arduino,numbers=none]
int myArray[6] = {}; //create an array of length 6 (indexes 0-5)
myArray[0] = 3; //set the value of the first index to 3
myArray[5] = 12; //set the value of the last index to 5
for(int i=0; i<6; i++)
{
Serial.println(myArray[i]); //cycle through the array printing out all the values
}
\end{lstlisting}
You could give a number to each type of movement or you could try to encode each cell in the maze, where position (x,y) in the maze (starting from (0,0)) is index x*L+y in the array.
\end{enumerate}
\end{document}
\ No newline at end of file
This diff is collapsed.
\documentclass[]{bilingualworkshop}
\usepackage{verbatim}
\usepackage[inline]{enumitem}
\usepackage{gensymb}
% latex file for the aberystwyth university magician chassis programming workshop
%
% set engtrue to print the english version
% set cymtrue to print the welsh version
\engtrue %english version
%\cymtrue %cymraeg version
\wsversion{0.1} % version
\aulogo{\includegraphics[width=0.3\textwidth]{logo.png}} % Institution logo at the top right of the memo, comment out this line for no logo
\footerLogoOne{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoTwo{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoThree{\includegraphics[height=0.08\textwidth]{img/arc.png}}
%\footerLogoFour{\includegraphics[height=0.08\textwidth]{img/playful-coding.png}}
%\footerLogoFive{\includegraphics[height=0.08\textwidth]{img/arc.png}}
\title{\en{Aberystwyth Robotics Club - Elegoo Robot\\Infrared Remote Control}\cy{Teitl Cymraeg}}
\usepackage{listings}
\usepackage{wrapfig}
\usepackage{caption}
\input{arduinoLanguage.tex}
\begin{document}
\maketitle
\section*{%
\cy{Shwmae!}
\en{Introduction}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{This worksheet will enable us to control our robot using the infrared remote, changing the behaviour dynamically.}
\section*{%
\cy{Shwmae!}
\en{Infrared communication}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Before we start, we need to add a library of code to communicate with the infrared.}
\begin{enumerate}
\item Click on menu "Sketch"
\item Select "Include library"
\item Add .ZIP Library
\item Locate and add the provided "IRremote.zip" file.
\end{enumerate}
\begin{figure}[h]
\centering
\includegraphics[width=0.55\linewidth]{img/IRsensor.png}
\includegraphics[width=0.4\linewidth]{img/IRremote.png}
\end{figure}
\subsection*{%
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Test program}}
\cy{Mae'r testun hwn yn Gymraeg.}
\en{To test that the library is loaded correctly and to help write our program, an example is provided with the library.}
\begin{enumerate}
\item Click on menu "File"
\item Select "Examples"
\item Scroll down to "Examples from custom libraries" and "IRremote"
\item From the list of examples, select "IRrecvDemo"
\item Change the line "int RECV\_PIN = 11;" to "int RECV\_PIN = 12;" as our IR receiver is on a different pin.
\item Change the line "Serial.println(results.value, HEX);" to just "Serial.println(results.value);" removing the HEX parameter.
\end{enumerate}
You should now be able to upload the program to your robot, and in the serial monitor see various numbers printed when pressing different buttons.
When a button is pressed on the remote, it sends a rapid burst of signals that are translated into a number to uniquely identify each button. Due to how this process works, there are two possible numbers for each button. For our program, we will want to make use of the direction arrows, the OK button and a couple of numbers. Draw up a table as follows:
\vspace{0.5cm}
\begin{tabular}{|c|c|c|}
\hline
Button & A & B\\ \hline
up arrow & 16736925 & 5316027 \\ \hline
... & & \\ \hline
... & & \\ \hline
\end{tabular}
\section*{%
\cy{Shwmae!}
\en{Steering the robot}}
We can use the remote control to steer our robot. Below are the additions we need to make to our program:
\begin{enumerate}
\item At the top, add a line to include the IRremote library for program:
\begin{lstlisting}[language=arduino,numbers=none]
#include <IRremote.h>
\end{lstlisting}
\item Define the pin and some useful variables for interacting with the IR receiver:
\begin{lstlisting}[language=arduino,numbers=none]
#define RECV_PIN 12 // Infrared signal receiving pin
IRrecv irrecv(RECV_PIN); // Specify the pin the IR receiver is connected to
decode_results results; // A special variable provided by the library for interpretting the signals
unsigned long val; // used to record the decoded value received
unsigned long preMillis; // we will use this for a timer below
\end{lstlisting}
\item Define the set of codes recorded in the table above e.g.:
\begin{lstlisting}[language=arduino,numbers=none]
#define Fa 16736925 // FORWARD
#define Fb 5316027 // FORWARD
...
\end{lstlisting}
\item In the setup function, add the following line to start the receiver:
\begin{lstlisting}[language=arduino,numbers=none]
irrecv.enableIRIn(); // Start the receiver
\end{lstlisting}
\item In the loop function, if a signal is received we want to do something with it, otherwise we will stop the robot.
\begin{lstlisting}[language=arduino,numbers=none]
if (irrecv.decode(&results)){
preMillis = millis(); //Record time at which signal was received
val = results.value; //The value indicating which button was pressed
Serial.println(val);
irrecv.resume();
...
}
else{ //Make sure the robot doesn't run away
if(millis() - preMillis > 500){
setStop(); //
preMillis = millis();
}
}
\end{lstlisting}
To make sure the robot doesn't run away, we will tell it to stop half a second (500 milliseconds) after receiving the last signal.
\item To process the values and decide on an action we are going to use a new type of control structure called a Switch-statement. A switch statement takes a value and compares it against a series of cases. This is much like an if-statement, but is useful when there are pre-defined values as we have here. The outline structure is:
\begin{lstlisting}[language=arduino,numbers=none]
switch(val){ //input from remote
case Fa:
case Fb: setForward(); break; // without a break, it will continue to execute code for next case too
case Ba:
case Bb: setReverse(); break;
?????
default: break; // end case for any other undefined input value
}
\end{lstlisting}
Fill in cases for the other buttons to drive the robot around with the remote. Remember the "break;" between cases for different buttons. Note that the button names cannot start with a numerical character, so for the number buttons you will need to write OneA... instead.
\end{enumerate}
\section*{%
\cy{Shwmae!}
\en{Changing modes}}
In the switch statement used above, we are calling functions to drive the robot. By putting the code for line following and obstacle avoidance into functions too, we can use those behaviours too.
\textbf{Challenge:} When line following or navigating a maze, we don't want to have to keep pressing the button every 500ms. Instead we want it to keep moving until instructed otherwise. However, if steering the robot we don't want it to run away. Consider how a function could be used to separate the steering.
\begin{enumerate}[label=Hint~\arabic*]
\item {
\cy{Mae'r testun hwn yn Gymraeg.}
\en{Use the number buttons to set 'modes' of operation}
}
\item Consider modes for line following, obstacle avoidance, maze navigation and steering
\item In the steering function use a loop to keep getting new readings until the 'stop' command is given
\item A 'while-loop' is like a 'for-loop' but just checks for a condition, e.g.:
\begin{lstlisting}[language=arduino,numbers=none]
while(!(val==stopA || val==stopB)){
...
}
\end{lstlisting}
Where the ! means 'Not', == means 'equal to', and the \verb'||' means 'OR', so can be read "While not value equals stop A or value equals stop B do...".
\end{enumerate}
\end{document}
\ No newline at end of file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% ~ Arduino Language - Arduino IDE Colors ~ %%%
%%% %%%
%%% Kyle Rocha-Brownell | 10/2/2017 | No Licence %%%
%%% -------------------------------------------------------------------------- %%%
%%% %%%
%%% Place this file in your working directory (next to the latex file you're %%%
%%% working on). To add it to your project, place: %%%
%%% \input{arduinoLanguage.tex} %%%
%%% somewhere before \begin{document} in your latex file. %%%