Controlling relay with pushButton

Hi,

I'm a newbie for C++ programing. I have attached a program which gives me an errors and have no idea how to solve it.
I want to activate relay with when pushbutton is on and deactivate when is off (or vice versus). I do appreciate a help.

Thank you

int buttonPin = 5;
int relay = 3;

void setup(){
Serial.begin(9600);
pinMode(buttonPin, INPUT);

pinMode(relay, OUTPUT);
}

void loop(){
boolean buttonState = digitalRead(buttonPin);

Serial.print("The button state = ");
Serial. println(buttonState);

if (buttonState == 0);{digitalWrite(relay, HIGH);}

if (buttonState == 1); {digitalWrite(relay, LOW);
}

 if (buttonState == 0);{
 if (buttonState == 1);{

Lose the semicolons

Hint: if a boolean isn't false, there's no need to test it again to see if it is true.

I did remove the semicolons but still getting error. Here is a copied message:

Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Daumantas\Documents\Arduino\Tutorial01\Button_relay\Button_relay.ino: In function 'void loop()':

Button_relay:20: error: expected '}' at end of input

}

^

exit status 1
expected '}' at end of input

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

The error message is pretty clear; the compiler has reached the end of your code, expecting to find a closing brace "}", but couldn't.

So why not give it a closing brace, and see what it says then?

Thank you so much. There was two closing braces need. One for "void loop" and one for "if". Now it works but when I releasing the button, relay doesn't come back on straight away. It pulsing first and the comes on. Slow response is well. Can I change something to improve it?

Thank you

How is your switch wired?

This Is my hardware:

A drawing or schematic would be easier to read, but I'm going to guess you have a floating input.

Here is a picture. Is there a free software to for drawing to do?

I can’t attach jpeg or doc files. They are far less than 1Mb. Maybe newbies can’t attach files?

Your sketch doesn't use a built-in pull up; does your hardware have a pull up or pull-down?
I'm not wasting mobile bandwidth watching a video.

Sorry I never thought about it. No mine hardware doesn't use any pull up/down. How I can introduce a build-in pull up in my sketch?

Check the documentation for pinMode

Tecline:
Thank you so much. There was two closing braces need. One for "void loop" and one for "if". Now it works but when I releasing the button, relay doesn't come back on straight away. It pulsing first and the comes on. Slow response is well. Can I change something to improve it?

Thank you

When you press or release a button contacts will bounce a few times. Arduino will catch these on/off transitions. I guess your relay is too slow to react when you press the button, but still you should add code to prevent it. Have a look at inbuilt examples, chapter 02.Digital, lesson Debounce.
Also next lesson might be good for your project. DigitalInputPullup.

I might be mistaken but it looks like you have connected both IN1 and GND on the relay module to ground on your breadboard, and you are driving VCC from your UNO output.

AWOL:
Check the documentation for pinMode

Thank you. This is sorted. I change feed of pushbutton from 5v to GND and sketch is a follow:

int buttonPin = 5;
int relay = 3;

void setup(){
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);

pinMode(relay, OUTPUT);
}

void loop(){
int sensorVal = digitalRead(buttonPin);

Serial.println(sensorVal);

if(sensorVal ==HIGH){
digitalWrite(relay, LOW);
}
else{
digitalWrite(relay, HIGH);}

}

Gabriel_swe:
When you press or release a button contacts will bounce a few times. Arduino will catch these on/off transitions. I guess your relay is too slow to react when you press the button, but still you should add code to prevent it. Have a look at inbuilt examples, chapter 02.Digital, lesson Debounce.
Also next lesson might be good for your project. DigitalInputPullup.

I might be mistaken but it looks like you have connected both IN1 and GND on the relay module to ground on your breadboard, and you are driving VCC from your UNO output.

Ok. I have changed wiring is well. Relay is connected to VCC and GND, IN1 is connected to pin 3, pushbutton joins GND and pin 5. Have I done it right way?

Good job.

you still have the debouncing issue probably - meaning that when you press your button your arduino will see a succession of HIGH and LOW which will be sent in a rapid sequence to your relay - well not so rapid as you do Serial.println(sensorVal); at 9600 bauds so might be why you hear a series of pulses into your relay. it is like sending current ON and OFF at a fast pace to whatever is after the relay... might not be too good for what's connected.

so what you want to do is check for stabilization of the input before sending the command to the relay - study the code linked above for ideas