Learning How to Code in Arduino
Transcription
Learning How to Code in Arduino
Learning How to Code in Arduino Worksheet 2: Serial.println(), State Variables and Counters Jason Krugman – Physical Computing - Fall 2012 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// How to Use the Serial.print() command Serial.print() allows us to debug our programs, aka, figure out what is going on To use Serial.print(), we need to include the following piece of code in the setup: This line of code tells the Arduino to start sending data back to the computer through the USB port. The 9600 part is the BAUD rate – the number of pulses per second that the Arduino should send out to the computer. For our purposes this will always be 9600. If we set up a button circuit like the one from ITP’s Digital In/ Digital Out Lab (shown below), we can start experimenting with Serial.print(); and Serial.println(); (when you add the “ln” at the end of Serial.print it skips a line when printing out) http://itp.nyu.edu/physcomp/Labs/DigitalInOut%20%20 For the resistor, it does not have to be exactly the one shown. Anything greater than 1K ohm is fine…. basically any resistor that has an orange or greater stripe next to the gold. Remember ROYGBIV? Resistor values increase by a power of 10 as the stripe next to the gold moves to the right in ROYGBIV or R < O < Y < G < B < I < V < is the simple for greater than less than. It’s a hungry mouth and wants to eat whichever number is bigger. Black is smallest, then brown, then R, then O, and so an so forth. If we set up the circuit above and upload the code below to our Arduino, it will print out a value for the buttonVal variable every time When we upload the program to the Arduino, run it, and then open the Serial monitor (click on the little magnifying glass in the upper right hand corner of the Arduino window), we would see something like this in the black display in the bottom of the Arduino window. 0 0 0 0 0 0 0 0 0 And when we press the button down that we have set up on the breadboard, we would see: 1 1 1 1 1 1 1 1 The Arduino will keep printing out the buttonVal variable every 10 milliseconds. We put the delay in there to slow things down a little so that we can read it. We could also do something like this: This would print out: buttonVal is 0 buttonVal is 0 buttonVal is 0 buttonVal is 0 buttonVal is 0 buttonVal is 0 buttonVal is 0 buttonVal is 0 or buttonVal is 1 Notice the use of quotes and also that I used both Serial.print() and Serial.println(). What is a state variable? A state variable is used to remember what state something is in, or was in, at a certain point in time. State variables are great for creating a series of conditions that have to be satisfied in order for another event to take place. For example, to make a simple combination lock, we could include two buttons. If we wanted the lock to open when both buttons are pushed at once, we could write something like: Do you see the mistake that I made when I wrote up this code? Its very small but it will cause an error when the code runs…. We can also write the if statements another way using the AND operator which we write as && This means that both things inside the if statement condition have to be true in order for the event to take place (the event being that we print out the secret code). Another very useful way to write if statement conditions is with the OR operator which we write || Those symbols are pipes, not capital i’s. They look the same but they’re not. Its on the same key as the \ If we were to write, then if either buttonOne OR buttonTwo are 1, the secret code would be printed. Remember Venn Diagrams (those overlapping circles with mutual or exclusive areas)? It’s the same thing. Okay, here is where state variables come in What if both buttons must be pressed but not at the same time? To do this we would need two variables to keep track of if each button has been pressed up to the current point in time. The below code does this with two additional variables, one for each button. What is a counter? A counter is a variable that we use to…. count. We do this by adding or subtracting to it every time we do something. We can also use division and multiplication with counters, but for now we will stick with addition. If we want program 10 LED blinks every so often, we could write a program that looks like this: Lets talk a bit about what this program is doing. First of all, what is counter ++? This notation means that counter will add 1 to its current value every time it encounters this command. -- works the same way ++ does except it takes away 1. You can also do something similar to this with += or -=. For example, counter += 5; will add 5 to the value of counter each time we run that line. Its the same as writing this: counter = counter + 5; Just a shortcut. We can also do the same with / or * (which is the symbol for multiply). What happens if we do counter**? I don't know, haven't tried it. We could use the Serial.print() command to see what the value of that would be. To review, these are all the same thing: counter++; counter = counter + 1; counter +=1;