Lesson#5: Intro Programming
Ok after the success of the last lesson we can now move onto something a bit more challenging. In this lesson we will learn more about the serial display uses. With this example we can see how we can create a simple math machine. Copy and paste the following code into your Arduino and upload away.
/* * Math is fun! */ int a = 5; int b = 10; int c = 20; void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Here is some math: "); Serial.print("a = "); Serial.println(a); Serial.print("b = "); Serial.println(b); Serial.print("c = "); Serial.println(c); Serial.print("a + b = "); // add Serial.println(a + b); Serial.print("a * c = "); // multiply Serial.println(a * c); Serial.print("c / b = "); // divide Serial.println(c / b); Serial.print("b - c = "); // subtract Serial.println(b - c); } void loop() // we need this to be here even though its empty { }
Check out the above code. Did you notice anything new? We have covered the "println" procedure but not the "print". The print procedure is just like println except it does not print out a "carriage return" at the end, starting a new line.
Ok so now lets dissect this code a little bit now.
Serial.println(a);If you use a quoted line of text as input to println procedure, it will display that text. In this case you can see that if you use a variable to println it will look up what that variable contains and print that out!
Serial.println(a + b);
Arduino looks at what the input to println is, and finds its actually a calculation. It looks up what a is (5) and what b is (10) and then adds them together (+) and then uses that as the value to send to println
No comments:
Post a Comment