Momentary switch latching

im haveing trouble with another code, ive found one which works for a single latching momentary switch ive changed the code around abit and added the internal pull up resistors etc and "actualy" managed to make it load but the leds are staying on the whole time, both codes work "separately" but added togther they seem to stay on
im trying to make a momentary button switch press once for on then again for off, im useing a nano atm with 9 and 10 going to two via resistors LEDS and A5 and A3 going to 0v through a button switch
here the code

const int buttonPin1 = A5; // Pin where the momentary switch is connected
const int ledPin2 = 10; // Pin where the ouput is connected
const int buttonPin = A3; // Pin where the momentary switch is connected
const int ledPin = 9; // Pin where the ouput is connected

int button1State = 0; // Variable for reading the button status
int lastButton1State = 0; // Variable for storing the last button state
int led1State = LOW; // Variable for storing the LED state

int buttonState = 0; // Variable for reading the button status
int lastButtonState = 0; // Variable for storing the last button state
int ledState = LOW; // Variable for storing the LED state

void setup() {
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}

void loop() {
button1State = digitalRead(buttonPin1);
if (button1State != lastButton1State) {
if (button1State == HIGH) {
led1State = !led1State;
digitalWrite(ledPin2, led1State);

buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
delay(50); // Debounce delay
}
lastButton1State = button1State;
lastButtonState = buttonState;
}
}
}

Try this instead(compiles, untested):

const int buttonPin1 = A5; // Pin where the momentary switch is connected
const int ledPin1 = 10; // Pin where the ouput is connected
const int buttonPin = A3; // Pin where the momentary switch is connected
const int ledPin = 9; // Pin where the ouput is connected

int button1State = 0; // Variable for reading the button status
int lastButton1State = 0; // Variable for storing the last button state
int led1State = LOW; // Variable for storing the LED state

int buttonState = 0; // Variable for reading the button status
int lastButtonState = 0; // Variable for storing the last button state
int ledState = LOW; // Variable for storing the LED state

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  button1State = digitalRead(buttonPin1);
  if (button1State != lastButton1State) {
    if (button1State == HIGH) {
      led1State = !led1State;
      digitalWrite(ledPin1, led1State);
    }
    lastButton1State = button1State;
  }
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
    }
    lastButtonState = buttonState;
  }
  delay(50); // Debounce delay
}

Notice the

  • formatting changes (use of autoformat makes your structure errors obvious)
  • variable name changes to enforce consistency
  • restructured to do things only when needed
  • Note how obvious the repeated code becomes…
1 Like

Try this mod, compiles but untested.

const int buttonPin1 = A5; // Pin where the momentary switch is connected
const int ledPin2 = 10; // Pin where the ouput is connected
const int buttonPin = A3; // Pin where the momentary switch is connected
const int ledPin = 9; // Pin where the ouput is connected

int button1State = 0; // Variable for reading the button status
int lastButton1State = 0; // Variable for storing the last button state
int led1State = LOW; // Variable for storing the LED state

int buttonState = 0; // Variable for reading the button status
int lastButtonState = 0; // Variable for storing the last button state
int ledState = LOW; // Variable for storing the LED state

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  button1State = digitalRead(buttonPin1);
  if (button1State != lastButton1State) {
    lastButton1State = button1State;
    delay(50); // Debounce delay
    if (button1State == HIGH) {
      led1State = !led1State;
      digitalWrite(ledPin2, led1State);
    }
  }  
      buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState) {
        lastButtonState = buttonState;
        delay(50); // Debounce delay
        if (buttonState == HIGH) {
          ledState = !ledState;
          digitalWrite(ledPin, ledState);
        }
      }
}

[quote="camsysca, post:3, topic:1419213"]

const int buttonPin1 = A5; // Pin where the momentary switch is connected
const int ledPin1 = 10; // Pin where the ouput is connected
const int buttonPin = A3; // Pin where the momentary switch is connected
const int ledPin = 9; // Pin where the ouput is connected

int button1State = 0; // Variable for reading the button status
int lastButton1State = 0; // Variable for storing the last button state
int led1State = LOW; // Variable for storing the LED state

int buttonState = 0; // Variable for reading the button status
int lastButtonState = 0; // Variable for storing the last button state
int ledState = LOW; // Variable for storing the LED state

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  button1State = digitalRead(buttonPin1);
  if (button1State != lastButton1State) {
    if (button1State == HIGH) {
      led1State = !led1State;
      digitalWrite(ledPin1, led1State);
    }
    lastButton1State = button1State;
  }
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
    }
    lastButtonState = buttonState;
  }
  delay(50); // Debounce delay
}

[/quote]ahh dude thats amazing thanks ive been trying things for hours now,
what did you do? im still learning and this is my like second code
its like 1am here so ill loook closer in the moring

Vely crever. :grin:

2 Likes

And, since I hate repetitive code(untested),

const int buttonPin1 = A5; // Pin where the momentary switch is connected
const int ledPin1 = 10; // Pin where the ouput is connected
const int buttonPin = A3; // Pin where the momentary switch is connected
const int ledPin = 9; // Pin where the ouput is connected

bool lastButton1State = 0; // Variable for storing the last button state
bool lastButtonState = 0; // Variable for storing the last button state

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  lastButton1State = doit(buttonPin1, ledPin1, lastButton1State);
  lastButtonState = doit(buttonPin, ledPin, lastButtonState);
  delay(50); // Debounce delay
}

bool doit(int buttonP, int LEDP, bool lastState) {
  bool state = digitalRead(buttonP);
  if (state != lastState) {
    if (state == HIGH) digitalWrite(LEDP, !digitalRead(LEDP));
  }
  return state;
}

And finally, a version where all you need to do to change the number of button:LED pairs is to add lines to the array:

struct PAIR {  //declare a structure of buttons and related pins
  int button;
  int LED;
};

PAIR pairs[] = { //create an array of PAIR structs
  {A5, 10},//clone and edit lines as required
  {A3, 9},
};
const int NumPairs = sizeof(pairs) / sizeof(PAIR);  //create a constant for the item count
bool lastState[NumPairs];                           //track last state of each button

void setup() {
  for (int i = 0; i < NumPairs; i++) {
    pinMode(pairs[i].button, INPUT_PULLUP);
    pinMode(pairs[i].LED, OUTPUT);
    lastState[i] = 0;
  }
}

void loop() {
  for (int i = 0; i < NumPairs; i++)  lastState[i] = doit(pairs[i].button, pairs[i].LED, lastState[i]);
  delay(50); // Debounce delay
}

bool doit(int buttonP, int LEDP, bool lastState) {
 bool state = digitalRead(buttonP);
  if (state != lastState && state == HIGH) digitalWrite(LEDP, !digitalRead(LEDP));
  return state;
}

The advantage of this last code is in the simplicity it provides for growth. Sure, it’s a super-simplistic example, but therein lies the gold - without a lot of cruft, the three codes I’ve provided supply the OP with a mental-model-expansion, from

  • from “clone and edit” code that’s always prone to errors in content and structure
  • to modular, function-call streamlining to reduce repetition and improve clarity
  • to loop-based data-driven structure allowing effortless growth with minimal error risk

@minidog1 I hope this provides you with inspiration and food for thought, not just a solution.

You can even test your code (or anyone else's) on a simulator. Here is your setup but you can modify it:

1 Like