How do I make the LEDs stay on after pressing a switch? then stay off when the switch is pressed succeeding the last

How do I make the LEDs stay and continue in a loop after I press the switch once, and turn off when the switch is pressed again? This is the code I have so far but it only allows the LEDs to light up when I hold onto pressing the switch and turn back off immediately after I release it. If anybody knows how to help going from here that would be great, thank you ^-^

int button1 = 2; //tact switch connected to digital pin 2
int LED = 4; //blue LED
int button2 = 7; //tact switch connected to digital pin 7
int LED2 = 9; //yellow LED

void setup() {
  pinMode(2, INPUT);
  pinMode(4, OUTPUT);
  pinMode(7, INPUT);
  pinMode(9, OUTPUT);
  
}

void loop() {
  if (digitalRead (button1)==HIGH) {
    digitalWrite (LED, HIGH);
  }
  else {
    digitalWrite (LED, LOW);
  }
{
  if (digitalRead (button2)==HIGH) {
    digitalWrite (LED2, HIGH);
  }
  else {
    digitalWrite (LED2, LOW);
  }
}
}

edit: I fixed the variables for the tact switch

bool tick = true;

not tested.

You need to detect when the button becomes pressed rather than when it is pressed

See the StateChangeDetection example in the IDE

Hello
Your sketch will not compile.

p.s. Do you have coding experience in C++.

Your code won't even compile. "switch" is a statement (switch..case) and can't be a variable.

Hello
Try this sketch to study digital.Read() and digital.Write() .
I´m assuming the switches are connected correct.

  int switch1 = 2; //tact switch connected to digital pin 2  
  int LED1 = 4; //blue LED
  int switch2 = 7;//tact switch connected to digital pin 7 
  int LED2 = 9; //yellow LED

void setup() {
  pinMode(switch1 , INPUT);
  pinMode(LED1 , OUTPUT);
  pinMode(switch2, INPUT);
  pinMode(LED2, OUTPUT);
}
void loop() {
  digitalWrite (LED1, digitalRead (switch1));
  digitalWrite (LED2, digitalRead (switch2));
}

Have a nice day and enjoy coding in C++.
p.s. This is a nice task to start with OOP.

1 Like
// This a simple OOP example to toggle LEDs, keep in mind to connection of the hardware 
// The sketch is tested using my test enviromnet and the initialization may has to be changed

// declare an object 
struct LEDBUTTON {
  byte switchPin;               // [port pin] ----- [switch] ------ [gnd]
  byte ledPin;                  // [port pin] -----  [led]   ------ [gnd]
  byte counter;                 // counter to toggle led
  int statusQuo;                // old switch state
};
// initialize an array using the object declaration
LEDBUTTON ledButtons[] { // add switch and led combinatios as you like simply 
  {A0, 3, 0,false},
  {A1, 5, 0,false},
  {A2, 6, 0,false},
  {A3, 7, 0,false},
};

void setup() {
  // the "range based for loop" will save typing work :) 
  for (auto &ledButton : ledButtons) {
    pinMode (ledButton.switchPin, INPUT_PULLUP);
    pinMode (ledButton.ledPin, OUTPUT);
  }
}
void loop() {
  delay (20);                 // dirty debouncing
   // the "range based for loop" will save typing work :) 
  for (auto &ledButton : ledButtons) { 
    // read button state 
    int stateNew = !digitalRead(ledButton.switchPin);
    // any changes ?
    if (ledButton.statusQuo != stateNew) {
      // save state 
      ledButton.statusQuo = stateNew;
      // if button is pressed, than toogle led on/off vs off/on
      if (stateNew) digitalWrite(ledButton.ledPin, ++ledButton.counter & 1);
    }
  }
}
1 Like

Ah yeah i've noticed that just now. As for C++, I've one heard of it and used it only a couple of times before but under instructions and tutorials.

Thank you, I'll be sure to keep those in mind! :DD

Hello
Many thanks for your reply.
You may use the sketch as starter for the coding in C++.

Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.