Hello,
This is my first post on here I have a few projects going on and most likely will be on here quite often for now on. This post I am creating a small box with multiple LEDs and toggle switches coded with a Nano to motivate and reward my toddler son for getting ready in morning and before bed. The issue that is giving me problems is I would like to have one toggle seperate from the rest that when switched will reset all the leds to go off so that when other toggles are switched back it will turn LED back on as to not have to go through each toggle an reset everynight and day. I did write simple code jus to get the switch working and attempting to get it to work with code. I think I may just be brain dead at the moment was trying to make this SUPER difficult all day and only use logic gates and flip flops so been going over alot of data sheets and HOW TOs lol.
heres the code:
// declares LED pin and Switch pin
const int led = 2;
const int swt = 3;
const int restart = 4;
void setup() {
Serial.begin(9600);
// Sets pins mode for output to pwr LED and input_pullup for
// resistor dedicated to switch to stop floating
pinMode(led, OUTPUT);
pinMode(swt, INPUT_PULLUP);
pinMode(restart, INPUT_PULLUP);
}
void loop() {
// reads switch pin for button press
int button = digitalRead(swt);
// reads restart switch to flip inputs starts new day of chores
int flip = digitalRead(restart);
if (button == LOW && flip == LOW) {
// puts LED pin in to low state to turn it off
digitalWrite(led, LOW);
Serial.println("button LOW, restart LOW");
} else if (button == HIGH && flip == LOW) {
// puts LED into high state to turn it on
digitalWrite(led, HIGH);
Serial.println("button HIGH, restart LOW");
} else if (button == HIGH && flip == HIGH) {
Serial.println("button HIGH, restart HIGH");
} else Serial.println("button LOW, restart HIGH");
}
//********************************************^************************************************
// Version YY/MM/DD Description
// 1.00 22/11/03 Running sketch
//
//
//********************************************^************************************************
#define CLOSED LOW
#define OPENED HIGH
#define PUSHED LOW
#define RELEASED HIGH
#define LEDon HIGH
#define LEDoff LOW
#define RELAYon LOW
#define RELAYoff HIGH
#define RUNNING true
#define notRUNNING false
#define ENABLED true
#define DISABLED false
//********************************************^************************************************
const byte switch1 = 2; // +5V---[50k Pullup]---[Input Pin]---[Switch]---GND
const byte switch2 = 3;
const byte switch3 = 4;
const byte switch4 = 5;
const byte switch5 = 6;
const byte resetSwitch = 7;
const byte led1 = 8; // +5V----[220R]----[A->|-K]----GND
const byte led2 = 9;
const byte led3 = 10;
const byte led4 = 11;
const byte led5 = 12;
const byte heartbeatLED = 13; // +5V----[220R]----[A->|-K]----GND
byte lastSwitch1State = OPENED;
byte lastSwitch2State = OPENED;
byte lastSwitch3State = OPENED;
byte lastSwitch4State = OPENED;
byte lastSwitch5State = OPENED;
byte lastResetSwitchState = OPENED;
//****************************
//timing stuff
unsigned long heartbeatTime;
unsigned long switchTime;
const unsigned long toggleInterval = 500ul; //500ms
// s e t u p ( )
//********************************************^************************************************
void setup()
{
Serial.begin(115200);
//******************************
//set pins to inputs with pullup resistor enabled
for (byte x = 2; x <= 7; x++)
{
pinMode(x, INPUT_PULLUP); // +5V---[50k Pullup]---[Input Pin]---[Switch]---GND
}
//******************************
//set pins to outputs
for (byte x = 8; x <= 13; x++)
{
pinMode(x, OUTPUT); //+5V----[220R]----[A->|-K]----GND
digitalWrite(x, LEDoff);
}
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//************************************* heartbeat T I M E R
//to see if the sketch has blocking code,
//toggle the heartbeat LED (every 500ms)
if (millis() - heartbeatTime >= toggleInterval)
{
//restart the TIMER
heartbeatTime = millis();
//toggle the LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//************************************* switch T I M E R
//is it time to check the switches ? (every 50ms)
if (millis() - switchTime >= 50ul)
{
//restart the TIMER
switchTime = millis();
//go and check the switches
checkSwitches();
}
//*********************************
//other non blocking code goes here
//*********************************
} //END of loop()
// c h e c k S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches()
{
byte currentState;
//************************************* s w i t c h 1
//switch1 code
currentState = digitalRead(switch1);
//*****************************
//was there a change in state ?
if (lastSwitch1State != currentState)
{
//update to the new state
lastSwitch1State = currentState;
//*************
//was the switch closed ?
if (currentState == CLOSED)
{
//toggle the LED
digitalWrite(led1, !digitalRead(led1));
}
} //END of this switch
//************************************* s w i t c h 2
//switch2 code
currentState = digitalRead(switch2);
//*****************************
//was there a change in state ?
if (lastSwitch2State != currentState)
{
//update to the new state
lastSwitch2State = currentState;
//*************
//was the switch closed ?
if (currentState == CLOSED)
{
//toggle the LED
digitalWrite(led2, !digitalRead(led2));
}
} //END of this switch
//************************************* s w i t c h 3
//switch3 code
currentState = digitalRead(switch3);
//*****************************
//was there a change in state ?
if (lastSwitch3State != currentState)
{
//update to the new state
lastSwitch3State = currentState;
//*************
//was the switch closed ?
if (currentState == CLOSED)
{
//toggle the LED
digitalWrite(led3, !digitalRead(led3));
}
} //END of this switch
//************************************* s w i t c h 4
//switch4 code
currentState = digitalRead(switch4);
//*****************************
//was there a change in state ?
if (lastSwitch4State != currentState)
{
//update to the new state
lastSwitch4State = currentState;
//*************
//was the switch closed ?
if (currentState == CLOSED)
{
//toggle the LED
digitalWrite(led4, !digitalRead(led4));
}
} //END of this switch
//************************************* s w i t c h 5
//switch5 code
currentState = digitalRead(switch5);
//*****************************
//was there a change in state ?
if (lastSwitch5State != currentState)
{
//update to the new state
lastSwitch5State = currentState;
//*************
//was the switch closed ?
if (currentState == CLOSED)
{
//toggle the LED
digitalWrite(led5, !digitalRead(led5));
}
} //END of this switch
//************************************* r e s e t S w i t c h
//resetSwitch code
currentState = digitalRead(resetSwitch);
//*****************************
//was there a change in state ?
if (lastResetSwitchState != currentState)
{
//update to the new state
lastResetSwitchState = currentState;
//*************
//was the switch closed ?
if (currentState == CLOSED)
{
//turn off all LEDs
digitalWrite(led1, LEDoff);
digitalWrite(led2, LEDoff);
digitalWrite(led3, LEDoff);
digitalWrite(led4, LEDoff);
digitalWrite(led5, LEDoff);
}
} //END of this switch
} //END of checkSwitches()
My summary is derived from your sketch:
You have a combination of n toggle switches and Leds.
Each toggle switch can light up the corresponding LED.
In addition to this combination, a toggle switch can switch off all Leds again.
Is this assumption correct?
If so, create an object for a toggle switch and LED combination. This object contains the data about the connection pins used. With a strucured array you can easily extend this to n combinations. A service will take this information to perform an action to light up the LED in this way. Next to this service, the "reset" toggle switch is also handled to clear all LEDs.