Two Buttons as inputs and Two Outputs

Can someone help me out? I uploaded the script into a mega 2560. My intensions are to feed a (on, off, on) toggle switch with 5v. Then depending if the toggle forward, then 5v output from respected pin. For example, if 5v goes to pin2, then output 5v from pin11. If the toggle is pulled back, a different output of 5v. The script compiles good, but is not doing anything besides two outputs staying on. My #6output should be off if my toggle is in the middle position, but it is staying on. Where did I go wrong? Do I need to ground 5v for a input to read low or just no 5v to it? const int buttonPinf = 2; const int buttonPinr = 7;// the number of the pushbutton pin const int forwardPin = 11; const int reversePin = 6; // the number of the LED pin

int buttonPinfState = LOW; int buttonPinrState = LOW;

void setup() {

pinMode(forwardPin, OUTPUT); pinMode(reversePin, OUTPUT); pinMode(buttonPinf, INPUT); pinMode(buttonPinr, INPUT); }

void loop(){ buttonPinfState = digitalRead(buttonPinf); buttonPinrState = digitalRead(buttonPinr);

if (buttonPinf == HIGH) { digitalWrite(forwardPin, HIGH); //close 1st relay with pin11 } else { digitalWrite(forwardPin, LOW); } if (buttonPinr == HIGH) { //turns on neutral&reverse for reverse: digitalWrite(forwardPin, LOW); delay(1000); digitalWrite(reversePin, HIGH); } else { //will stop reverse relay for neutral only: digitalWrite(reversePin, LOW); } }

How are the switches wired? What prevents the inputs from floating when the switches are open, pull down resistors?

I only took 5v from the board 5v and ran it to a regular (on, off, on) toggle switch. Then each leg of the ONs are going to send the 5v if the toggle is flipped.

A switch needs pull up or pull down resitors to prevent the pin from "floating" when the switch contact is open.

Easiest way is to use Arduino's buildin pull up resitors (then no external resistors are needed). Connect the center pin of the switch to ground, and the outside pins of the switch to the two input pins.

Change pinMode to:

pinMode(buttonPinf, INPUT_PULLUP); pinMode(buttonPinr, INPUT_PULLUP);

Inverted logic now. Pins are HIGH when the switch is in the center position and LOW when switched, so change your code accordingly. Leo..

I am going to try that. I was debating if Analog inputs would do that too, but it's been awhile and I usually do timing outputs with no inputs. I went to upload a photo, but the preview wasn't showing it. Making a didn't want a wasted look at the no photo.

How to Upload image.


I put the file in OneDrive instead and here’s the link. Adobe might be easier, but I’ll try to link the reply image too.

Note that the Mega can't/shouldn't power more than four relays at the time on USB supply, and even less on external supply. Active relays draw about 75mA each. Leo..

The relay strip only happened to be sitting for the project. Only two of the 8 would be used. I also would get new ones once I figure the prohect out. I have a few baseline supplies that seem to not break and then this project would probably end up on an arduino nano. I switched the script around a little, but believe I understand what everyone means by grounding the button or PULLUP, but it still seems I will need to tinker and reorganize a better presentation. Let me mess around and see what happens. I’ll let everyone know.