Thank you for the respond ![]()
At the moment my code looks like this (I'm trying to light up one RGB [doesn't matter which colour] by pressing single button - but it works like I wrote above - LED flashes all the time and the button turns it off (if u let go the button RGB turns on); I want achieve a situation where button turns on RGB and stay like this until the button will be pressed again and turns RGB off.
int ledPin = 12; // LED is connected to digital pin 13
int switchPin = 3; // switch connected to digital pin 2
int switchValue; // a variable to keep track of when switch is pressed
void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
pinMode(switchPin, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin, HIGH); // sets the default (unpressed) state of switchPin to HIGH
}
void loop() // run over and over again
{
switchValue = digitalRead(switchPin); // check to see if the switch is pressed
if (switchValue == LOW) { // if the switch is pressed then,
digitalWrite(ledPin, HIGH); // turn the LED on
}
else { // otherwise,
digitalWrite(ledPin, LOW); // turn the LED off
}
}
I thought about this code but at the end the button doesn't respond - RGB is on all the time:
//Button Toggle LED
int ledPin = 12;
int buttonPin = 3;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
boolean ledState = LOW;//storage for the current state of the LED (off/on)
void setup(){
pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
pinMode(ledPin, OUTPUT);
Serial.begin(9600);//initialize Serial connection
}
void loop(){
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
//toggle the state of the LED
if (ledState == HIGH){
digitalWrite(ledState, LOW);
ledState = LOW;
} else {
digitalWrite(ledState, HIGH);
ledState = HIGH;
}
}
lastState = currentState;
}