INC 161 , CPE 100 Computer Programming Lab 12
PC Interface with the World Command
Arduino Embedded computer (small computer) designed for easy to use Based on C language More details at http://www.arduino.cc/
Our board Arduino Duemilanove Based on CPU Atmega328 Powered by Plug-in USB
Software Download at www.arduino.cc
Structure of Arduino Program void setup() { // Commands here will run only once } void loop() // Commands here will run repeatedly
Digital Output The board pins can be programmed to give out 0 or 1 logic. 0 = 0 volts 1 = 5 volts Setup pinMode(pin,mode) - Use to set mode OUTPUT Loop digitalWrite(pin,value) - Use to set the voltage
Practice Board LED Connected to D6 – D13 Switch connected to D2 D3 D14 D15
Digital Output Example 1 void setup() { pinMode(6, OUTPUT); } void loop() digitalWrite(6, HIGH); How can you set the light to on and off?
Delay Because the board can compute quite fast, we need to add a delay. Function delay(n_millisec) will pause for n milliseconds (n is the input of the function)
Digital Output Example 2 void setup() { pinMode(6, OUTPUT); } void loop() digitalWrite(6, HIGH); delay(1000); digitalWrite(6, LOW);
Task 1 Write a program of running lights from left to right continuously.
Serial Output The board can communicate messages via serial port. Use serial monitor to view the output. Setup Serial.begin(9600) - Initialize serial port with speed 9600 bps (default) Loop Serial.print(arg) Serial.println(arg) - Use to print
Serial Output Example 3 int a = 0; void setup() { Serial.begin(9600); } void loop() Serial.print(“Value = “); Serial.println(a); delay(1000); a++;
Digital Input The board can read the value of switch. Setup pinMode(pin,mode) - Use to set mode INPUT_PULLUP Loop digitalRead(pin) - Function returns the value read from pin
Digital Input Example 4 int a; void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); } void loop() a = digitalRead(2); Serial.println(a); delay(100);