Arduino with counter

0

I can not understand what is wrong with this code, I was supposed to press s1 once and m1 to stay connected but it turns off after a while. The program also does not count as it should.

The question is this: In the system below when a part is placed in the position given by the s1 sensor the motor m1 is turned on, taking the part to the sensor s2 and falling in the output box. It is known that the box supports up to 20 pieces. Implement a (cont) counter so that and only when 20 pieces fall into the box the motor m1 is turned off and the l1 lamp is turned on, alerting the operator of the need to change the Cashier. Implement an algorithm that satisfies the request.

const int s1 = 6;

const int s2 = 7;

const int m1 = 8;

const int l1 = 11;

int cont = 0;

void setup() {

// put your setup code here, to run once:

  pinMode(s1, INPUT_PULLUP);

  pinMode(s2, INPUT_PULLUP);

  pinMode(l1, OUTPUT);

  pinMode(m1, OUTPUT);

  Serial.begin(9600);

}

   void loop(){

  while(digitalRead(s1) == HIGH){

    Serial.print("Valor de S1 = ");

    Serial.println(s1);

 }

  digitalWrite(m1, HIGH);

  if((digitalRead (s2) == LOW) && (cont < 20)){

    cont ++;

    Serial.print("número de vezes em que o botão foi pressionado:");

    Serial.println(cont);

    }

  else{

  digitalWrite (m1, LOW);

digitalWrite (l1, HIGH);

 delay(5000); //tempo para o LED permanecer acesso

 digitalWrite (l1, LOW);

 }

 }
    
asked by anonymous 14.04.2018 / 12:17

1 answer

0

I did it. This was the code I used:  const int s1 = 6;

const int s2 = 7;

const int m1 = 8;

const int l1 = 11;

int cont = 0;

void setup () {

// put your setup code here, to run once:

pinMode (s1, INPUT_PULLUP);

pinMode (s2, INPUT_PULLUP);

pinMode (l1, OUTPUT);

pinMode (m1, OUTPUT);

Serial.begin (9600);

while (digitalRead (s1) == HIGH) {

Serial.print("Valor de S1 = ");

Serial.println(s1);

}

digitalWrite(m1, HIGH);

}

void loop () {

if((digitalRead (s2) == LOW) && (cont < 20)){

cont ++;

Serial.print("número de vezes em que o botão foi pressionado:");

Serial.println(cont);

}

 else if((digitalRead (s2) == HIGH) && (cont >= 20)){

  digitalWrite (m1, LOW);

  digitalWrite (l1, HIGH);

  delay(5000); //tempo para o LED permanecer acesso

  digitalWrite (l1, LOW);

 }

}

    
14.04.2018 / 15:45