latching help

hi all i have this code to latch relays but what i need to do is only latch one at a time
ie if relay one is high the you cannot get relay2 high till relay one is low

//Connect a normally open switch between 2 and ground
#define BUTTONPIN 9
#define RELAY 2
#define RELAY2 4
#define BUTTONPIN1 8
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);


const unsigned char PROGMEM BMP_DEMO[] ={
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x03, 0x40, 0x00,
0x00, 0x02, 0x60, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x10, 0x08, 0x00, 0x00, 0x20, 0x04, 0x00, 0x00, 0x60, 0x06, 0x00, 0x00, 0x50, 0x0A, 0x00,
0x00, 0xA8, 0x25, 0x00, 0x01, 0xAA, 0x55, 0x80, 0x01, 0x55, 0xB6, 0x80, 0x02, 0x54, 0x74, 0x40,
0x06, 0x29, 0x94, 0x20, 0x04, 0x21, 0x8C, 0x30, 0x08, 0x11, 0x88, 0x10, 0x18, 0x03, 0xC0, 0x08,
0x10, 0x03, 0xC0, 0x0C, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xE0, 0x3C,
0x05, 0x81, 0x90, 0x10, 0x07, 0x01, 0x80, 0x10, 0x05, 0x08, 0xB2, 0x10, 0x04, 0x80, 0x60, 0x10,
};


unsigned long buttonDebounce = 0;
unsigned long buttonDebounce1 = 0;
byte state = 0;
byte statepower = 0;

void setup() {
  pinMode(RELAY, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(BUTTONPIN1, INPUT);
  pinMode(BUTTONPIN, INPUT);
  digitalWrite(BUTTONPIN, HIGH); // enable internal pullup
  digitalWrite(BUTTONPIN1, HIGH); // enable internal pullup
 Serial.begin(9600);
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  


  // miniature bitmap display
  display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
  display.display();

}

void ToggleState() {
  state = state ^ 1; //XOR 1 toggles LSB, effectively jumping between 0 and 1  
  digitalWrite(RELAY, state);
  
}

void ToggleStatepower() {
  statepower = statepower ^ 1; //XOR 1 toggles LSB, effectively jumping between 0 and 1  
  
  digitalWrite(RELAY2, statepower);
}

void loop() {
//  if (statepower == 1)
  if (digitalRead(BUTTONPIN) == 0 && buttonDebounce == 0) {
    //button pushed, get current time for debouncing
    buttonDebounce = millis();    
  }

  if (digitalRead(BUTTONPIN1) == 0 && buttonDebounce1 == 0) {
    //button pushed, get current time for debouncing
    buttonDebounce1 = millis();
  }
  
  if (digitalRead(BUTTONPIN) == 1 && statepower == 0 ) {
    if ((buttonDebounce != 0) &&  (millis() - buttonDebounce > 20)) {
      ToggleState();
    }; 
    buttonDebounce = 0;
  } 

    if (digitalRead(BUTTONPIN1) == 1 && state == 0) {
     if ((buttonDebounce1 != 0) && (millis() - buttonDebounce1 > 20)) {
      ToggleStatepower();
    }; 
    buttonDebounce1 = 0;
  }
/*
  if (statepower ==1)
 if (state == 1 ) {
  txtslectbc();
 } 
 else 
  txtslectab();


 
 /*if (statepower == 0 && state == 1)
  
 state = 0 ;
 digitalWrite(RELAY, state);
 
 if (statepower == 0)
 txtslectoff();*/
 
}

void txtslectab(void) {
  display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
   display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(40,0);
  display.println("A=B");
  display.display();
}

void txtslectbc(void) {

display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(40,0);
  display.println("B=C");
  display.display();
}
void txtslectoff(void) {
  display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
   display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(40,0);
  display.println("OFF");
  display.display();
}

It looks like you have already tried the solution. Use a variable to hold the state of relay 1 and before activating relay 2 test the state of relay 1 and only activate it if relay 1 is low. By low do you mean on or off ?

Note that relay boards are usually active LOW and are turned on by a LOW input.

yes so i can only have one relay on at a time

so say i have relay1 on it must be turned off before relay2 can be turned on and visa versa

so say i have relay1 on it must be turned off before relay2 can be turned on and visa versa

A state variable for each relay will allow you to do that

Something like this maybe :

if relay1 state is off and button2 becomes pressed
  toggle relay2 state
  write relay2 state to relay2
end if

if relay2 state is off and button1 becomes pressed
  toggle relay1 state
  write relay1 state to relay1
end if

One can use a digitalRead to read the output pin that controls the relay; no need for an extra variable.

sterretje:
One can use a digitalRead to read the output pin that controls the relay; no need for an extra variable.

True, but using a variable with a sensible name makes the code more readable and the program possibly easier to revise and extend should the need arise.

ok i found a good library and got it working
:slight_smile:
so here is the new code. it is also smaller than the original

//Connect a normally open switch between 2 and ground
#include <Button.h>
Button BUTTONPIN1 (8);
Button BUTTONPIN (9);
#define RELAY 2
#define RELAY2 4
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);


const unsigned char PROGMEM BMP_DEMO[] ={
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x03, 0x40, 0x00,
0x00, 0x02, 0x60, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x10, 0x08, 0x00, 0x00, 0x20, 0x04, 0x00, 0x00, 0x60, 0x06, 0x00, 0x00, 0x50, 0x0A, 0x00,
0x00, 0xA8, 0x25, 0x00, 0x01, 0xAA, 0x55, 0x80, 0x01, 0x55, 0xB6, 0x80, 0x02, 0x54, 0x74, 0x40,
0x06, 0x29, 0x94, 0x20, 0x04, 0x21, 0x8C, 0x30, 0x08, 0x11, 0x88, 0x10, 0x18, 0x03, 0xC0, 0x08,
0x10, 0x03, 0xC0, 0x0C, 0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xE0, 0x3C,
0x05, 0x81, 0x90, 0x10, 0x07, 0x01, 0x80, 0x10, 0x05, 0x08, 0xB2, 0x10, 0x04, 0x80, 0x60, 0x10,
};


unsigned long buttonDebounce = 0;
unsigned long buttonDebounce1 = 0;
byte state = 0;
byte stateplus = 0;
byte statepower = 0;

void setup() {
  pinMode(RELAY, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  BUTTONPIN.begin();
  BUTTONPIN1.begin();

 Serial.begin(9600);
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  


  // miniature bitmap display
  display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
  display.display();

}

void ToggleState() {
  state = state ^ 1; //XOR 1 toggles LSB, effectively jumping between 0 and 1  
  digitalWrite(RELAY, state);
  
}
void ToggleStateplus() {
  stateplus = stateplus ^ 1; //XOR 1 toggles LSB, effectively jumping between 0 and 1  
  digitalWrite(RELAY2, stateplus);
  
}
void ToggleStatepower() {
  statepower = statepower ^ 1; //XOR 1 toggles LSB, effectively jumping between 0 and 1  
  
  digitalWrite(RELAY2, statepower);
}

void loop() {

  
  if (BUTTONPIN.pressed() && statepower == 0)
      {ToggleState();
        ToggleStateplus();}
 
      
  if (BUTTONPIN1.pressed() && state == 0)
      ToggleStatepower();
 
    //if (statepower == 1)
 
 
 if (state == 1 ) {
  txtslectbc();
 } 
 
 if (statepower == 1 && state == 0)
  {txtslectab();}

 
 if (statepower == 0 && state == 0)
 {txtslectoff();}
}
void txtslectab(void) {
  display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
   display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(40,0);
  display.println("A=B");
  display.display();
}

void txtslectbc(void) {

display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(40,0);
  display.println("B=C");
  display.display();
}
void txtslectoff(void) {
  display.clearDisplay();
  display.drawBitmap(0, 0, BMP_DEMO,32 ,32 ,1);
   display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(40,0);
  display.println("OFF");
  display.display();
}

i know the coding is not elegant as some may do but i'm no guru :)

Good for you for getting it working, but will you know how and why it works if you look at it again in 6 months ?

It will make no difference to the operation of the program, but can I suggest that you use meaningful names for the variables and/or defines ? What do the buttons do ? BUTTONPIN and BUTTONPIN1 tell us nothing apart from the fact that you did not plan the names ahead of time when testing with just one button.

statepower as a variable name probably indicates its function but not plain state or stateplus. What are those states ? Could you give them meaningful names ?

These suggestions may seem to be a waste of time to implement but will pay dividends in the long term. You have not said it, but people often say that they will improve their program once it works but I bet that most don't.