#include <Indio.h>
#include <Wire.h>
#include <EEPROM.h>
#include <UC1701.h>
const int T1Pin = 2; // the number of the pushbutton pin
const int T2Pin = 3; // the number of the pushbutton pin
const int T3Pin = 4; // the number of the pushbutton pin
const int T4Pin = 5; // the number of the pushbutton pin
const int T5Pin = 6; // the number of the pushbutton pin
const int B1Pin = 7; // the number of the LED pin
const int B2Pin = 8; // the number of the LED pin
const int B3Pin = 9; // the number of the LED pin
const int B4Pin = 10; // the number of the LED pin
int SHORT_TIME = 2000; /* Time that will be considered as the short press time */
long ON_Duration; /* Variable that will store the value of time for which the button is pressed */
int previousState = LOW; /* Setting the initial state of push button HIGH as we are using the INPUT_PULLUP mode */
int presentState; /* Variable that will store the present state if the button*/
unsigned long press_Time = 0; /* Time at which the button was pressed */
unsigned long release_Time = 0; /*Time at which the button is released */
// Variables will change:
int B1State = HIGH; // the current state of the output pin
int B2State = HIGH; // the current state of the output pin
int B3State = HIGH; // the current state of the output pin
int B4State = LOW; // the current state of the output pin
int T1buttonState; // the current reading from the input pin
int T2buttonState; // the current reading from the input pin
int T3buttonState; // the current reading from the input pin
int T4buttonState; // the current reading from the input pin
int T5buttonState; // the current reading from the input pin
int T1lastButtonState = LOW; // the previous reading from the input pin
int T2lastButtonState = LOW; // the previous reading from the input pin
int T3lastButtonState = LOW; // the previous reading from the input pin
int T4lastButtonState = LOW; // the previous reading from the input pin
int T5lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long T1lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long T2lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long T3lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long T4lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long T5lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long T1debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long T2debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long T3debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long T4debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long T5debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(T1Pin, INPUT_PULLUP);
pinMode(T2Pin, INPUT_PULLUP);
pinMode(T3Pin, INPUT_PULLUP);
pinMode(T4Pin, INPUT_PULLUP);
pinMode(T5Pin, INPUT_PULLUP);
pinMode(B1Pin, OUTPUT);
pinMode(B2Pin, OUTPUT);
pinMode(B3Pin, OUTPUT);
pinMode(B4Pin, OUTPUT);
// set initial LED state
digitalWrite(B1Pin, B1State);
digitalWrite(B2Pin, B2State);
digitalWrite(B3Pin, B3State);
digitalWrite(B4Pin, B4State);
}
void loop() {
// read the state of the switch into a local variable:
int T1reading = digitalRead(T1Pin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (T1lastButtonState == HIGH && T1reading == LOW) /* If button is pressed */
press_Time = millis(); /* Save the time in milliseconds using the Millis function */
else if (T1lastButtonState == LOW && T1reading == HIGH) { /* If button is released*/
release_Time = millis(); /* save the time at which the button was released */
long ON_TIME = release_Time - press_Time; /* calculating the time for which the button remained in the LOW state*/
if (ON_TIME > SHORT_TIME) /* comparing the value of time for which the button is pressed to the value for short press time*/
B1State = HIGH;
if (ON_TIME > SHORT_TIME) /* comparing the value of time for which the button is pressed to the value for short press time*/
B2State = B3State = B4State = LOW;
}
if (T1reading != T1lastButtonState) {
// reset the debouncing timer
T1lastDebounceTime = millis();
}
if ((millis() - T1lastDebounceTime) > T1debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (T1reading != T1buttonState) {
T1buttonState = T1reading;
// only toggle the LED if the new button state is HIGH
if (T1buttonState == HIGH) {
B1State = !B1State;
}
}
}
// set the LED:
digitalWrite(B1Pin, B1State);
digitalWrite(B2Pin, B2State);
digitalWrite(B3Pin, B3State);
digitalWrite(B4Pin, B4State);
// save the reading. Next time through the loop, it'll be the lastButtonState:
T1lastButtonState = T1reading;
//------------------------------------------------------------------------------
// read the state of the switch into a local variable:
int T2reading = digitalRead(T2Pin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (T2reading != T2lastButtonState) {
// reset the debouncing timer
T2lastDebounceTime = millis();
}
if ((millis() - T2lastDebounceTime) > T2debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (T2reading != T2buttonState) {
T2buttonState = T2reading;
// only toggle the LED if the new button state is HIGH
if (T2buttonState == HIGH) {
B2State = !B2State;
}
}
}
// set the LED:
digitalWrite(B2Pin, B2State);
// save the reading. Next time through the loop, it'll be the lastButtonState:
T2lastButtonState = T2reading;
//------------------------------------------------------------------------------
// read the state of the switch into a local variable:
int T3reading = digitalRead(T3Pin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (T3reading != T3lastButtonState) {
// reset the debouncing timer
T3lastDebounceTime = millis();
}
if ((millis() - T3lastDebounceTime) > T3debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (T3reading != T3buttonState) {
T3buttonState = T3reading;
// only toggle the LED if the new button state is HIGH
if (T3buttonState == HIGH) {
B3State = !B3State;
}
}
}
// set the LED:
digitalWrite(B3Pin, B3State);
// save the reading. Next time through the loop, it'll be the lastButtonState:
T3lastButtonState = T3reading;
//------------------------------------
// Read the state of the buttons into local variables
int T4reading = digitalRead(T4Pin);
int T5reading = digitalRead(T5Pin);
// Check T4 button for state change and debounce
if (T4reading != T4lastButtonState) {
T4lastDebounceTime = millis();
}
if ((millis() - T4lastDebounceTime) > T4debounceDelay) {
if (T4reading != T4buttonState) {
T4buttonState = T4reading;
if (T4buttonState == HIGH) {
B4State = !B4State;
}
}
}
digitalWrite(B4Pin, B4State);
T4lastButtonState = T4reading;
// Check T5 button for state change and debounce
if (T5reading != T5lastButtonState) {
T5lastDebounceTime = millis();
}
if ((millis() - T5lastDebounceTime) > T5debounceDelay) {
if (T5reading != T5buttonState) {
T5buttonState = T5reading;
if (T5buttonState == HIGH) {
// Check if any of the lights are on
if (B1State || B2State || B3State || B4State) {
// Turn off all lights
B1State = B2State = B3State = B4State = LOW;
} else {
// Turn on B3 if all lights are off
B3State = HIGH;
}
}
}
}
digitalWrite(B4Pin, B4State);
digitalWrite(B1Pin, B1State);
digitalWrite(B2Pin, B2State);
digitalWrite(B3Pin, B3State);
digitalWrite(B4Pin, B4State);
T5lastButtonState = T5reading;
}