Presentation is loading. Please wait.

Presentation is loading. Please wait.

INC 161 , CPE 100 Computer Programming

Similar presentations


Presentation on theme: "INC 161 , CPE 100 Computer Programming"— Presentation transcript:

1 INC 161 , CPE 100 Computer Programming
Lab 12

2 PC Interface with the World
Command

3 Arduino Embedded computer (small computer) designed for easy to use
Based on C language More details at

4 Our board Arduino Duemilanove Based on CPU Atmega328
Powered by Plug-in USB

5 Software Download at

6 Structure of Arduino Program
void setup() { // Commands here will run only once } void loop() // Commands here will run repeatedly

7 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

8 Practice Board LED Connected to D6 – D13 Switch
connected to D2 D3 D14 D15

9 Digital Output Example 1
void setup() { pinMode(6, OUTPUT); } void loop() digitalWrite(6, HIGH); How can you set the light to on and off?

10 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)

11 Digital Output Example 2
void setup() { pinMode(6, OUTPUT); } void loop() digitalWrite(6, HIGH); delay(1000); digitalWrite(6, LOW);

12 Task 1 Write a program of running lights from left to right continuously.

13 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

14 Serial Output Example 3 int a = 0; void setup() { Serial.begin(9600);
} void loop() Serial.print(“Value = “); Serial.println(a); delay(1000); a++;

15 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

16 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);


Download ppt "INC 161 , CPE 100 Computer Programming"

Similar presentations


Ads by Google