View
215
Download
0
Category
Preview:
Citation preview
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
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
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
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
Recommended