Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more. Accepting essential orders - here's how.
0

Some help with my code..
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Some help with my code..

by kucza on Mon May 04, 2009 6:00 pm

Hi.
I kind of new to Arduino and coding. But im trying.

My problem is, when i press the "switchpin" the motor stops when i release it again. It is surpose to continue to run until I press the "switchpin2", then the motor stops again, waits 50ms and then turn on another motor.
But I can see where I think the problem is. I just dont know how to fix it.

Hope you can help me!

Code: Select all | TOGGLE FULL SIZE
int backPin = 2;      // back input
int forwardPin = 3;   // forward input
int motor1Pin = 4;    // H-bridge leg 1  Rotation right
int motor2Pin = 5;    // H-bridge leg 2  Rotation left
int switchPin = 6;      // Loads the beer! GET READY!
int switch2Pin = 7;      // Stops the wheel
int motor3Pin = 8;    // H-bridge leg 3 LIFT
int motor4Pin = 9;    // H-bridge leg 4 LIFT

void setup() {
  // set the back/forward pins as an input:
  pinMode(backPin, INPUT);
  pinMode(forwardPin, INPUT);
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(motor3Pin, OUTPUT);
  pinMode(motor4Pin, OUTPUT);
 

}

void loop() {

  if (digitalRead(switchPin) == HIGH) // Switch button... Where it all goes wrong!!!
{   
   digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
}

  if(digitalRead(switch2Pin) == HIGH) // stop on the rotation
  {
   digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low
   delay(50);
   digitalWrite(motor3Pin, LOW); 
   digitalWrite(motor4Pin, HIGH);
  }

else if(digitalRead(backPin) == LOW && digitalRead(forwardPin) == LOW && digitalRead(switchPin) == LOW) // AND HERE!!
  {
    digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
  }
  else if(digitalRead(backPin) == HIGH && digitalRead(forwardPin) == LOW)
  {
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  }
  else if(digitalRead(backPin) == LOW && digitalRead(forwardPin) == HIGH)
  {
    digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge high
    digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low
  }
}
kucza
 
Posts: 2
Joined: Mon Mar 23, 2009 6:29 pm

Re: Some help with my code..

by adafruit on Mon May 04, 2009 7:00 pm


adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Some help with my code..

by karlgg on Mon May 04, 2009 8:59 pm

I love the conclusion on lesson 5... "blah blah blah" :)
Don't ever change it! Ever!
I think I am, therefore I am... I think.

karlgg
 
Posts: 212
Joined: Sat Dec 27, 2008 2:41 pm
Location: Anthony, NM

Please be positive and constructive with your questions and comments.