Presentation is loading. Please wait.

Presentation is loading. Please wait.

Warmup – 16FEB2012 This one is for practice. I have paper if you need it. Suppose there are eight, single-pole, single-throw (SPST) switches connected.

Similar presentations


Presentation on theme: "Warmup – 16FEB2012 This one is for practice. I have paper if you need it. Suppose there are eight, single-pole, single-throw (SPST) switches connected."— Presentation transcript:

1 Warmup – 16FEB2012 This one is for practice. I have paper if you need it. Suppose there are eight, single-pole, single-throw (SPST) switches connected between the PORTD pins on the Arduino and ground. Write code (pseudocode first) to test and see if any one of the switches is closed. If any one of the switches is closed, turn on an LED on PB0. Single-pole/ single-throw (SPST) PoleThrow Single-pole/ double-throw (SPDT) Pole Throws Double-pole/ single-throw (DPST) Double-pole/ double-throw (DPDT) Poles:the number of separate circuits that can be controlled by the switch Throws: the number of separate connections that can be made by the movable switch element Arduino PD0 PD7 PB0

2 Solution – Arduino Style #include “me106.h” #define LED PIN_D8 // PB0 #define ON HIGH #define OFF LOW #define N_COUNT 1 void setup() { for (int i = 0; i <= 7; i++) { pinMode(i, INPUT); digitalWrite(i, HIGH); } pinMode(LED, OUTPUT); } void loop() { byte n_low = 0; for (int i = 0; i <= 7; i++) { if ( digitalRead(i) == LOW) { n_low++; } If ( n_low >= N_COUNT ) { digitalWrite( LED, ON); n_low = 0; } else { digitalWrite( LED, OFF); n_low = 0; } Arduino PD0 PD7 PB0 Setup pins: PD0 – PD7 make INPUTS Turn on pullup resistors for PD0-PD7 Setup pin PB0 as OUTPUT Loop forever for all eight pins (PD0-PD7) if pin i is LOW increment n_low counter end if end for loop if n_low value >= n_count Turn on LED reset n_low counter else Turn off LED reset n_low counter end if Pseudocode

3 Solution – PORT Style #include “me106.h” #define LED PIN_D8 // PB0 void setup() { DDRD = 0x00; PORTD | = 0xFF; DDRB | = ( 1 << 0 ); PORTB & = ( ~ (1 << 0 ) ); } void loop() { int current_state = PIND; if ( ( ~ current_state) & 0xFF ) ) { PORTB | = ( 1 << 0 ); } else { PORTB & = ~( 1 << 0 ); } Arduino PD0 PD7 PB0 Setup pins: PD0 – PD7 make INPUTS Setup pin PB0 as OUTPUT Loop forever get current state of PD0-PD7 if any of bits are zero turn on LED else Turn off LED end if Pseudocode

4 Add blinking LED Suppose there are eight, single-pole, single-throw (SPST) switches connected between the PORTD pins on the Arduino and ground. Write code (pseudocode first) to test and see if any one of the switches is closed. If any one of the switches is closed, blink an LED on PB0 at a rate of 1 Hz. Arduino PD0 PD7 PB0


Download ppt "Warmup – 16FEB2012 This one is for practice. I have paper if you need it. Suppose there are eight, single-pole, single-throw (SPST) switches connected."

Similar presentations


Ads by Google