Can anyone make me a code like this but with 3 buttons and LEDs?
I have searched and searched but without finding something that has helped me. Have promised to make something to a friend but I have probably promised more than I can bear... ![]()
Can anyone make me a code like this but with 3 buttons and LEDs?
I have searched and searched but without finding something that has helped me. Have promised to make something to a friend but I have probably promised more than I can bear... ![]()
What specifically do you intend to do?
I have probably promised more than I can bear... ![]()
That happens to many of us. ![]()
We can help you write this.
What hardware do you have?
What programming have you done in the past?
.
Oh, thank you ![]()
I have a Uno r3 and a lot of LEDs, buttons, resistors etc. So i should be good to go ![]()
I have just done some basic programming, not much. I have managed to get one LED to turn on/off, but when I tried 2 LEDs, I ended up pulling my hair off...
What I need is that when I press button1, LED1 lights up and stays on until I press button1 again. And when I press button2, LED2 lights up and so on.
But if possible, if I press button1, led1 lights up and led2 and led3 are "low". And if i press button2 is led2 on and led1 and led3 "low" and if button3 is pressed then led3 is on and led1 and led2 off?
Show us a schematic (hand drawn or otherwise) of how you plan to wire the LEDs and switches.
There is an example called "State Change in Detection" in the examples that comes with the IDE.
Edit
You could modify this as needed:
/*
SwitchChangeDetectionSimple.ino
LarryD
Version YY/MM/DD
1.00 16/09/28 Running code
1.01 16/09/30 Added heartbeat LED
Demonstrates switch state change detection, no library.
*/
//**********************************************************************
//+5V----InternalPullupResitor----InputPin----[SwitchPin1/SwitchPin2]----GND
#define Pressed LOW
#define Released HIGH
//+5V----[LEDanode/LEDcathode]----[220ohmResistor]----OutputPin
#define LEDon LOW
#define LEDoff HIGH
//***************************
/*
//**********************************************************************
//+5V----[SwitchPin1/SwitchPin2]----InputPin----[10K resistor]----GND
#define Pressed HIGH
#define Released LOW
//OutputPin----[LEDanode/LEDcathode]----[220ohmResistor]----GND
#define LEDon HIGH
#define LEDoff LOW
//***************************
*/
const byte switchOne = 8;
const byte switchTwo = 9;
const byte LED_One = 11;
const byte LED_Two = 12;
const byte heartBeatLED = 13;
byte switchOneState;
byte lastswitchOneState;
byte lastswitchTwoState;
unsigned long heartBeatMillis;
unsigned long switchMillis;
// s e t u p ( )
//**********************************************************************
void setup()
{
pinMode(switchOne, INPUT_PULLUP);
pinMode(switchTwo, INPUT_PULLUP);
pinMode(heartBeatLED, OUTPUT);
pinMode(LED_One, OUTPUT);
digitalWrite(LED_One, LEDoff);
pinMode(LED_Two, OUTPUT);
digitalWrite(LED_Two, LEDoff);
} //END of s e t u p ( )
// l o o p ( )
//**********************************************************************
void loop()
{
//check for blocking code
heartBeat();
//***************************
//do some loop() stuff
//***************************
//every 50ms check the switches
if (millis() - switchMillis >= 50ul)
{
//reset timing
switchMillis = millis();
//***************
//check switchOne
if (checkSwitch(switchOne, lastswitchOneState) == true)
{
//toggle LED
digitalWrite(LED_One, !digitalRead(LED_One));
}
//***************
//check switchTwo
if (checkSwitch(switchTwo, lastswitchTwoState) == true)
{
//toggle LED
digitalWrite(LED_Two, !digitalRead(LED_Two));
}
//***************
}//END of if (millis() - switchMillis >= 50ul)
//***************************
} //END of l o o p ( )
//======================================================================
// F U N C T I O N S
//======================================================================
// h e a r t B e a t ( )
//**********************************************************************
//optional, check for blocking code
//toggle heartBeatLED every 500ms
void heartBeat()
{
if (millis() - heartBeatMillis < 500ul)
{
//nothing to do yet
return;
}
//reset timing
heartBeatMillis = millis();
//toggle LED
digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
}//END of h e a r t B e a t ( )
// c h e c k S w i t c h ( )
//**********************************************************************
//check if a switch has changed state
bool checkSwitch(byte switchPin, byte &lastState)
{
byte thisSwitchState = digitalRead(switchPin);
//***************************
//was there a change in state?
if (thisSwitchState != lastState)
{
//update the switch state
lastState = thisSwitchState;
//***************************
if (thisSwitchState == Pressed)
{
//this switch was pressed
return true;
}
else
{
//this switch was released
//nothing to do here
}
}//END of if (thisSwitchState != lastState)
//***************************
return false;
}//END of c h e c k S w i t c h ( )
//======================================================================
// E N D O F C O D E
//======================================================================
EDIT
Updated code
.
As mentioned, the sketch in post #4 can be modified do do what you want, give it a try.
.
Oh, great, another unreadable Fritzing.
A few comments.
I see the buttons are paired with a resistor. Wire your buttons between the pin and ground and enable the internal pull-up, then you can lose the external resistor. Note that buttons are active LOW in this case, so logic is reversed.
You'll have to look into debouncing your buttons (either in hardware - with a small capacitor - or in software, a short delay/blackout is probably good enough for you - the sketch in #4 has a 50 millisecond interval which should be good enough), so you don't register multiple presses upon a single press.
Other than that your code should be fairly straightforward. A great beginners project to get to understand the basics.