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