0

I setup the feeding system with arduino uno, L298, dc motor and two limit switches. When i press the push button rotate CW and opened gate. All right up to here. But i want to make i pushed button opened and after 5 second rotate CCW and then closed gate, how can i do that ?

The Code ;

const int button = 4;     // Limit switch
const int button2 = 2;     // Limit Switch
const int button3 = 13;     // Start signal
const int motorPin =  8;      // L298 IN2
const int motorPin2 =  7;      // L298 IN1

boolean buttonstatus = 0;         
boolean buttonstatus2 = 0;        
boolean buttonstatus3 = 0; 

int drawPosition = 2; 

void setup() {
    pinMode(motorPin, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(button, INPUT);
    pinMode(button2, INPUT);
    pinMode(button3, INPUT);
    // if the power fails close gate
    digitalWrite(motorPin, HIGH);
    digitalWrite(motorPin2, LOW);
}

void drawForward() {
     digitalWrite(motorPin, LOW);
     digitalWrite(motorPin2, HIGH);
}

void drawBackward() {
     // turn motor in other direction:
     digitalWrite(motorPin, HIGH);
     digitalWrite(motorPin2, LOW);  
}

void stopDrawFromMoving() {
  digitalWrite(motorPin, LOW);
  digitalWrite(motorPin2, LOW);

}

void loop() {
   buttonstatus = digitalRead(button);
   buttonstatus2 = digitalRead(button2);
   buttonstatus3 = digitalRead(button3);

   if (buttonstatus == HIGH) {
      drawPosition = 2; 
      stopDrawFromMoving();
   }else if (buttonstatus2 == HIGH) {
      drawPosition = 1;        
      stopDrawFromMoving();
  } 
if (buttonstatus3 == HIGH) {
    if (drawPosition == 2 ) {
      delay(100); 
      drawForward();
      delay(100); 
      drawPosition = 1;       
    }
  }
}
  • 1
    The logic of your code is not clear enough, also the wanted behavior. Be more specific and add comments to the code. Use more speaking variable names (open_button instead of button3). What exactly is your problem here? Timed things can be done with millis(), there are tons of tutorials on the web about that – chrisl Jun 17 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.