samedi 27 juin 2020

Arduino timer with set/reset buttons

i'm building an automatic hay feeder for my horse,something like this but with just two floors enter image description here

every 24hrs the plate in the middle should fall and let the hay fall down for the horse to eat,the interface should be very simple,3 buttons one to start the timer, one to stop it and deploy the hay and one to reset the servo in his initial position to lock the plate again, i'm having some problems with the timer,it starts when i press the green button but after it finishes to count it stops and i have to press the green button again, instead it should go endlessly unless i press the red button to reset it

const int greenB = 2; 
const int redB = 3;        

int inAction = 0;
int greenState = 0;
int redState = 0;         
void setup() {

Serial.begin(9600);
pinMode(greenB, INPUT);
pinMode(redB, INPUT);
}

void loop() {

  greenState = digitalRead(greenB);
  redState = digitalRead(redB);

  if(greenState == HIGH){
    inAction = 1;
    while(inAction == 1){
      for(int i = 0; i<10;i++){
        if(redState == HIGH){
          Serial.println("timer stopped");
          goto stopTimer;        
        }
        if(i == 9){
          Serial.println("Cycle completed");
        }
         
        Serial.println("10 seconds timer");
        delay(1000);  
      }
      stopTimer: inAction = 0; 
      
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire