Created
September 23, 2018 16:59
-
-
Save jimmyjxiao/4f9a1881077d60aa9a08106bd81e56d2 to your computer and use it in GitHub Desktop.
For EGR101
This file contains hidden or 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
/* | |
Dorm room sign code by: | |
Jimmy Xiao | |
Amanda Andriessen | |
Based on: | |
Exploring Arduino - Code Listing 2-4: Simple LED Control with a Button | |
http://www.exploringarduino.com/content/ch2 | |
Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com ) | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License v3 as published by | |
the Free Software Foundation. | |
*/ | |
// const int definitions to refer back to the pin numbers easily in later code. | |
const int LED_A=9; //The LED A (for Amanda) is connected to pin 9 | |
const int BUTTON_A=2; //The Button A is connected to pin 2 | |
const int LED_E = 10; //The LED E (for Elena) is connected to pin 10 | |
const int BUTTON_E= 3; //The Button E is connected to pin 3 | |
void setup() | |
{ | |
pinMode (LED_A, OUTPUT); //Set the LED A pin as an output | |
pinMode (BUTTON_A, INPUT); //Set button A as input | |
pinMode (LED_E, OUTPUT); //Set the LED E pin as an output | |
pinMode (BUTTON_E, INPUT); //Set button E as input | |
} | |
void loop() // main loop | |
{ | |
static bool light_state_A = true; //holds the state of the led. Static so it doesn't get deleted once this stack ends and loop gets called again | |
if (digitalRead(BUTTON_A) == HIGH) //If Button is pressed | |
{ | |
light_state_A = !light_state_A; //flip state | |
if(light_state_A) // turn on/off LED pin according to state variable | |
digitalWrite(LED_A, HIGH); | |
else | |
digitalWrite(LED_A,LOW); | |
delay(100); // delay 100 ms because without this, the state will flip again if the user holds the button it down for more than one loop duration which is pretty much guaranteed unless they were super fast about pressing it | |
} | |
static bool light_state_E = true; //everything here is identical except LED and buttons for Elena instead of Amanda | |
if (digitalRead(BUTTON_E) == HIGH) | |
{ | |
light_state_E = !light_state_E; | |
if(light_state_E) | |
digitalWrite(LED_E, HIGH); | |
else | |
digitalWrite(LED_E,LOW); | |
delay(100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment