One relay multiple Buttons

Hi All

I have only just started to learn the Arduino 3 days ago and I am kind of struggling with task I have set myself.

Using an Arduino Leonardo I have 1 relay and 3 buttons to control the relay, I can get the buttons to work intermittently for the require times but the button presses do not always work, I am unsure as to how to use the if, else statements in the code, I have had a good look at many examples and I am sorry to say I have thoroughly confused myself :o

Any Help, pointers would be greatly appreciated.

int buttonPin(2);
int buttonPin2(4);
int buttonPin3(5);                    
int buttonPress=0;
int buttonPress2=0;
int buttonPress3=0;           
int RelayPin(3);


void setup()
  {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP); 
  pinMode(buttonPin3, INPUT_PULLUP);  
  pinMode(RelayPin, OUTPUT);
  Serial.begin(9600);
  }
  

void loop()
  {
  buttonPress = digitalRead(buttonPin); 
  if(buttonPress == 1){
  delay (50);
  buttonPress = digitalRead(buttonPin);
  if(buttonPress == 0){
  digitalWrite(RelayPin, HIGH);
  delay (1500);
  digitalWrite(RelayPin, LOW);
  }}
  else{
  delay(100);
  }

  {    
  buttonPress2 = digitalRead(buttonPin2); 
  if(buttonPress2 == 1){
  delay (50);
  buttonPress2 = digitalRead(buttonPin2);
  if(buttonPress2 == 0){
  digitalWrite(RelayPin, HIGH);
  delay (500);
  digitalWrite(RelayPin, LOW);
  }}
  else{
  delay(100);
  }}

  {    
  buttonPress3 = digitalRead(buttonPin3);
  if(buttonPress3 == 1){
  delay (50);
  buttonPress3 = digitalRead(buttonPin3);
  if(buttonPress3 == 0){
  digitalWrite(RelayPin, HIGH);
  delay (1000);
  digitalWrite(RelayPin, LOW);
  }}  
  else{ 
  delay(100);
  }}
  }

Welcome to the forums. +1 karma for using code tags on your first post.

Your initialization code

int buttonPin(2);
int buttonPin2(4);
int buttonPin3(5);
int buttonPress = 0;
int buttonPress2 = 0;
int buttonPress3 = 0;
int RelayPin(3);

while technically correct is not very commonly used. Usually you just use assignment (with the const keyword since these are constants that will not change during the execution of the program)

const int buttonPin=2;
const int buttonPin2=4;
const int buttonPin3=5;
int buttonPress = 0;
int buttonPress2 = 0;
int buttonPress3 = 0;
const int RelayPin=3;

As for why your buttons don't always respond, it is because of all those delay()s in your code. The code is blocking during that time so nothing else can happen. Usually a bad technique. Look at the Blink Without Delay example (File->examples->02.Digital->BlinkWithoutDelay) to see how to get around this. Also check out the State Change Detection example.

Thank you for such prompt help it is greatly appreciated.

I shall review the blink without delay and adjust as required

Every days a school day :slight_smile:

Thank you

Suggest you use CTRL T or CMD T to format your sketches.


Also suggest you study the “State Change Detection (Edge Detection)” example rather than using switch levels.

See this thread: For beginners: The simple way to program for multiple buttons [code example] - Introductory Tutorials - Arduino Forum