ARDUINO 1 By Wilmer Arellano Arduino is an
ARDUINO 1 By Wilmer Arellano
Arduino is an open-source electronics prototyping platform based on flexible, easy-touse hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. http: //www. arduino. cc/
Arduino Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing). http: //www. arduino. cc/
Arduino If you like processing please visit: http: //openprocessing. org/ You can find another interesting software at: http: //fritzing. org/ This software allows for easy documenting and PCB generation of Arduino projects.
Arduino projects can be stand-alone or they can communicate with software on running on a computer (e. g. Flash, Processing, Max. MSP). http: //www. arduino. cc/
Download the latest version from the page: http: //arduino. cc/en/Main/Software
Basics In the Arduino environment programs are referred to as sketches http: //www. arduino. cc/ http: //arduino. cc/en/Reference/Home. Page http: //arduino. cc/en/Tutorial/Foundations
Basics Comments /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on. . . We use pin 13 because, * depending on your Arduino board, it has either a built-in LED * or a built-in resistor so that you need only an LED. * * http: //www. arduino. cc/en/Tutorial/Blink */ // LED connected to digital pin 13
Digital / Analog
Power The Arduino Duemilanove can be powered via the USB connection or with an external power supply. The power source is selected automatically. The Arduino Diecimila requires the user to select the power option with a jumper External (non-USB) power can come either from an AC-to-DC adapter (wall-wart) or battery. The adapter can be connected by plugging a 2. 1 mm center-positive plug into the board's power jack. Leads from a battery can be inserted in the Gnd and Vin pin headers of the POWER connector
Power The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7 V, however, the 5 V pin may supply less than five volts and the board may be unstable. If using more than 12 V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.
Arduino Microcontroller Boards 13 The power pins are as follows: • Vin. The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin. • 5 V. The regulated power supply used to power the microcontroller and other components on the board. This can come either from Vin via an on-board regulator, or be supplied by USB or another regulated 5 V supply. • 3 V 3. A 3. 3 volt supply generated by the on-board FTDI chip. Maximum current draw is 50 m. A. • GND. Ground pins. USB connector Power connector 3 V 3 output 5 V output Vin
breadboards A breadboard is used to make up temporary circuits for testing or to try out an idea. No soldering is required so it is easy to change connections and replace components. Parts will not be damaged so they will be available to re-use afterwards. http: //www. kpsec. freeuk. com/ breadb. htm
Basic Programming Declare variables at top setup() and loop() There are two special functions that are a part of every Arduino sketch: setup() and loop(). The setup() is called once, when the sketch starts. It's a good place to do setup tasks like setting pin modes or initializing libraries. The loop() function is called over and is heart of most sketches. You need to include both functions in your sketch, even if you don't need them for anything.
Basic Programming void setup() { } void loop() { }
Variables You can use variables in a similar way as they are used in math or physics All variables have to be declared before they are used , and optionally, set an initial value (initializing the variable).
int Example int led. Pin = 13; Syntax int var = val;
ASCII Table
/* * “Hello World!” * This is the Hello World! for Arduino. * It shows how to send data to the computer */ void setup() // run once, when the sketch starts { Serial. begin(9600); // set up Serial library at 9600 bps Serial. println("Is anybody out there? "); line break // prints phrase with ending } void loop() // run over and over again { // do nothing! } // After sending program to the Arduino, press Reset button on the board and watch Serial monitor
Basic Programming The pin. Mode() function configures a pin as either an input or an output. To use it: You pass it the number of the pin to configure and the constant INPUT or OUTPUT. pin. Mode(11, INPUT); pin. Mode(13, OUTPUT); When configured as an input, a pin can detect the state of a sensor like a pushbutton. As an output, it can drive an actuator like an LED.
Basic Programming The digital. Write() functions outputs a value on a pin. Possible values are: LOW (0 V)or HIGH (5 V) For example: digital. Write(13, HIGH); digital. Write(11, LOW);
Basic Programming The delay() causes the Arduino to wait for the specified number of milliseconds before continuing on to the next line. There are 1000 milliseconds in a second, so the line: delay(1000); creates a delay of one second.
/* *Blink *Turns on an LED on for one second, then off for one second, repeatedly. *The circuit: * LED connected from digital pin 13 to ground. * Note: On most Arduino boards, there is already an LED on the board * connected to pin 13, so you don't need any extra components for this example. *Created 1 June 2005 *By David Cuartielles *http: //arduino. cc/en/Tutorial/Blink *based on an orginal by H. Barragan for the Wiring i/o board */ int led. Pin = 13; // LED connected to digital pin 13 // The setup() method runs once, when the sketch starts void setup() { // initialize the digital pin as an output: pin. Mode(led. Pin, OUTPUT); } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { digital. Write(led. Pin, HIGH); // set the LED on delay(1000); // wait for a second digital. Write(led. Pin, LOW); // set the LED off delay(1000); // wait for a second }
int led. Pin = 13; // LED connected to digital pin 13 void setup() { pin. Mode(led. Pin, OUTPUT); } void loop() { digital. Write(led. Pin, HIGH); // set the LED on delay(1000); // wait for a second digital. Write(led. Pin, LOW); // set the LED off delay(1000); // wait for a second }
Fading with digital. Write
delay. Microseconds(us) us: the number of microseconds to pause (unsigned int) Example int out. Pin = 8; // digital pin 8 void setup() { pin. Mode(out. Pin, OUTPUT); // sets the digital pin as output } void loop() { digital. Write(out. Pin, HIGH); // sets the pin on delay. Microseconds(50); // pauses for 50 microseconds digital. Write(out. Pin, LOW); // sets the pin off delay. Microseconds(50); // pauses for 50 microseconds } unsigned int Description On the Uno and other ATMEGA based boards, unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value. Instead of storing negative numbers however they only store positive values, yielding a useful range of 0 to 65, 535 (2^16) - 1).
delay. Microseconds(us) Caveats and Known Issues This function works very accurately in the range 3 microseconds and up. We cannot assure that delay. Microseconds will perform precisely for smaller delay-times.
int led. Pin = 9, fade. Value = 128, fading; unsigned int time. On, time. Off; long period = 30000, repetitions, long. Time. On; // LED connected to digital pin 9 void setup() { pin. Mode(led. Pin, OUTPUT); } void loop() { long. Time. On = (long) fade. Value*period/255; time. On = long. Time. On; time. Off = period - time. On + 1; digital. Write(led. Pin, HIGH); delay. Microseconds(time. On); digital. Write(led. Pin, LOW); delay. Microseconds(time. Off); } Cast Description The cast operator translates one variable type into another and forces calculations to be performed in the cast type. Syntax (type)variable Parameters
int led. Pin = 9, fade. Value, fading; unsigned int time. On, time. Off; long period = 255, repetitions, holding. Time = 30000; // LED connected to digital pin 9 void setup() { pin. Mode(led. Pin, OUTPUT); repetitions = holding. Time/period; } void loop() { // fade in from min to max in increments of 5 points: for(fade. Value = 1 ; fade. Value <= 254; fade. Value +=5) { for(fading = 0; fading <= repetitions; fading++){ time. On = fade. Value; time. Off = period - time. On; digital. Write(led. Pin, HIGH); delay. Microseconds(time. On); digital. Write(led. Pin, LOW); delay. Microseconds(time. Off); } // fade out from max to min in increments of 5 points: for(fade. Value = 254 ; fade. Value >= 1; fade. Value -=5) { for(fading = 0; fading <= repetitions; fading++){ time. On = fade. Value; time. Off = period - time. On; digital. Write(led. Pin, HIGH); delay. Microseconds(time. On); digital. Write(led. Pin, LOW); delay. Microseconds(time. Off); } } }
int led. Pin = 9, fade. Value, fading; unsigned int time. On, time. Off; long period = 255, repetitions, holding. Time = 30000; // LED connected to digital pin 9 void setup() { pin. Mode(led. Pin, OUTPUT); repetitions = holding. Time/period; } void loop() { // fade in from min to max in increments of 5 points: for(fade. Value = 1 ; fade. Value <= 254; fade. Value +=5) { fade(fade. Value); } // fade out from max to min in increments of 5 points: for(int fade. Value = 254 ; fade. Value >= 1; fade. Value -=5) { fade(fade. Value); } } void fade(int fade. Value){ for(int fading = 0; fading <= repetitions; fading++){ time. On = fade. Value; time. Off = period - time. On; digital. Write(led. Pin, HIGH); delay. Microseconds(time. On); digital. Write(led. Pin, LOW); delay. Microseconds(time. Off); } }
Int led. Red = 13; int led. Green = 11; int led. Yellow = 12; void setup() { pin. Mode(led. Red, OUTPUT); // sets the digital pin as output pin. Mode(led. Yellow, OUTPUT); // sets the digital pin as output pin. Mode(led. Green, OUTPUT); // sets the digital pin as output } void loop() { digital. Write(led. Green, HIGH); // sets the Green LED on delay(1000); // waits for a second digital. Write(led. Green, LOW); // sets the Green LED off digital. Write(led. Yellow, HIGH); // sets the Yellow LED on delay(1000); // waits for a second digital. Write(led. Yellow, LOW); // sets the Yellow LED off digital. Write(led. Red, HIGH); // sets the Red LED on delay(1000); // waits for a second digital. Write(led. Red, LOW); // sets the Reed LED off }
Lots of useful functions pin. Mode() – set a pin as input or output digital. Write() – set a digital pin high/low digital. Read() – read a digital pin’s state analog. Read() – read an analog pin analog. Write() – write an “analog” PWM value delay() – wait an amount of time (ms) millis() – get the current time
Traffic Light Control Int led. Red = 13; int led. Green = 11; int led. Yellow = 12; void setup() { pin. Mode(led. Red, OUTPUT); // sets the digital pin as output pin. Mode(led. Yellow, OUTPUT); // sets the digital pin as output pin. Mode(led. Green, OUTPUT); // sets the digital pin as output } void loop() { digital. Write(led. Green, HIGH); // sets the Green LED on delay(1000); // waits for a second digital. Write(led. Green, LOW); // sets the Green LED off digital. Write(led. Yellow, HIGH); // sets the Yellow LED on delay(1000); // waits for a second digital. Write(led. Yellow, LOW); // sets the Yellow LED off digital. Write(led. Red, HIGH); // sets the Red LED on delay(1000); // waits for a second digital. Write(led. Red, LOW); // sets the Reed LED off }
//LED Pin Variables int led. Pins[] = {2, 3, 4, 5, 6, 7, 8, 9}; //An array to hold the pin each LED is connecte d to //i. e. LED #0 is connected to pin 2, LED #1, 3 and so on //to address an array use led. Pins[0] this would equal 2 //and led. Pins[7] would equal 9 /* * setup() - this function runs once when you turn your Arduino on * We three control pins to outputs */
void setup() { //Set each pin connected to an LED to output mode (pulling high (on) or low (off) for(int i = 0; i < 8; i++){ //this is a loop and will repeat eight times pin. Mode(led. Pins[i], OUTPUT); //we use this to set each LED pin to output } //the code this replaces is below /* (commented code will not run) * these are the lines replaced by the for loop above they do exactly the * same thing the one above just uses less typing pin. Mode(led. Pins[0], OUTPUT); pin. Mode(led. Pins[1], OUTPUT); pin. Mode(led. Pins[2], OUTPUT); pin. Mode(led. Pins[3], OUTPUT); pin. Mode(led. Pins[4], OUTPUT); pin. Mode(led. Pins[5], OUTPUT); pin. Mode(led. Pins[6], OUTPUT); pin. Mode(led. Pins[7], OUTPUT); (end of commented code)*/ }
void one. After. Another. No. Loop(){ int delay. Time = 100; //the time (in milliseconds) to pause between LEDs //make smaller for quicker switching and larger for slower digital. Write(led. Pins[0], HIGH); //Turns on LED #0 (connected to pin 2 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[1], HIGH); //Turns on LED #1 (connected to pin 3 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[2], HIGH); //Turns on LED #2 (connected to pin 4 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[3], HIGH); //Turns on LED #3 (connected to pin 5 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[4], HIGH); //Turns on LED #4 (connected to pin 6 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[5], HIGH); //Turns on LED #5 (connected to pin 7 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[6], HIGH); //Turns on LED #6 (connected to pin 8 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[7], HIGH); //Turns on LED #7 (connected to pin 9 ) delay(delay. Time); //waits delay. Time milliseconds
//Turns Each LED Off digital. Write(led. Pins[7], LOW); //Turns on LED #0 (connected to pin 2 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[6], LOW); //Turns on LED #1 (connected to pin 3 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[5], LOW); //Turns on LED #2 (connected to pin 4 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[4], LOW); //Turns on LED #3 (connected to pin 5 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[3], LOW); //Turns on LED #4 (connected to pin 6 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[2], LOW); //Turns on LED #5 (connected to pin 7 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[1], LOW); //Turns on LED #6 (connected to pin 8 ) delay(delay. Time); //waits delay. Time milliseconds digital. Write(led. Pins[0], LOW); //Turns on LED #7 (connected to pin 9 ) delay(delay. Time); //waits delay. Time milliseconds }
//Turn Each LED on one after another for(int i = 0; i <= 7; i++){ digital. Write(led. Pins[i], HIGH); //Turns on LED #i each time this runs i delay(delay. Time); //gets one added to it so this will repeat } //8 times the first time i will = 0 the final //time i will equal 7; //Turn Each LED off one after another for(int i = 7; i >= 0; i--){ //same as above but rather than starting at 0 and counting u //p //we start at seven and count down digital. Write(led. Pins[i], LOW); //Turns off LED #i each time this runs i delay(delay. Time); //gets one subtracted from it so this will repeat } //8 times the first time i will = 7 the final //time it will equal 0
void loop() // run over and over again { motor. On. Then. Off(); //motor. On. Then. Off. With. Speed(); //motor. Acceleration(); }
void motor. On. Then. Off(){ int on. Time = 2500; //the number of milliseconds for the motor to turn on for int off. Time = 1000; //the number of milliseconds for the motor to turn off for digital. Write(motor. Pin, HIGH); // turns the motor On delay(on. Time); // waits for on. Time milliseconds digital. Write(motor. Pin, LOW); // turns the motor Off delay(off. Time); // waits for off. Time milliseconds }
/* * motor. On. Then. Off. With. Speed() - turns motor on then off but uses speed values as well * (notice this code is identical to the code we used for * the blinking LED) */ void motor. On. Then. Off. With. Speed(){ int on. Speed = 200; // a number between 0 (stopped) and 255 (full speed) int on. Time = 2500; //the number of milliseconds for the motor to turn on for int off. Speed = 50; // a number between 0 (stopped) and 255 (full speed) int off. Time = 1000; //the number of milliseconds for the motor to turn off for analog. Write(motor. Pin, on. Speed); // turns the motor On delay(on. Time); // waits for on. Time milliseconds analog. Write(motor. Pin, off. Speed); // turns the motor Off delay(off. Time); // waits for off. Time milliseconds }
/* * motor. Acceleration() - accelerates the motor to full speed then * back down to zero */ void motor. Acceleration(){ int delay. Time = 50; //milliseconds between each speed step //Accelerates the motor for(int i = 0; i < 256; i++){ //goes through each speed from 0 to 255 analog. Write(motor. Pin, i); //sets the new speed delay(delay. Time); // waits for delay. Time milliseconds } //Decelerates the motor for(int i = 255; i >= 0; i--){ //goes through each speed from 255 to 0 analog. Write(motor. Pin, i); //sets the new speed delay(delay. Time); // waits for delay. Time milliseconds } }
If / else if/else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. For example, an analog input could be tested and one action taken if the input was less than 500, and another action taken if the input was 500 or greater. The code would look like this: if (Comparison) { // action A (Comparison true) } else { // action B (Comparison false) }
Comparison Operators x < y (x is less than y) x > y (x is greater than y) x <= y (x is less than or equal to y) x >= y (x is greater than or equal to y) x == y (x is equal to y) x != y (x is not equal to y)
ARDX Practice examples: 1, 2, 3, 6, 7, 8
Button Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the
int button. Pin = 2; // the number of the pushbutton pin int led. Pin = 13; // the number of the LED pin int button. State = 0; // variable for reading the pushbutton status void setup() { pin. Mode(led. Pin, OUTPUT); // initialize the LED pin as an output: pin. Mode(button. Pin, INPUT); // initialize the pushbutton pin as an input: } void loop(){ button. State = digital. Read(button. Pin); // read the state of the pushbutton value: if (button. State == HIGH) { // check if the pushbutton is pressed. If it is, the button. State is HIGH: digital. Write(led. Pin, HIGH); // turn LED on: } else { digital. Write(led. Pin, LOW); // turn LED off: } }
Button The problem with the last program is that the switch has to remain pressed in order for the LED to turn on We want the LED to change state when we press the button and to stay in the new state when the button is released
int button. Pin = 2; // the pin that the pushbutton is attached to int led. Pin = 13; // the pin that the LED is attached to int button. State = 0; // current state of the button int last. LEDState = 0; // previous state of the button void setup() { pin. Mode(button. Pin, INPUT); // initialize the button pin as a input: pin. Mode(led. Pin, OUTPUT); // initialize the LED as an output: } void loop() { button. State = digital. Read(button. Pin); // read the pushbutton input pin: if (button. State == HIGH) { // Determine if button State is HIGH if (last. LEDState == HIGH) { // if the current state is HIGH then turn LED off digital. Write(led. Pin, LOW); last. LEDState = LOW; } else {// if the current state is LOW then turn LED on digital. Write(led. Pin, HIGH); last. LEDState = HIGH; } while(button. State == HIGH){ button. State = digital. Read(button. Pin); // read the pushbutton input pin: }; delay(250); } }
Arithmetic Operators Arithmetic operators include addition, subtraction, multiplication, and division. They return the sum, difference, product, or quotient of two operands. y x i r = = y x j r + * / 3; 7; 6; 5;
/* Math */ int a = 5; int b = 10; int c = 20; void setup() { Serial. begin(9600); // set up Serial library at 9600 bps Serial. println("Here is some math: "); Serial. print("a = "); Serial. println(a); What do you see on the Serial Monitor? Serial. print("b = "); Serial. println(b); Replace format “int” with “float” Serial. print("c = "); Serial. println(c); Serial. print("a + b = "); // add Run this program again. // multiply What do you see on the Serial Monitor? Serial. println(a + b); Serial. print("a * c = "); 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 { } Run this program.
Playing tones /*Syntax tone(pin, frequency) tone(pin, frequency, duration) Parameters pin: the pin on which to generate the tone frequency: the frequency of the tone in hertz duration: the duration of the tone in milliseconds (optional) */ int speaker. Pin = 11; void setup() { pin. Mode(speaker. Pin, OUTPUT); } void loop() { tone(speaker. Pin, 262, 1000); // Generate tone delay(2000); }
Playing tunes #include "pitches. h" int speaker. Pin = 11; int i = 0; int length = 15; // the number of notes int melody[] = {n_C 4, n_G 4, n_A 4, n_G 4, n_F 4, n_E 4, n_D 4, n_C 4, n_P}; // P represents a rest int beats[] = { 1, 1, 1, 2, 4 }; int tempo = 300; void setup() { pin. Mode(speaker. Pin, OUTPUT); } void loop() { while (i < length) { tone(speaker. Pin, melody[i], beats[i]*tempo); delay(beats[i]*tempo + tempo / 2); // pause between notes i = i + 1; } }
#include /************************* * Public Constants *************************/ #define P 0 #define B 0 31 #define C 1 33 #define CS 1 35 #define D 1 37 #define DS 1 39 #define E 1 41 #define F 1 44 #define FS 1 46 #define G 1 49 #define GS 1 52 #define A 1 55 #define AS 1 58 #define B 1 62 #define C 2 65 #define CS 2 69 #define D 2 73 Download and install under Arduino libraries Pitches
#include "pitches. h" int speaker. Pin = 11; int i = 0; int length = 15; // the number of notes int melody[] = {n_C 4, n_G 4, n_A 4, n_G 4, n_F 4, n_E 4, n_D 4, n_C 4, n_P}; // P represents a rest int beats[] = { 1, 1, 1, 2, 4 }; int tempo = 300; void setup() { delay(1000); pin. Mode(speaker. Pin, OUTPUT); } void loop() { while (digital. Read(1)&&(i<length)) {// Plays only if pin 1 is HIGH tone(speaker. Pin, melody[i], beats[i]*tempo); delay(beats[i]*tempo + tempo / 2); // pause between notes i = i + 1; } i=0; }
#include "pitches. h" int speaker. Pin = 11; int i = 0; int length = 15; // the number of notes int melody[] = {n_C 4, n_G 4, n_A 4, n_G 4, n_F 4, n_E 4, n_D 4, n_C 4, n_P}; // P represents a rest int beats[] = { 1, 1, 1, 2, 4 }; int tempo = 300; void setup() { delay(1000); pin. Mode(speaker. Pin, OUTPUT); pin. Mode(1, OUTPUT); digital. Write(1, LOW); } void loop() { while ((i<length)) { digital. Write(1, HIGH); tone(speaker. Pin, melody[i], beats[i]*tempo); delay(beats[i]*tempo + tempo / 2); // pause between notes i = i + 1; } i=0; digital. Write(1, LOW); }
A B D E F G
Keyboard 1 #include "pitches. h" int speaker. Pin = 11; int key 1 = 1, key 2 = 2, key 3 = 3; int melody[] = {0, A 4}; int keep_on = 20; int play; void setup() { pin. Mode(speaker. Pin, OUTPUT); pin. Mode(key 1, INPUT); } void loop() { play = digital. Read(key 1); if(play == 1){ tone(speaker. Pin, melody[key 1], keep_on); play = 0; } }
#include "pitches. h" int speaker. Pin = 11; int key 1 = 1, key 2 = 2, key 3 = 3; int melody[] = {0, A 4, B 4, C 3 }; int keep_on = 20; int play; void setup() { pin. Mode(speaker. Pin, OUTPUT); pin. Mode(key 1, INPUT); pin. Mode(key 2, INPUT); pin. Mode(key 3, INPUT); } void loop() { play = digital. Read(key 1); if(play == 1){ tone(speaker. Pin, melody[key 1], keep_on); play = 0; } play = digital. Read(key 2); if(play == 1){ tone(speaker. Pin, melody[key 2], keep_on); play = 0; } play = digital. Read(key 3); if(play == 1){ tone(speaker. Pin, melody[key 3], keep_on); play = 0; } }
Voltage measurement unsigned int voltage 1, distance 1; int sensor 1 = 0; void setup() { Serial. begin(9600); // setup serial } void loop() { voltage 1 = analog. Read(sensor 1); Serial. println(voltage 1); // debug value delay(1000); }
distance = 32/voltage Good for voltage < 2. 6 V, distance > 10 cm
Voltage measurement int analog. Read(pin) Description Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano), 10 -bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, . 0049 volts (4. 9 m. V) per unit. It takes about 100 us (0. 0001 s) to read an analog input, so the maximum reading rate is about 10, 000 times a second.
Blinking Led /* */ int red. Led. Pin = 13; void setup() { pin. Mode(red. Led. Pin, OUTPUT); } void loop() { digital. Write(red. Led. Pin, LOW); delay(1000); digital. Write(red. Led. Pin, HIGH); delay(1000); }
File/examples/01. Basics/Fade int led = 9; // the PWM pin the LED is attached to int brightness = 0; // how bright the LED is int fade. Amount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pin. Mode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analog. Write(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fade. Amount; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fade. Amount = -fade. Amount; } // wait for 30 milliseconds to see the dimming effect delay(30); }
Led Switch /* */ boolean state = 0; int green. Led. Pin = 12; int switch. Pin = 2; void setup() { pin. Mode(green. Led. Pin, OUTPUT); pin. Mode(switch. Pin, INPUT); } void loop() { state = digital. Read(switch. Pin); if(state == 0){ digital. Write(green. Led. Pin, LOW); } else{ digital. Write(green. Led. Pin, HIGH); } }
Toggle Switch /* */ boolean state = 0; int switch. Read; int green. Led. Pin = 12; int switch. Pin = 2; void setup() { pin. Mode(green. Led. Pin, OUTPUT); pin. Mode(switch. Pin, INPUT); } void loop() { if(digital. Read(switch. Pin) == 0){ state = !state; } if(state == 0){ digital. Write(green. Led. Pin, LOW); } else{ digital. Write(green. Led. Pin, HIGH); } }
LED Switch with Debouncing /* */ boolean state = 0; boolean button. Enable = true; int switch. Read; int green. Led. Pin = 12; int switch. Pin = 2; void setup() { pin. Mode(green. Led. Pin, OUTPUT); pin. Mode(switch. Pin, INPUT); } void loop() { if(digital. Read(switch. Pin) == 0){ while(digital. Read(switch. Pin) == 0); state = !state; delay(10); } if(state == 0){ digital. Write(green. Led. Pin, LOW); } else{ digital. Write(green. Led. Pin, HIGH); } }
Toggle + Blinking /* */ boolean state = 0; boolean button. Enable = true; int switch. Read; int red. Led. Pin = 13; int green. Led. Pin = 12; int switch. Pin = 2; void setup() { pin. Mode(red. Led. Pin, OUTPUT); pin. Mode(green. Led. Pin, OUTPUT); pin. Mode(switch. Pin, INPUT); } void loop() { digital. Write(red. Led. Pin, LOW); delay(1000); digital. Write(red. Led. Pin, HIGH); delay(1000); if(digital. Read(switch. Pin) == 0){ while(digital. Read(switch. Pin) == 0); state = !state; delay(10); } if(state == 0){ digital. Write(green. Led. Pin, LOW); } else{ digital. Write(green. Led. Pin, HIGH); } }
Toggle + Blinking 2 /* */ boolean state = 0; boolean button. Enable = true; boolean high_low = false; int switch. Read; int red. Led. Pin = 13; int green. Led. Pin = 12; int switch. Pin = 2; long last. Time. Switched; void setup() { pin. Mode(red. Led. Pin, OUTPUT); pin. Mode(green. Led. Pin, OUTPUT); pin. Mode(switch. Pin, INPUT); last. Time. Switched = millis(); } void loop() { if(high_low){ digital. Write(red. Led. Pin, LOW); } if(!high_low){ digital. Write(red. Led. Pin, HIGH); } if(millis() - last. Time. Switched > 1000){ high_low = !high_low; last. Time. Switched = millis(); } if(digital. Read(switch. Pin) == 0){ while(digital. Read(switch. Pin) == 0); state = !state; delay(10); } if(state == 0){ digital. Write(green. Led. Pin, LOW); } else{ digital. Write(green. Led. Pin, HIGH); } }
Analog and Serial Communication char selection = 0; int sensor. Pin = A 0; int voltage; void setup() { Serial. begin(9600); // Serial 1. begin(9600); } void loop() { if (Serial. available()) { // If anything comes in Serial (USB), selection = Serial. read(); if(selection == 'V'){ Serial. print("Voltage "); voltage = analog. Read(sensor. Pin); Serial. println(voltage); selection = 0; } else{ if(selection != 10){ Serial. println("Invalid Input"); } }
General Recommendations
Report Format For the Traffic Light Controller just report code as indicated in next slide. For the Multimeter and the Robot use the format indicated in : http: //web. eng. fiu. edu/~arellano/1002/Resourc es/Robot. Report. doc
Reporting Code When writing the code part of your projects: Show the pseudo-code Show the flowcharts before Show the code. For good and brief description, please follow the link bellow. http: //www. unf. edu/~broggio/cop 2221/2221 pseu. h tm. Don’t forget to use indentations where needed.
Reporting Code For learning the basics of Flow Charts, the links bellow could be used as a guide: https: //www. computerhope. com/jargon/f/flowchar. ht m http: //www. mindtools. com/pages/article/new. TMC_9 7. htm
Design a Traffic Light Controller
Traffic Light Controller with Pedestrian Cycle The traffic light must be doing the car cycle continuously (Green, Yellow, Red, repeat) The cycle for the pedestrian must be started by pressing a button.
Design a Multimeter as Explained in Class
5 V 1 30 k. Ohm 2 30 k. Ohm 3 30 k. Ohm 1. Calculate voltages at points 1, 2, and 3 2. Measure voltages at points 1, 2, and 3 3. Compare values from 1. and 2. and comment
5 V Rx Ri 1. Repeat the procedure bellow for values of Rx of 10 and 30 k. Ohm 2. Calculate current for Ri of 220 and 1, 000 Ohms 3. Measure current for Ri of 220 and 1, 000 Ohms 4. Compare values from 2. and 3. and comment
Robot Project Robot should autonomously follow a dark line. The line may include turns at different angles and curved segments The line is not closed and would have object before one end (End 1) and will end abruptly at the other end (End 2). The robot will start close to the object and facing End 2 and will make a given number of repetitions on this track. When reaching End 2 the robot will find a way to get back on the track facing End 1 and continue navigation. When getting at 15 cm away from the object, the robot should make a 180 degree turn to face End 2 and continue
- Slides: 99