Pages: [1]   Go Down
Author Topic: Project 10 - Zoetrope , The motor spin in only one direction  (Read 1354 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley-grin


* IMAG0295.jpg (1190.02 KB, 2560x1440 - viewed 68 times.)

* IMAG0296.jpg (1187.04 KB, 2560x1440 - viewed 55 times.)

* IMAG0297.jpg (1445.75 KB, 1440x2560 - viewed 53 times.)
Logged

Pages: [1]   Go Up
Jump to: