This code adapt from @Robin2 "Demonstration code for several things at the same time"
http://forum.arduino.cc/index.php?topic=223286.0
// This code adapt from Robin2 "Demonstration code for several things at the same time"
// http://forum.arduino.cc/index.php?topic=223286.0
// this sketch does the following
// it turns the Led (buttonLed connected to pin 11,12,13) on or off whenever a button
// connected to pin 2,3,4 is pressed
// One leg of each LED should be connected to the relevant pin and the other leg should be connected to a
// resistor of 470 ohms or more and the other end of the resistor to the Arduino GND.
// If the LED doesn't light its probably connected the wrong way round.
// The use of millis() to manage the timing of activities
// The definition of all numbers used by the program at the top of the sketch where
// they can easily be found if they need to be changed
//========================================
// --------CONSTANTS (won't change)---------------
const int buttonLed1_Pin = 13; // the pin numbers for the LEDs
const int buttonLed2_Pin = 12;
const int buttonLed3_Pin = 11;
const int buttonPin1 = 2; // the pin number for the button
const int buttonPin2 = 3; // the pin number for the button
const int buttonPin3 = 4; // the pin number for the button
const int buttonInterval = 300; // number of millisecs between button readings
//------------ VARIABLES (will change)---------------------
byte buttonLed1_State = LOW; // used to record whether the LEDs are on or off
byte buttonLed2_State = LOW; // LOW = off
byte buttonLed3_State = LOW;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousButtonMillis = 0; // time when button press last checked
//========================================
void setup() {
Serial.begin(9600);
Serial.println("Starting LED Button"); // so we know what sketch is running
// set the Led pins as output:
pinMode(buttonLed1_Pin, OUTPUT);
pinMode(buttonLed2_Pin, OUTPUT);
pinMode(buttonLed3_Pin, OUTPUT);
// set the button pin as input with a pullup resistor to ensure it defaults to HIGH
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
}
//========================================
void loop() {
// Notice that none of the action happens in loop() apart from reading millis()
// it just calls the functions that have the action code
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
readButton(); // call the functions that do the work
switchLeds();
}
//========================================
void switchLeds() {
// this is the code that actually switches the LEDs on and off
digitalWrite(buttonLed1_Pin, buttonLed1_State);
digitalWrite(buttonLed2_Pin, buttonLed2_State);
digitalWrite(buttonLed3_Pin, buttonLed3_State);
}
//========================================
void readButton1() {
// this only reads the button state after the button interval has elapsed
// this avoids multiple flashes if the button bounces
// every time the button is pressed it changes buttonLed_State causing the Led to go on or off
// Notice that there is no need to synchronize this use of millis() with the flashing Leds
if (millis() - previousButtonMillis >= buttonInterval) {
if (digitalRead(buttonPin1) == LOW) {
buttonLed1_State = ! buttonLed1_State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
}
if (digitalRead(buttonPin2) == LOW) {
buttonLed2_State = ! buttonLed2_State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
}
if (digitalRead(buttonPin3) == LOW) {
buttonLed3_State = ! buttonLed3_State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
}
previousButtonMillis = millis();
}
}
//========================================END