Simple

I am not completely new to arduino but I am pretty terrible at coding. I just started a project with my kid to control a relay when a button is pushed. Simple right? I can’t seem to figure out what I’m doing wrong.

I’m using an arduino uno and a I have pin 2 set as an output and it is wired to a trigger on a 2 relay board. Ground and 5v from the arduino to the ground and vcc pin on on the relay board. To start I wanted to have the arduino trigger the relay from a push button and hold it on for 4 sec. After that I was going to add functions but one step at a time.

I wired it up and cannot figure what I cant get it to work. I have been able to get the arduino to trigger the relay in code but when I try to do it by reading an input i cant seem to get it to do what I want. I have tried it using both digital pins. On the digital pins have tried first pulling the pin to ground and tried presenting it with a 5v signal.

Can someone please have a look at this code and see if I’m just overlooking something obvious?

Thanks.

/* Light Control v1.0
I hate Rice Cakes */


const int Relay1 = 2;          // Set light "relay 1" to Arduino digital pin2
const int WallSwitch = 0;     // Set WallSwitch  To Arduino Pin 0

void setup() {

  pinMode(Relay1, OUTPUT);       // Set Arduino Pin 2 as an output
  pinMode(WallSwitch, INPUT);    // Set WallSwitch (arduino pin 0) to input

  digitalWrite(Relay1, HIGH);      //set relay pin High


}

void loop() {


  digitalRead(WallSwitch);    //read the state of the wall switch input

  if (WallSwitch == LOW) { 
    digitalWrite(Relay1, LOW);    // If switch pin pulled low, trigger relay
    delay (4000);
  }

  digitalWrite (Relay1, HIGH);    //Turn Relay Off

}

Micr0:

  digitalRead(WallSwitch);    //read the state of the wall switch input

if (WallSwitch == LOW) {

Please read:
https://www.arduino.cc/en/Reference/DigitalRead
File > Examples > 01.Basics > DigitalReadSerial

File > Examples > 02.Digital > Button

I think those resources will make it clear what's wrong with the above code.

Micr0:

const int WallSwitch = 0;     // Set WallSwitch  To Arduino Pin 0

Generally it's a bad idea to use pins 0 and 1 because they're also used for serial communication (Serial Monitor and uploading sketches to your Arduino). If you have other pins free use those instead. It's not to say you can never use those pins but at this point the less extra complications the better.

Micr0:
On the digital pins have tried first pulling the pin to ground and tried presenting it with a 5v signal.

I first took that to mean that you're using an external pull-up or pull-down resistor but now I'm not sure. If you aren't using one then you definitely should check out these resources:
File > Examples > 02.Digital > DigitalInputPullup

https://www.arduino.cc/en/Reference/PinMode

 digitalRead(WallSwitch);    //read the state of the wall switch input

Reads the state of the wall switch input and then throws it away.

    delay (4000);

Wastes the considerable power of an Arduino Uno for four seconds.

vaj4088:

 digitalRead(WallSwitch);    //read the state of the wall switch input

Reads the state of the wall switch input and then throws it away.

    delay (4000);

Wastes the considerable power of an Arduino Uno for four seconds.

oops that was old. I had the value from that digital read placed in a variable called WallSwitchState

WallSwitchState = digitalRead....

I know I should be using millis(). I figured I'd learn how to implement that function once I got the hardware working.

pert:
I first took that to mean that you're using an external pull-up or pull-down resistor but now I'm not sure. If you aren't using one then you definitely should check out these resources:
File > Examples > 02.Digital > DigitalInputPullup
https://www.arduino.cc/en/Tutorial/InputPullupSerial
pinMode() - Arduino Reference

Thanks

I should have been more clear. I have wired it with a 10k resistor to 5v and a switch that shunts it ground (pull up) and visa versa.

I'll go over those links tonight. I have done all this from memory which I think is problem number 1.

Micr0:
Can someone please have a look at this code and see if I'm just overlooking something obvious?

Which model/type of relay do you have?

Do you have a 'naked' relay', or is it a "relay module" on a PCB with some small additional components soldered on it?

If it is a module on a PCB: Is it an "active LOW" or "active HIGH" switching type relay module?

Micr0:
I should have been more clear. I have wired it with a 10k resistor to 5v and a switch that shunts it ground (pull up) and visa versa.

Ok, that was actually how I read it the first time but then I thought maybe you just meant that you had only connected the button between 5 V or GND. So it's not necessary to use the internal pull-up resistor since you have an external resistor. Many people don't realize they need the resistor and then don't understand why they are getting phantom readings from the floating pin. In the examples Arduino seems to have taken the approach that the concept of an external pull-down resistor should first be introduced, I suppose so that the idea is easier to grasp and so that the expected logic of a button press giving a HIGH instead of a LOW reading can be achieved. I'm really not sure which is better. Certainly the circuit is more simple if you use the internal pull-up resistor. Well either way I think it will be easy for them to get it with a little explanation.

Micr0:
I know I should be using millis(). I figured I'd learn how to implement that function once I got the hardware working.

I think using delay is acceptable to keep the code very simple and intuitive beginner. You just can't continue the practice into more advanced lessons. I could see a progression from a "here's Blink" to "this is what's wrong with Blink and now I'll show you how to write the code non-blocking using millis()".

jurs:
Which model/type of relay do you have?

Do you have a 'naked' relay', or is it a "relay module" on a PCB with some small additional components soldered on it?

If it is a module on a PCB: Is it an "active LOW" or "active HIGH" switching type relay module?

It's a simple 2 relay board, Active low.

http://tinkersphere.com/power/1247-2-channel-relay-module.html]2 relay module[/url]