Button start value?

Hi

If I start the program both relays are ON, but I want that one of them are ON when I start the program. The buttons themselves are working as I want to.

I can somebody tell what to add to the code? Starting values?

My code ise here:

int Relay1 = 5;
int Relay2 = 6;
int buttonApin = 9;
int buttonBpin = 8;
 
byte leds = 0;
 
void setup() 
{
  
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  
}
 
void loop() 

{ 
  
  if (digitalRead(buttonApin) == LOW)
  {
    digitalWrite(Relay1, HIGH);
    digitalWrite(Relay2, LOW);
  }
  if (digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(Relay1, LOW);
    digitalWrite(Relay2, HIGH);
  }
}

Sorry for my English, it's not my first language.

You need to send one of the relays high in the Setup before you start your loop

int Relay1 = 5;
int Relay2 = 6;
int buttonApin = 9;
int buttonBpin = 8;

byte leds = 0;

void setup()
{

pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
digitalWrite(Relay1, HIGH);
}

void loop()

{

if (digitalRead(buttonApin) == LOW)
{
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, LOW);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, HIGH);
}
}

haydensmart:
You need to send one of the relays high in the Setup before you start your loop

right, but more likely, LOW since the relay pins are pulled up... I'm assuming "Active Low"

void setup()
{

pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
digitalWrite(Relay1, LOW); //<<<<<<<<<<<<
}

Ok. So the code compiles but it is not doing what you want to do.

In your code, you declare the byte data type(8 bits) variable leds = 0; , but you never actually use it.

Also, you are making it a lot more complicated than what is needed. Your current code is just controlling two outputs(5 and 6) with 2 inputs(8 and 9)

Should it be else if() for the second? or does that work?

Finally, you might want to add some delay() s in the if statements.

By the way, for a second language, your english is great!! :slight_smile:

Ethan :slight_smile: :slight_smile: :slight_smile:

Why is it that only the OP, with his first ever post on these forums, used code tags, and everyone else with more experience posted it inline? Setting a bad example.

haydensmart:
You need to send one of the relays high in the Setup before you start your loop

Ok it works now! Thank you all :slight_smile:

BulldogLowell:
right, but more likely, LOW since the relay pins are pulled up... I'm assuming "Active Low"

It doesn't seem to work in LOW.

EQOxx123:
Ok. So the code compiles but it is not doing what you want to do.

In your code, you declare the byte data type(8 bits) variable leds = 0; , but you never actually use it.

Also, you are making it a lot more complicated than what is needed. Your current code is just controlling two outputs(5 and 6) with 2 inputs(8 and 9)

Should it be else if() for the second? or does that work?

Finally, you might want to add some delay() s in the if statements.

By the way, for a second language, your english is great!! :slight_smile:

Ethan :slight_smile: :slight_smile: :slight_smile:

Yes I now deleted the variable leds = 0; and I changed the second to else if(). Thanks

FiveO:
Ok it works now! Thank you all :slight_smile:

Wish they were all like this.

Ikr

PaulMurrayCbr:
Wish they were all like this.