How hard can it be to use an Arduino to control two relays using two momentary switches?

This is a problem which I have been trying to solve for literally months now! I am using an Arduino Nano (V3.0) to operate two non latching relays by the push of a button. Switch A will turn on Relay A at the first push, it will then turn it off at the second push. Switch B does the same to Relay B, hopefully.
So far, I have been using the 'debounce' example to control one set - Switch A and Relay A. This works perfectly, however I have issues when I try and make a sketch which will enable both A and B to work together at the same time but independently. After spending hours researching and looking through the Arduino forums, I know it can be done, but most likely not with this sketch due to the 'delay' in there? I read about the 'millis' command somewhere too, but I'm baffled by it! Obviously, I still have A LOT to learn about Arduinos. 
The circuit is wired up and ready, I could just really do with a bit (*lot! ;) ) oh help with the programming, as it seems like it needs to be completely re-written :( 
Last thing - this is part of my A-level Product Design project, and my deadline is next week, so if this approach cannot be resolved, I'll just have to buy a second Nano and use both, which seems a waste of two pretty powerful microelectronics! 
Thanks guys!

sort by: active | newest | oldest

The Vin pin on a Nano WILL accept 12V

Be aware that some chinese copy Nano will NOT take 12V on the Vin pin despite being sold that they will. Some of the AMS1117 5V regulators the chinese makers use will fail if more than 10V is applied. I learnt this the hard way!

I like to use tracking variables. Create a variable for button and and button b. On the first push check the variable. If the variable is 0 then store a 1 to that variable. Now check the variable again to determine which action to carry out. If the varaible is 0 than don't allow power to the relay's coil. If the variable is 1 than power the coil. I'll post a quick example shortly.

Ok here is a quick example making a few tweaks to the button example sketch. Now you may be able to simplify this a bit further. Just keep in mind i threw this together rather quickly.

As you can see when we check the state of the button we also check the state of our variable. If it's a 1 we change it to 0 and if it's a 0 we change it to a 1 when the button is pressed. Now the actions that are taken are based on the state of that variable. In this case if the buttonval is 1 the LED will stay on otherwise it is off. This leaves things open so if you wanted to add a 3rd function for the 3rd button press you can do so. Then instead of using else you would use 2 other if statements to check the variable and carry out the required actions. Your 3rd option could be to blink the LED every second.

const int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 13; // the number of the LED pin

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

int buttonval = 0; //variable for tracking the number of times the button has been pressed

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop(){

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState == HIGH && buttonval == 0) {

buttonval = 1;

}

if (buttonState == HIGH && buttonval == 1) {

buttonval = 0;

}

if (buttonval == 1){

// turn LED on:

digitalWrite(ledPin, HIGH);

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

}

lukestanmore (author)  mpilchfamily3 years ago

Thanks for your reply! Having read what youve said a couple of times, I think I understand how it should work in principle... When I read the sketch, I understand parts of it to an extent and I can more or less follow it through, however, when I uploaded it to my nano, nothing happened! It compiled and programmed fine and the lights all flashed, but the sketch seemed to just not work somehow? Any suggestions? Thanks!

It's the example sketch for the regular arduino. So it's looking for the LED to be connected to pin 13 and the button to be on pin 2. Refer to the arduino examples page to make sure you have everything connected correctly.

http://arduino.cc/en/Tutorial/Button#.UxdhEvldUpg

Also check out the button state example for a better way of dealing with your project.

http://arduino.cc/en/Tutorial/ButtonStateChange#.U...

lukestanmore (author)  mpilchfamily3 years ago

Double checked how its wired up, and even tried using a different output/:ED on D12, still nothing! And the button definitely works, because I've been using it with the debounce example - I'm at a loss!

I'll check out the second tutorial you've posted, thanks again!

Show your circuit, and your code.

lukestanmore (author)  steveastrouk3 years ago

Sorry for the long delay, had a lot of work this week!! I have tried lots of little variations of code, mainly the debounce example (which worked with one input and output only) I also tried the example posted above, which seemingly did nothing!

These photos show - the home made lamp which this circuit is for, the circuit diagram (quickly drawn) and more or less the complete circuit on the breadboard

20140312_204604.jpg20140313_214933.jpg20140313_214949.jpg

Show me your code.

How is the arduino even working at all? You show the 12V source being fed into the Vin pin. The arduino can only handle 5V on the Vin.

The whole idea behind using a relay is to isolate your lower voltage micro controller from the higher voltage lamp.

If the main cryteria here is to use 2 push buttons to turn a couple of relays on and off and you don't need any other fancy stuff there is a simpler solution. What you need to do is make a couple of Transistor Latches.

http://homemadecircuitsandschematics.blogspot.com/...

One push and the circuit is on, another push and the circuit is off.