Hi at all, I got a little problem in the project 10, the enable button works, the potentiometer too but there is a problem with the direction... the spin only in one direction, in the other it stops. That's the code:
const int controlPin1=2;
const int controlPin2 = 3;
const int enablePin = 9;
const int directionSwitchPin = 4;
const int onOffSwitchStatePin = 5;
const int potPin = A0;
int onOffSwitchState=0;
int previousOnOffSwitchState=0;
int directionSwitchState = 0;
int previousDirectionSwitchState = 0;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
void setup()
{
pinMode(directionSwitchPin,INPUT);
pinMode(onOffSwitchStatePin,INPUT);
pinMode(controlPin1,OUTPUT);
pinMode(controlPin2,OUTPUT);
pinMode(enablePin,OUTPUT);
digitalWrite(enablePin, LOW);
Serial.begin(9600);
}
void loop()
{
onOffSwitchState = digitalRead(onOffSwitchStatePin);
delay(1);
directionSwitchState=digitalRead(directionSwitchPin);
motorSpeed = analogRead(potPin)/4;
if(onOffSwitchState != previousOnOffSwitchState)
{
if(onOffSwitchState == HIGH)
{
motorEnabled = !motorEnabled;
}
}
if(directionSwitchState != previousDirectionSwitchState)
{
if(directionSwitchState == HIGH)
{
motorDirection = !motorDirection;
}
}
if(motorDirection == 0)
{
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
Serial.println("Direction 1 (The motor turn)");
}
else
{
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
Serial.println("Direction 2(The motor doesn't turn)");
}
if(motorEnabled == HIGH)
{
analogWrite(enablePin, motorSpeed);
}
else
{
analogWrite(enablePin , 0);
}
previousDirectionSwitchState = directionSwitchState;
previousOnOffSwitchState=onOffSwitchState;
}
Arduino switch correctly between the and and else of the direction because on the serial monitor I see both the message(direction 1 and direction 2) but in the direction 2 the motor doesn't turn. I tried to reverse HIGH and LOW and I realized that the motor never turn when the controlPin1 is LOW and the controlPin2 is HIGH. Where is the problem?
In the attached I there are the photo of the breadboard. Thank you in advance and sorry for bad language, I''m not english