Arduino with counter

Asked

Viewed 285 times

-1

I can’t understand what’s wrong with that code S1 once and M1 stay turned on but it turns off after a while. The program also doesn’t count as it should.

The question is this: In the system below when a part is placed in the position given by the sensor S1 the engine M1 is turned on, leading the piece to the sensor s2 and falling into the output box. It is known that the box supports up to 20 pieces. Implement a counter (cont) so that and only when 20 pieces fall into the engine box M1 is switched off and the lamp L1 is turned on, alerting the operator of the need to exchange the box. Implement an algorithm that meets the requested.

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);

 }

 }
  • Dude, identa the code, and have a spare key at the end, and what you want to do is out of the loop(), it will cause what this out of the loop() run only once.

  • Remember that what is in setup() runs only once, what is in loop() will be "eternally" running

  • How do I ident the code, what key is left?

  • No one else is inside the loop()

1 answer

-1

I got it. This is 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);

 }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.