I have two micro switches, two connections to a relay board, and an led output. Trying to control motor direction via the relay board and getting very inconsistent results. One switch will enable a single relay, but the other enables both which of course creates a short on the H bridge. I have about 4 feet of wire from the DigiSpark to the switches - could this be causing the issue?
Here is my code:
int led = 1;
int motorR = 4;
int motorL = 5;
int switch1 = 0;
int switch2 = 2;
// variable declarations
int switch1State = 0;
int switch2State = 0;
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(motorR, OUTPUT);
pinMode(motorL, OUTPUT);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
digitalWrite(motorR, HIGH); // HIGH = off
digitalWrite(motorL, HIGH); // HIGH = off
}
void loop() {
digitalWrite(led, HIGH);
delay(75);
digitalWrite(led, LOW);
delay(75);
switch1State = digitalRead(switch1);
if (switch1State == HIGH) {
digitalWrite(led, LOW);
digitalWrite(motorL, LOW);
} else {
digitalWrite(motorL, HIGH);
}
switch2State = digitalRead(switch2);
if (switch2State == HIGH) {
digitalWrite(led, LOW);
digitalWrite(motorR, LOW);
} else {
digitalWrite(motorR, HIGH);
}
} // end void
Any help would be appreciated
Thanks