I can't additional my variable

Hello,

I am working on a project where I have to set a timer each time you scan a false RFID-badge. After, when the time is over you can retry to scan the right RFID-badge (because there are multiple badges on the table and you have to find the right badge). When you still scan the false RFID-badge, well the timer grows exponentially.

...
 
 else   {
    aantalFouteBadge++;
    if(aantalFouteBadge != 1){
    minuten = minuten + minuten;
    Serial.println();
    Serial.println(minuten);
   }

My problem is that minuten = minuten + minuten doesn't work.
minuten is in the beginning 2, so after this statement minuten should be 4 (2+2) but it's still 2.
Does anyone have any idea why ?

Here I put my whole code and you can find minuten is in the void setup().

#include <SPI.h>

#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int pinRST = 9;
const int pinSS = 10;
MFRC522 mfrc522(pinSS, pinRST);

int incomingByte;
int rood = 2;
int wit = 3;
int geel = 4;
int groen = 5;
int lijstGetallen[14];
int teller = 4;
int tellerr = 1;
int seconden = 0;
int minuten = 2;
int aantalFouteBadge = 0;
boolean badgeOpnieuw = true;
boolean doorOfNiet = true;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  randomSeed((analogRead(0)));
  pinMode(6, OUTPUT);
  pinMode(rood, OUTPUT);
  pinMode(wit, OUTPUT);
  pinMode(geel, OUTPUT);
  pinMode(groen, OUTPUT);
  for(int a = 0; a < 14; a++){
    lijstGetallen[a] = random(2,6);
  }
  lcd.begin();
  while(badgeOpnieuw){
    lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Luister en scan");
  lcd.setCursor(0,1);
  lcd.print("de juiste badge");
    delay(2000);
    tone(6, 500);
    delay(250);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(250);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(500);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(500);
    while ( ! mfrc522.PICC_IsNewCardPresent()){
   
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  
  if (content.substring(1) == "2B 41 71 0D") //change here the UID of the card/cards that you want to give access
  {
    lcd.clear();
    digitalWrite(5, HIGH);
    delay(1000);
    digitalWrite(5, LOW);
    lcd.setCursor(0,0);
    lcd.print("proficiat, pass:");
    lcd.setCursor(0,1);
    lcd.print("1234");
    delay(3000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("download arduin.");
    lcd.setCursor(0,1);
    lcd.print("bluetooth contr.");
    badgeOpnieuw = false;
    delay(3000);
  }
 
 else   {
    aantalFouteBadge++;
    if(aantalFouteBadge != 1){
    minuten = minuten + minuten;
    Serial.println();
    Serial.println(minuten);
   }
    lcd.clear();
    digitalWrite(2, HIGH);
    delay(1000);
    digitalWrite(2, LOW);
    lcd.print("foute badge");
    tone(6, 500);
    delay(1000);
    noTone(6);
    lcd.setCursor(0,1);
    seconden = 60;
    

   while(true){
    lcd.setCursor(0,1);
    if(seconden < 10){
      lcd.print((String)(minuten-1)+ ":0" + (String)seconden); 
    }else{
      lcd.print((String)(minuten-1)+ ":" + (String)seconden); 
    }
    seconden--;
    if(minuten == 1 && seconden == 0){
      break;
    }
    if(seconden == 0){
      seconden = 60;
      minuten--;
    }
    delay(1000);
   }
  } 
  }
}


void loop() {
  // put your main code here, to run repeatedly:
  //hier wordt de memory game afgespeeld
  

  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("level " + (String)tellerr);
  for(int b = 0; b < teller; b++){
     digitalWrite(lijstGetallen[b], HIGH);
     delay(500);
     digitalWrite(lijstGetallen[b], LOW);
     delay(500);
  }
 
  //hier voert de gebruiker zijn gegevens
  for(int c = 0; c < teller; c++){
    while(Serial.available() <= 0){
   
     }
     if (Serial.available() > 0) {
      incomingByte = Serial.read();
      switch(incomingByte){
        case '5':
          digitalWrite(groen, HIGH);
          delay(500);
          digitalWrite(groen, LOW);
          break;
        case '4':
          digitalWrite(geel, HIGH);
          delay(500);
          digitalWrite(geel, LOW);
          break;
        case '3':
          digitalWrite(wit, HIGH);
          delay(500);
          digitalWrite(wit, LOW);
          break;
        case '2':
          digitalWrite(rood, HIGH);
          delay(500);
          digitalWrite(rood, LOW);
          break;
      }
      delay(200);
      if((incomingByte-48) != lijstGetallen[c]){
        doorOfNiet = false;
        lcd.clear();
        lcd.print("failed");
        tone(6,500);
        for(int d = 0; d < 4; d++){
          digitalWrite(rood, HIGH);
          digitalWrite(wit, HIGH);
          digitalWrite(geel, HIGH);
          digitalWrite(groen, HIGH);
          delay(250);
          digitalWrite(rood, LOW);
          digitalWrite(wit, LOW);
          digitalWrite(geel, LOW);
          digitalWrite(groen, LOW);
          delay(250);
        }
        noTone(6);
      }
   }
  }
  //blinkende lampen en buzzer indien alles juist
  delay(200);
  if(doorOfNiet){
    tellerr++;
    if(teller < 14){
       teller++;
    }
    tone(6,1000);
    digitalWrite(rood, HIGH);
    digitalWrite(wit, HIGH);
    digitalWrite(geel, HIGH);
    digitalWrite(groen, HIGH);
    delay(500);
    digitalWrite(rood, LOW);
    digitalWrite(wit, LOW);
    digitalWrite(geel, LOW);
    digitalWrite(groen, LOW);
    noTone(6);
    delay(1000);
  }
}

Please help me because I am in the shit right now and it sucks. It's a project for school.

My first guess would be that the block of code changing the value of minuten isn't being executed?

MK1888:
My first guess would be that the block of code changing the value of minuten isn't being executed?

Hello MK1888

Thanks for you answer, but the else block is being executed so why would

int minuten = minuten + minuten

not being executed ?

Thank you that you are trying to help me because it's a shame that you are the only one that answers me.

int minuten = minuten + minuten

not being executed ?

Who says it isn't?

(That "int" is a worry)

int minuten = minuten + minuten

That is not the line of code in the sketch that you posted

Why have you got a big WHILE loop in setup()? Stuff that needs to repeat should go into loop().

Also, it will be a great deal easier to develop and debug your program if you break it down into a series of short functions with meaningful names. Have a look at Planning and Implementing a Program

And your program is littered with long delay()s which block the Arduino from doing other things until they complete. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

See Using millis() for timing. A beginners guide if you need more explanation.

...R

UKHeliBob:

int minuten = minuten + minuten

That is not the line of code in the sketch that you posted

Yeah that's true but it's only to make it easier for you so that you know which datatype it is.

Robin2:
Why have you got a big WHILE loop in setup()? Stuff that needs to repeat should go into loop().

Also, it will be a great deal easier to develop and debug your program if you break it down into a series of short functions with meaningful names. Have a look at Planning and Implementing a Program

And your program is littered with long delay()s which block the Arduino from doing other things until they complete. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

See Using millis() for timing. A beginners guide if you need more explanation.

...R

Ok thank you for your answer, I will try.

TheMemberFormerlyKnownAsAWOL:
Who says it isn't?

I checked this by debug it with Serial.println() and the else block is being executed except the minuten = minuten + minuten.

TheMemberFormerlyKnownAsAWOL:
(That "int" is a worry)

But it's only to make it easier for you so that you know which datatype it is. But why is it a worry ?

Thanks for your answer.

it's only to make it easier for you so that you know which datatype it is.

I knew that from the sketch.

Try printing the value before adding it to itself but print a text label so that you know what you are seeing in the Serial monitor. What happens if you multiply the value by 2 instead of adding it to itself ? What happens if you add a fixed value to it ? Do any of them work ?

UKHeliBob:
I knew that from the sketch.

Try printing the value before adding it to itself but print a text label so that you know what you are seeing in the Serial monitor. What happens if you multiply the value by 2 instead of adding it to itself ? What happens if you add a fixed value to it ? Do any of them work ?

I tried but it doesn't work. What doe you mean by "print a text label" ? I guess you just mean Serial.println() ?

Also when I print in the Serial monitor well it only prints the half of my text. I always have a problem after problem.

thank you that you are trying to help me.

I tried but it doesn't work.

What exactly did you try ?

What doe you mean by "print a text label" ?

What I meant was that if you just print the value of the variable you will get a number in the Serial monitor but won't know what it means. If, however, you do something like this

Serial.print("minuten before doubling : ");
Serial.println(minuten);
minuten = minuten + minuten;
Serial.print("minuten after doubling : ");
Serial.println(minuten);
Serial.println();  //blank line before next printout

then you will know what the values relate to

iliwasel:
I tried but it doesn't work.

Then you need to post the program that you tried and tell us exactly what happens when you run it. "It doesn't work" provides no useful information from which to help you.

...R

Robin2:
Why have you got a big WHILE loop in setup()? Stuff that needs to repeat should go into loop().

Also, it will be a great deal easier to develop and debug your program if you break it down into a series of short functions with meaningful names. Have a look at Planning and Implementing a Program

And your program is littered with long delay()s which block the Arduino from doing other things until they complete. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

See Using millis() for timing. A beginners guide if you need more explanation.

...R

Where should I declare functions ? I see some people declaring functions after they are called. Like this

void setup(){
...
blink();
}

blink(){
...
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
...
}

Shouldn't you declare functions before they are called ?

UKHeliBob:
What exactly did you try ?
What I meant was that if you just print the value of the variable you will get a number in the Serial monitor but won't know what it means. If, however, you do something like this

Serial.print("minuten before doubling : ");

Serial.println(minuten);
minuten = minuten + minuten;
Serial.print("minuten after doubling : ");
Serial.println(minuten);
Serial.println();  //blank line before next printout



then you will know what the values relate to

Now I can't test what you posted because my RFID doesn't work after using more than 1 time.

I opened a new topic for this problem where no one answers.

Here is the link of my topic

https://forum.arduino.cc/index.php?topic=678441.msg4565525#msg4565525

iliwasel:
Shouldn't you declare functions before they are called ?

The Arduino IDE does not require that. It adds function declarations when necessary.

...R

The Arduino IDE does not require that. It adds function declarations when necessary.

The Arduino IDE does not require that in most cases. It adds function declarations when necessary except when it doesn't such as for functions with optional parameters

iliwasel:
I opened a new topic for this problem where no one answers.

Double Posting is against the rules of the Forum - it just wastes everyone's time. I suggest you go to your other Topic and click Report to Moderator and ask to have it deleted.

...R