4
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) Pole Throw 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

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

Embed Size (px)

Citation preview

Page 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

Warmup – 16FEB2012This 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) Pole Throw

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

Page 2: 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

Solution – Arduino Style#include “me106.h”#define LED PIN_D8 // PB0#define ON HIGH#define OFF LOW#define N_COUNT 1void 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 INPUTSTurn on pullup resistors for PD0-PD7Setup pin PB0 as OUTPUTLoop 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

Page 3: 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

Solution – PORT Style

#include “me106.h”#define LED PIN_D8 // PB0void 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 INPUTSSetup pin PB0 as OUTPUTLoop forever get current state of PD0-PD7 if any of bits are zero turn on LED else Turn off LED end if

Pseudocode

Page 4: 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

Add blinking LEDSuppose 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