Arduino Tutorial Common Syntax Serial Monitor Output Serial.Begin (9600); : Connects the Program to the Serial Monitor Serial.Print: Prints Text in the Dialogue Box
Total Page:16
File Type:pdf, Size:1020Kb
Arduino Tutorial Common Syntax Serial Monitor Output Serial.begin (9600); : Connects the program to the serial monitor Serial.print: Prints text in the dialogue box. Used for output readings from the Arduino board. Serial.println(): Drops cursor down to the next line on the serial monitor Serial.parseint() or Serial.parsefloat(): Allows user to read either integer or real numbers for input Serial.flush (); : Waits for transmission of outgoing data to be complete Serial.available (); : Waits until the input buffer has a character Digital/Analog Commands digitalWrite or analogWrite: Write code to the Arduino board (I.E digitalWrite (13, HIGH) = Turns LED light linked to Pin 13 On) digitalRead or analogRead: Reads information from the Arduino Board. Typically used with a switch or sensor to determine if contact is being made (I.E digitalRead (5, Low) = Switch/Sensor linked to Pin 5 on the Arduino board is not making contact) Serial.read: Reads and Output information from a specified pin on the Arduino Board or keyboard (NOTE Keyboard input will always read as character variable (even numbers) Power Transfer/Wait Time HIGH: Power transferred or switch will read in contact (On) LOW: Power is not transferred or switch will read not in contact (Off) delay(): Will pause the program for x number of milliseconds (NOTE: 1000 milliseconds = 1 second) Pin Declaration pinMode (): Declares the numbered pin to be either input or output (I.E pinMode (13, INPUT); or pinMode (13, OUTPUT) NOTE: All Loops (for, while, do/while), if/then, if/else syntax is the same as learned in previous lessons Basic Anatomy of the Arduino Board Breadboard Setup Reading a Resistor Workspace Setup NOTE: It is not necessary to declare the #include <iostream> library or to int main() in your program. Those codes are prebuilt into the structure of the Arduino program. NOTE: Change Font Height of Text: Pull Down Menu File > Preferences > Font > Change to desired text height Upload: Sends code to Arduino Software provides 2 default functions: void setup(){}: only runs the code between braces one time Verify: Compiles code and outputs any errors void loop(){}: runs code between the braces infinitely. There is no way to end it. However program can be designed in a way to trap the program in an infinite loop that will not execute any commands within the loop Dialogue Box: Verify Program or show errors Connecting Arduino Uno to the Computer NOTE: These steps should been done each time Arduino Software is used. 1. Using the USB-Fire Wire connector, connect the Arduino Board to the computer 2. Open Arduino Software from the desktop 3. Setting up Arudino Board Set the following a Tools > Board > Select Arduino Uno b Tools > Port > Select the Com# Port that has (Arduino Uno) in parentheses (NOTE: if Arduino Uno does not show up; look at the list of ports > unplug the wire see which port disappears (remember the number) > replug the wire in and choose the port that had disappeared from the list in the previous step) c Tools > Programmer > USBasp This will set the communication between the PC and the Arduino Program 1: Flashing Light and Hello World! Part 1: Turn on a light for a specified duration Yellow W ire = Ground Wire White Wire = Power Wire connects into Port 13 to Negative column on breadboard NOTE: Connection ports DO NOT need to be the same as long as the wires and electrical components match up properly. NOTE: LED Anode (Long Leg) should always be linked to the resistor and Cathode (short Side) linked to the ground or the direction the circuit is going. Long Leg = Electricity In, + (Positive) Short Leg = Electricity Out, - (Negative) Program Code Type following code Save Program as Blink Connect the Arduino Board to the computer using the USB-Fire Wire Cable. NOTE: If board is not connected through a COM port unplug the board and reconnect with the steps listed above (Page 4 Connecting Arduino Board to Computer) Result: Light should turn on for 1 second and then turn off Part 2: Make the light blink repeatedly 1. Copy the code from the void setup() and paste in the void loop() 2. Add a delay statement after the digitalWrite (13, LOW) of 200. 3. This will make the light flash on and off repeatedly. Experiment in changing the delays to alter the flashing of the light. Part 3: Adding Input/Output , Variables, If/Statement, and Loops 1. Delete the Blink Code; Type the following Code 2. Modify the code to do the following a. Change the code to calculate the volume of a cone b. Flash the lights as follows i. Volume < 100 - Flash ON = 2 sec - OFF = 2sec - 3 Times (USE For Loop or Do/While Loop) ii. 100 <= Volume <= 500 - Flash ON = 1 sec - OFF = 3 sec - 3 Times (USE For Loop or Do/While Loop) iii. Volume > 500 - Flash ON = .5 sec - OFF = 2sec - 3 Times (USE For Loop or Do/While Loop) Part 4: Limiting the times through the loop NOTE: There is no way to exit the Void Loop() in Arduino. However a delay statement or moving into a loop that does nothing can solve this problem. (I.E delay (100000000); or Create a Loop: while (TRUE) {} (while TRUE will always make the loop true so it will run infinitely.) Modify the program to do the following 1. After the first time through the program ask the user of they want to find the volume of another cone. Use 0 or Yes and 1 for No a. If Yes repeat the program b. If No end the program and say “Goodbye!” Program 2: Switch Statement Circuit Design NOTE: LED Anode (Long Leg) should always be linked to the resistor and Cathode (short Side) linked to the ground or the direction the circuit is going. Long Leg = Electricty In, + (Positive) Short Leg = Electricity Out, - (Negative) Program Code Switch statement is like an If/Else/If/Else Statement. It only allows the user to compare discrete values (one thing at a time). It is not possible to compare ranges of objects. (For that an If statement is needed). Program uses the Serial Monitor. Type in the characters a, b, c, d, e will turn on the LEDS they are connected to. Using a Switch/Case statement is the equivalent of writing multiple If/Else Statements For Example: Switch (inByte) { case ‘a’: } is equivalent to writing if (inByte == ‘a’) {then do this line(s) of code} Upload the program to test it. Notice how when one key is pressed then another the light of the previous key does not turn off. Assignment: LED Lights Modify the program to do the following 1. Only have the inputted LED Lamp Turned ON (All other LED’s turn off with each new input) I.E When ‘a’ is pressed ‘a’ LED light is on, then when ‘b’ is pressed the ‘b’ LED light is on and the ‘a’ LED is turned off. 2. Output if Input is NOT a, b, c, d, or e a. Blink all of the LED Lamps for a duration of 2 seconds b. Output in the Serial Monitor “ERROR, INPUT INVALID!” (HINT: if inbyte < 97 || inbyte >101) NOTE: Serial.read converts all input to ASCCII code form. See below of values Wiring Diagram Option 1 Program 3: Push Button Direct Wiring to Arduino Source Code Defines each pin VCC = Volt GND = Ground Out = Digital Signal Port Grey Wire: Out goes to Port 13 Brown Wire: GND goes to GND Red Wire: VCC to 5V Wiring Diagram Option 2 Direct Wiring to Arduino Brown Wire: Ground (GND) to Arduino Ground (GND) Align each wire in the same row as the pin Red Wire: Voltage (VCC) to Arduino 5V connection Grey Wire: Out to Arduino Port 13 Top Shows what each pin represents FRONT Back Left Side Assignment: Push Button 1. Modify the program so the serial monitor screen shows a. Text On (Button Pressed) and Off (Button Released) b. Counter showing how many times the button has been released 2. Limit of On/Off a. Maximum presses = 5 b. Once Limit is met do the following i. Stop the light from turning on and off when button is pressed ii. Serial Port Print “ALL DONE!” iii. Only print “ALL DONE!” twice on separate lines (Serial.println) Program 4: Ultrasonic Sensor Source Code Upload Program and Test it. NOTE the program will print the distance in the serial port Assignment: Ultrasonic Sensor 1. Add a 220 Ohm resistor and LED on the circuit 2. Turn the light on when the an object gets with 20cm of ultrasonic sensor and turns off when it is out of range Program 5: Photoresistor Sensor Allows the user to implement a variable resistor that uses light to determine the resistances on the circuit based on the intensity of the light. Can also be used to sense the amount to light intensity around the photoresistor and then can activate other circuits given a defined set of constraints. Wiring Diagram Sent to Arduino Port A0 (This is an Analog Port) Ground Wire is not necessary in this case since we just want to see how much light is present. Voltage Wire is not needed as well since the surrounding light could supply the power if needed and adjust based on the intensity of the light Legs of the photoresistor are the same. Does not matter which leg the wire lines up to Write the following program Testing: Upload the program a. Open the Serial Monitor b. Place your hand over the Photoresistor > Notice the value change.