Arduino Training For You! Learn Programming at Your Own Pace! A course developed from the text book: “Introduction to Arduino A Piece of Cake!” By Alan G. Smith Your Instructor is: Richard T. Vannoy II RoboticsProfessor@gmail.com Chapter 1 www.TeachElectronics.com Copyright 2016, Richard T. Vannoy II Chapter 2: Making Light Patterns Learning Objectives • At the completion of this lesson, the student will be able to: – State the use of a constant. – Explain why setup() and loop() are required functions in all Arduino sketches (programs). – Explain the use of the Arduino functions/commands: pinMode( ), digitalWrite( ) and delay( ). – Demonstrate how to use the two types of comments used in Arduino programming. Text/Author Information • Text Book used in this course: – Title: Introduction to Arduino: A piece of Cake! – Author: Alan G. Smith – ISBN: 1463698348 – ISBN-13: 978-1463698348 – Email: alan@introtoarduino.com – Purchase text: www.amazon.com – Download the free PDF: www.introtoarduino.com Instructor Information • This PowerPoint presentation – Instructor: Richard T. Vannoy II, M.S.I.T., B.S.E.E.T. – Email: RoboticsProfessor@gmail.com – Web Site: www.TeachElectronics.com Instructor’s Note: Great PowerPoint presentations are supposed to have few words and lots of white space. A typical presentation is meant to augment the presenter’s dialog, not contain all of the presentation material. I violate this rule here by maximizing the amount of information on each slide. This allows any student who cannot attend my presentation to view the slide show, and learn as much as he could with me presenting. Chapter 2: Making Light Patterns Learning Objectives • At the completion of this lesson, the student will be able to: – Explain how “if” is used to ask True/False questions. – Explain how =, +, -, *, /, % are used. – List the six Comparison Operators. – Explain how to use the Comparison Operators to ask True/False questions. Much of the code here is the same as the Blink program from Chapter 1. Lines 1 through 7 are the same. They set up Pin 13 as the output Pin for our LED. This line says: “Give me a storage place in memory where I can store an integer. I want to name that place delayTime, and I want to place the number 1000 in that location” We do NOT make delayTime a constant, because we will be needing to change its value in the program. The program starts out by subtracting 100 from delayTime. More specifically, this command says: 1. “Go get the present value in the memory location called “delayTime” (1000). 2. Subtract 100 from that value. 3. Return the new value (900) back to that memory location. And since all code inside loop() continues to repeat over-and-over, we will subtract 100 each time we get here. In the next few slides, we will look at how computers ask True/False questions. True/False Questions Frequently, computers need to know which of two or more actions they should take. They do this by asking one or more True/False questions to decide which action to take. Let’s take an example and walk through it. True/False Questions A True/False question starts with the keyword “if” tells the computer that a True/False question comes next. if (age >= 18) Print “Adult” else Print “Child” True/False Questions After the “if” the computer looks for a set of parentheses where it can find a comparison operator that sets up a condition that can be either True or False. if (age >= 18) Print “Adult” else Print “Child” These are the Comparison Operators used by the Arduino and many other programming languages. The idea is to put something on either side of one of these operators in such a way that a comparison can be made. True/False Questions In this example, we are comparing age with 18. The computer “reads” age >= 18 like this: 1. Go get the value in the memory location called age. 2. Compare that with the number 18 and ask this question: Is age greater than or equal to 18? 3. If the answer is True, the next statement will be executed and “Adult” will be printed. 4. If the answer is False, the next statement will be skipped and the statement after the “else” will be executed and “Child” will be printed. if (age >= 18) Print “Adult” else Print “Child” Several Examples If (myScore > yourScore) print “I win!”; If (myScore < yourScore) print “You win!”; If (myScore == yourScore) print “A Tie!”; So, this True/False question asks: “Is delayTime less than or equal to zero?” So, this True/False question asks: “Is delayTime less than or equal to zero?” Since line 8 set it to 1000, and line 12 reduced it to 900, the answer to the question is no, or “False.” Since the answer is “False”, line 14 is not executed, and the program gets to line 16 with 900 in delayTime. So… What does this program do? Sample Math Operations: int X = X = X = X = X; 14 31 18 18 + – / % 7; 4; 4; 4; // // // // Assigns Assigns Assigns Assigns (Puts) (Puts) (Puts) (Puts) 21 into X. 27 into X. 4 into X. 2 into X. 4 Sample Math Operations: 4 √18 int X; X = 14 + 7; // Assigns (Puts) 21 into X. 16 X = 31 – 4; // Assigns (Puts) 27 into X. X = 18 / 4; // Assigns (Puts) 4 into X. 2 X = 18 % 4; // Assigns (Puts) 2 into X. 4 Sample Math Operations: 4 √18 int X; X = 14 + 7; // Assigns (Puts) 21 into X. 16 X = 31 – 4; // Assigns (Puts) 27 into X. X = 18 / 4; // Assigns (Puts) 4 into X. 2 X = 18 % 4; // Assigns (Puts) 2 into X. Division (/) stores the Quotient. Modulo (%)Operations: stores the Remainder. Sample Math 4 4 √18 int X; X = 14 + 7; // Assigns (Puts) 21 into X. 16 X = 31 – 4; // Assigns (Puts) 27 into X. X = 18 / 4; // Assigns (Puts) 4 into X. 2 X = 18 % 4; // Assigns (Puts) 2 into X. …same program we just went through, with a different True/False question format below. Here the program resets the delayTime to 1000 or subtracts 100 from it depending on the answer to theTrue/False question. The “while” statement also looks for a True/False statement in the parentheses. Only this parentheses only has a “1” in it. How does that work? When the compiler sees: while (1) There is no Comparison Operator, but the machine knows that FALSE IS DEFINED AS 0. Since 1 (or ANY number other than zero) is not zero, the computer evaluates this as True. Assignment vs. Comparison x = 3; sum = 14 + 16 + 3; total = a + b + c; if (age == 18) if (3 == 4) if (myAge == yourAge) ledState = !ledState; Is equivalent to: if (ledState == HIGH) ledState = LOW; else ledState = HIGH; The “for” construct is a way we can do one or more actions many times. It does a number of things… 1. “for” says: We are going to repeat one or more commands a number of times. 2. “int i” says: Give me a memory space to store an integer called “i”. I want to use it as a counter. 3. “= 0” says: …and set my counter to zero. 4. “i < 4” says: Keep repeating the actions below as long as the value of “i” is less than 4. 5. “i++” says: Add 1 to the memory location “i”. So, the code in the box will be executed 4 times. …or… “Turn the LED on for one-fifth of a second, then off for one-fifth of a second, 4 times. Then go on to the next command.” The “for loop” below will print the numbers 0 through 10. for (x = 0; x <= 10; x++) Serial.print(x); There are two ways to add 1 or subtract 1 from a number: number = number + 1; number++; counter = counter - 1; Counter--; Let’s use these 4 LEDs to create a pattern of lights. I’m hooking up 4 LEDs to Arduino Pins 2 through 5. I’m hooking up 4 LEDs to Arduino Pins 2 through 5. I’m telling Arduino to set up Pins 2 through 5 for OUTPUT so I can control the state of the 4 LEDs. Turn on the 4 LEDs at onetenth second intervals. …Then, turn them off. This program does the same thing as the previous one, only it uses different, more efficient methods to achieve the same result. kPinLeds[ ] is an array. An array lets us store as many numbers of the same data type as we wish. Here it is used to store the numbers of the 4 Pins we will use to control 4 LEDs. Each item in the array has an index number, starting at 0, so we can refer to each element by its index number. Instead of creating four different statements to set the Pins up as OUTPUT Pins, we use a loop to repeat the command 4 times. The End You Can Learn Arduino Programming! Your Instructor is: Richard T. Vannoy II RoboticsProfessor@gmail.com www.TeachElectronics.com