0
votes

I have been using my Arduino to control motor polarity with a Cytron MD-10 motor driver shield, and I had it working.

Then when I came back to work it seemed as though the digital read pins were not differentiating between high and low voltage and one of the lights was powered on continuously.

The outputs function as set up with a continuous supply of measured 4.84 volts and then the switch is closed the voltage drops to 0 and the corresponding loop should be entered. Is my board fried?

Anything I should try?

const int outSwitch = 13;
const int inSwitch = 12;
const int pinPWM = 3;
const int pinDir = 2;

int lightOne = 11;
int lightTwo = 10;

static int Dir = 1;
static int cycleCounter = 0;

void setup() {
    // Set Pin Out/Inputs:
    pinMode(pinPWM, OUTPUT);
    pinMode(pinDir, OUTPUT);
    pinMode(outSwitch, INPUT);
    pinMode(inSwitch, INPUT);
    pinMode(lightOne, OUTPUT);
    pinMode(lightTwo, OUTPUT);
    analogWrite(pinPWM, LOW);
}

void loop() {

    // Both read a low input value from the switch and then makes
    // direction the opposite causing it to travel backwards:
    if(digitalRead(inSwitch == LOW)){
        analogWrite(pinPWM, HIGH);

        digitalWrite(pinDir, Dir);
        digitalWrite(lightOne, LOW);
        digitalWrite(lightTwo, HIGH);
    }

    else if(digitalRead(outSwitch == LOW)){
        analogWrite(pinPWM, HIGH);

        digitalWrite(pinDir, -Dir);
        digitalWrite(lightOne, LOW);
        digitalWrite(lightTwo, HIGH);
    }
}
2
In if and else if conditions, brackets are bit wrong. They should be if(digitalRead(inSwitch) == LOW) and else if(digitalRead(outSwitch) == LOW)svtag

2 Answers

0
votes

Sma correctly identified the problem as a misplacement of the parentheses:

if (digitalRead(inSwitch == LOW))  // OOPS!

This compares inSwitch to LOW, which probably returns false. Since digitalRead expects an int, that false is converted to 0. So you do a read of pin 0. I don't think there is a pin 0, so you probably get an error. That return value is then implicitly compared to 0 in order to determine which branch of the if-statement to take.

The correct statement would look like this:

if (digitalRead(inSwitch) == LOW)

(Community Wiki because I'm just trying to provide a more detailed answer. I'm not trying to get rep for someone else's observation.)

0
votes

What does analogWrite(pinPWM, HIGH); even mean? Missing a value (0-255), HIGH is not a valid value.