0

Multitasking while running a timer.

I have implemented the example timer code that we have been given. Now I also want to scan rfid tags while the timer is running. I have replaced all de delays by milli's. The problem is that the timer only starts running after a tag has been scanned. After this first tag it will also stop scanning for other tags. This is a very imported part of our design and it also has to be done with a distance sensor instead of an RFID scanner.

void timer() {

  scannedCard = cardID();

  display.clearDisplay();

  //scannedCard = cardID();
  while (state == 2) {
    //  if ( (scannedCard != rfidNumber[0]) || (scannedCard != rfidNumber[1]) || (scannedCard != rfidNumber[2]) || (scannedCard != rfidNumber[3])) {

    unsigned long currentMillis = millis();


    // update countdown timer every second
    if (currentMillis - previousMillis > interval) { // interval passed?
      previousMillis = currentMillis; // save the last time
      if (state == 2) { // countdown running?
        if (min >= 0 && sec >= 0) { // if counter not finished
          display.drawString(0, 7, "running     ");
          sec--; // decrease timer
          if (sec < 0) {
            if (min > 0 ) {
              min--;
              sec = 59;
            }
            else sec = 0;
          }
        }
        if (min == 0 && sec == 0) { // if counter finished
          display.drawString(0, 7, "finished    ");
          state = 4;
        }
        sprintf(buf, "%02d:%02d", min, sec);
        display.draw2x2String(2, 3, buf);

        if (scannedCard == rfidNumber[0]) {
          //RED
          if (red == 0) { // colour == 0 means that is hasn't been scanned before
            red = 1;
            Serial.println("Red wristband scanned");
            display.clearLine(3);
            display.clearLine(4);
            display.draw2x2String(2, 3, "RED");
            digitalWrite(A0, HIGH);
            sound (500, 1000, 1);

          }
        }


        //BLUE
        if (scannedCard == rfidNumber[1]) {
          if (blue == 0) {
            blue = 1;
            Serial.println("Blue wristband scanned");
            display.clearLine(3);
            display.clearLine(4);
            display.draw2x2String(2, 3, "BLUE");
            digitalWrite(A1, HIGH);
            sound (500, 1000, 1);

          }
        }


        //GREEN
        if (scannedCard == rfidNumber[2]) {
          if (green == 0) {
            green = 1;
            Serial.println("Green wristband scanned");
            display.clearLine(3);
            display.clearLine(4);
            display.draw2x2String(2, 3, "GREEN");
            digitalWrite(A2, HIGH);
            sound (500, 1000, 1);

          }
        }
        //YELLOW
        if (scannedCard == rfidNumber[3]) {
          if (yellow == 0) {
            yellow = 1;
            Serial.println("Yellow wristband scanned");
            display.clearLine(3);
            display.clearLine(4);
            display.draw2x2String(2, 3, "YELLOW");
            digitalWrite(A3, HIGH);
            sound (500, 1000, 1);

          }
        }

      }
    }

  }

}
Share a link to this question (includes your user id)
| edit | | close | delete |
0

Why the while (state == 2) { ? It seems to me the timer() function is in the loop()? (which by default repeats). Why not make that an if (state == 2) { ? There is a chance the Arduino is stuck in the the while (state == 2) { loop and therefore cannot do other things (like scanning tags).

In code like this I would use a lot of Serial.print... statements to closely monitor every relevant variable. Sometimes I have more print statements than normal code. I PRINT EVERYTHING. That is the only way to get insight into what is happening.

In your case you already have some output on the Oled-display, that might help also.

If printing a lot still does not help you to solve the problem, you can send me the entire sketch by email, then I can have a look.

Share a link to this answer (includes your user id)
| edit | delete |

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.