Last active
February 19, 2017 22:30
-
-
Save jywarren/f7cc7ee13e461f676c470f016305de92 to your computer and use it in GitHub Desktop.
Button turns on one of two fans, randomly. On second press, it reveals which had been on by turning on one of two corresponding LEDs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Button turns on one of two fans, randomly. On second press, it reveals which | |
had been on by turning on one of two corresponding LEDs. | |
The circuit: | |
* LED A attached from pin D11 to ground | |
* LED B attached from pin D12 to ground | |
* fan A attached from pin D2 to ground | |
* fan B attached from pin D3 to ground | |
* pushbutton attached to pin 4 from +5V | |
* 10K resistor attached to pin 4 from ground | |
Based on: | |
http://www.arduino.cc/en/Tutorial/Button | |
*/ | |
// constants won't change. They're used here to | |
// set pin numbers: | |
const int buttonPin = 4; // the number of the pushbutton pin | |
const int ledAPin = 11; // the number of the LED A pin | |
const int ledBPin = 12; // the number of the LED B pin | |
const int fanAPin = 2; // the number of the fan A pin | |
const int fanBPin = 3; // the number of the fan B pin | |
// variables will change: | |
int buttonState = 0; // variable for reading the pushbutton status | |
int state = 2; // state 0 = reset, | |
// 1 = start A or B fan, switch to state 1 on press | |
// 2 = show A or B LED, switch to 2 on press | |
int mode = 0; // mode 0 = LED & fan A, mode 1 = LED and fan B | |
int buttonPressed = 0; // switch to 0,1 when we detect a change in button state | |
void setup() { | |
// initialize serial communications at 9600 bps for debugging | |
Serial.begin(9600); | |
// initialize the LED pin as an output: | |
pinMode(ledAPin, OUTPUT); | |
pinMode(ledBPin, OUTPUT); | |
pinMode(fanAPin, OUTPUT); | |
pinMode(fanBPin, OUTPUT); | |
// initialize the pushbutton pin as an input: | |
pinMode(buttonPin, INPUT); | |
// use noise from a pin to get a better randomness starting point: | |
randomSeed(analogRead(0)); | |
// initialize all lights on: | |
digitalWrite(ledAPin, HIGH); | |
digitalWrite(ledBPin, HIGH); | |
} | |
void loop() { | |
// read the state of the pushbutton value: | |
buttonState = digitalRead(buttonPin); | |
// check if the pushbutton is currenty up or down | |
// and if it's changed since last time, trigger a state change | |
if (buttonState == HIGH && buttonPressed == 0) { | |
buttonPressed = 1; | |
trigger(); | |
delay(100); // wait for a second | |
} else if (buttonState == LOW) { | |
buttonPressed = 0; | |
delay(100); // wait for a second | |
} | |
} | |
// run when button is pressed | |
void trigger() { | |
if (state == 1) { | |
long randNumber = random(100) + 1; // generate random # | |
// set which mode we're in: | |
if (randNumber > 49) { | |
mode = 0; | |
digitalWrite(fanAPin, HIGH); | |
digitalWrite(fanBPin, LOW); | |
} else { | |
mode = 1; | |
digitalWrite(fanAPin, LOW); | |
digitalWrite(fanBPin, HIGH); | |
} | |
Serial.println(mode); | |
state = 2; | |
digitalWrite(ledAPin, LOW); | |
digitalWrite(ledBPin, LOW); | |
} else if (state == 2) { | |
// display which mode we have been in: | |
if (mode == 0) { | |
digitalWrite(ledAPin, HIGH); | |
digitalWrite(ledBPin, LOW); | |
} else { | |
digitalWrite(ledAPin, LOW); | |
digitalWrite(ledBPin, HIGH); | |
} | |
state = 0; | |
} else if (state == 0) { | |
// reset | |
digitalWrite(ledAPin, HIGH); | |
digitalWrite(ledBPin, HIGH); | |
digitalWrite(fanAPin, LOW); | |
digitalWrite(fanBPin, LOW); | |
state = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment