0

Using an Arduino-Nano, I'm trying to make a circuit that will change the direction of a motor based on two inputs. The input from the Arduino will be amplified by a transistor. Here's the code:

int rightIn = 2;
int rightOut = 3;
int leftIn = 4;
int leftOut = 5;
int rightVal = 0;
int leftVal = 0;

void setup() {
  pinMode(rightIn, INPUT);
  pinMode(leftIn, INPUT);
  pinMode(rightOut, OUTPUT);
  pinMode(leftOut, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  rightVal = digitalRead(rightIn);
  Serial.println(rightVal);
  leftVal = digitalRead(leftIn);
  if ((rightVal == HIGH) || (leftVal == HIGH)) {
    if (rightVal == HIGH) {
      digitalWrite(rightOut, HIGH);
      digitalWrite(leftOut, LOW);
    }
    if (leftVal == HIGH) {
      digitalWrite(rightOut, LOW);
      digitalWrite(leftOut, HIGH);
    }
  }
  else {
    digitalWrite(rightOut, LOW);
    digitalWrite(leftOut, LOW);  
  }
}

And the arduino wiring:

  • Pins 3 and 5 are attached to the motors power and ground
  • Pins 2 ad 4 go to an seprate inputs

closed as unclear what you're asking by Chris Stratton, Mattia, Dave X, Paul, RSM Jun 1 '16 at 7:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    And your question is? The Stack Exchange sites require stating a clear, answerable problem – Chris Stratton May 14 '16 at 1:11
  • Please read How do I ask a good question? - you have stated what you want. Now you need to describe what is happening, compared to what you want to have happen. – Nick Gammon May 20 '16 at 5:59
0

I think you are mixing up your rightVal and rightIn, and your leftVal and leftIn.

rightVal = digitalRead(rightIn);
....
if (rightIn == HIGH) {
  • I'm just testing it with an led. In theory, would the code work? – Kavi M-D May 13 '16 at 19:46
  • Thanks, now that that's fixed could it work? – Kavi M-D May 13 '16 at 19:50
  • Probably, yes - as long as your circuit is up to it – Majenko May 13 '16 at 19:51
  • Your HIGH/LOW signals will have to switch a motor's pole between power and ground - so you need more than "a transistor". You will need at least two each side in a H-bridge arrangement. – Majenko May 13 '16 at 20:09
  • my motor only needs 3.3 volts, is 2 transistors per side really necessary? – Kavi M-D May 14 '16 at 12:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.