Push button turn's on relay and a led

hi im trying to make a code to do the following
i press a push button- turn on a led and send a signal to a relay 1 seg (auto turn off) when i press the push button again the led turns off and send a signal to the relay 1seg (auto turn off)
the led part i managed to find some code in the interwebs and figure out to make it work but now i can´t figure out a way to make the relay part to work
there is my code if someone can help
<
const byte buttonPin = 4;
const byte ledPin = 13;
const byte relay = 5;

bool buttonState = 0;
bool lastButtonState = 0;
bool relayState = 0;
void setup()
{

pinMode(buttonPin, INPUT_PULLUP);

pinMode(ledPin, OUTPUT);

pinMode(relay,OUTPUT);

Serial.begin(9600);
}

void loop()
{
if (buttonState = 1);
{digitalWrite (relay=1);}
delay (5);
{digitalWrite (relay=0);}
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();

  buttonState = digitalRead(buttonPin);
 
  if (buttonState != lastButtonState)
  {
     if (buttonState == LOW)
     {
       
        digitalWrite(ledPin, !digitalRead(ledPin)); 
     }
  }
 
  lastButtonState = buttonState;

}
}
/>

there shouldn't be a semicolon on the line, or other lines like this

where does timer get set?

im new to this hobby :smiley: trying to figure out what should i do then?

If something "shouldn't be there" what should you do?

If your finger is stuck in your eye, but shouldn't be stuck in your eye, what should you do?

1 Like

And you prolly want ==, test for equality, not =, assignment.

Try
if(buttonState == 1)

“=“ changes the value of a variable
“==“ compares two expressions

Also, study the syntax of digitalWrite. This is wrong. Lookup examples. Do your homework.

First, you have to read the the buttonPin:

buttonState = digitalRead(buttonPin);
if (buttonState == 1)

updated the code with your help but now the led pin is always high and the button does not do anything (i connected a led to the relay pin (for test proposes and this one does not do anything)
const byte buttonPin = 4;
const byte ledPin = 13;
const byte relay = 5;

bool buttonState = 0;
bool lastButtonState = 0;
bool relayState = 0;
void setup()
{

pinMode(buttonPin, INPUT_PULLUP);

pinMode(ledPin, OUTPUT);

pinMode(5, OUTPUT);

Serial.begin(9600);
}

void loop()
{buttonState = digitalRead(buttonPin);
if (buttonState == 1);
digitalWrite (5,HIGH);
delay (3000);
digitalWrite (5,LOW);
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();

  buttonState = digitalRead(buttonPin);
 
  if (buttonState != lastButtonState)
  {
     if (buttonState == LOW)
     {
       
        digitalWrite(ledPin, !digitalRead(ledPin)); 
     }
  }
 
  lastButtonState = buttonState;

}
}

if (buttonState = 0) // no semicolon, ";"

if | Arduino Documentation

still have a semicolon

what is the purpose of this timer code?
why does checking for a button strate change and press need to be in a timer?

looks like you want the timer to toggle an LED, only while a button is being pressed or when a button is pressed?

fixed it and now is working
basically i press a push button turns on and off a relay and turns on a led if i press again turns on and off a relay and turn the led off
const byte buttonPin = 4;
const byte ledPin = 13;
const byte relay = 5;

bool buttonState = 0;
bool lastButtonState = 0;
bool relayState = 0;
void setup()
{

pinMode(buttonPin, INPUT_PULLUP);

pinMode(ledPin, OUTPUT);

pinMode(5, OUTPUT);

Serial.begin(9600);
}

void loop()
{buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
digitalWrite (5,HIGH);
delay (200);
digitalWrite (5,LOW);
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();

  buttonState = digitalRead(buttonPin);
 
  if (buttonState != lastButtonState)
  {
     if (buttonState == LOW)
     {
       
        digitalWrite(ledPin, !digitalRead(ledPin)); 
     }
  }
 
  lastButtonState = buttonState;

}
}

const byte buttonPin = 4; // button wired between pin 4 and GND
const byte ledPin = 13;
const byte relay = 5;
bool buttonState = true;
bool lastButtonState = true;
bool relayState = false;
void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
}

void loop()
{ 
  buttonState = digitalRead(buttonPin);
  if (buttonState == false)
  {
    digitalWrite (5, HIGH);
  }
  delay (200);
  digitalWrite (5, LOW);
  static unsigned long timer = 0;
  unsigned long interval = 50;
  if (millis() - timer >= interval)
  {
    timer = millis();
    buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState)
    {
      if (buttonState == false)
     {
       digitalWrite(ledPin, !digitalRead(ledPin));
     }
    }
  lastButtonState = buttonState;
  }
}

I just noticed you posted the code by putting <, then pasting your code, then finishing with />.

Very creative, but that is not what the <CODE/> thing in the message composition window means, that's actually a button and when you press it, you'll see it types for you

```
type or paste code here
```

so do that, leave the backticks and paste your code over "type or paste code here", then it will look like code. @JCA34F did it for you, see how nice it looks?

And beyond looking nice, it won't get mangled. All the characters you put in there will be taken literally, so they don't end up being interpreted as formatting commands.

Next time. :expressionless:

a7

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