[SOLVED] Variable doesn't get stored

Hi!
I'm having some weird thing I can't figure out in the code.
briefly, it's a tachometer, but I'm doing it with a laser and a light sensor (because I have them) and it works just fine, if not for the rpm variable that seems to not keep the latest value it's given.
every 10 seconds, I'm updating the rpm value, but in the plotter it only stays for that loop iteration where the if statement is true. otherwise it's set to 0. I don't get why since prevVal is taking the proper value and there's nowhere else in the code where it sets rpm to 0. I usually don't have this type of issues.
anyone knows the right way?

int count=0;
int prevVal;
int val=0;
int rpm=0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  val=analogRead(A0);
  Serial.print(val);Serial.print(" ");
  if (val>400 && val-prevVal>200){
    count++;
  }
  Serial.print(count);Serial.print(" ");
  prevVal=val;
  if(millis()%10000<10){
    rpm=count*6;
    count=0;
  }
  Serial.println(rpm);
}

Did you post the right version of your code? I suspect that won't compile.

What you posted does not compile

This if statement does not result in the rpm being updated every 10 seconds. What it actually does is cause the rpm to be updated in every loop iteration for 10 ms every 10 seconds.

  int rpm=0;

This variable is defined inside setup(). That makes the variable local to setup(). As soon as setup() ends (which is immediately after the line) it's value is lost.

    rpm=count*6;

Here, the variable is used in loop(). But it is not local to loop and it is not a global variable. So this line will give an error message.

What you probably want is this:

  if (millis() - prevMillis > 10000){
    rpm=count*6;
    count=0;
    prevMillis = millis();
  }

You'll want to define prevMillis as an unsigned long.

sorry, I tried that briefly and saw it didn't compile. unfortunately copy pasted that one... -___-

I edited the code. I know it won't work if it's not declared all the way up. so now with the proper code, it should work, but rpm won't keep the previous value.

@ToddL1962 , thanks. using modulo or prevMillis does the same thing so I guess the issue isn't in the condition of the statement. I tried unsigned long on rpm but that doesn't change a thing.

Not possible. Using modulo the way you are is a huge issue.

In your original code change the the if statement to the following and try it:

  if(millis()%10000<10){
    rpm=count*6;
    count=0;
    Serial.println("10s Update rpm");
  }

Now try this code:

int count=0;
int prevVal;
int val=0;
int rpm=0;
unsigned long prevMillis = millis();

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  val=analogRead(A0);
  Serial.print(val);Serial.print(" ");
  if (val>400 && val-prevVal>200){
    count++;
  }
  Serial.print(count);Serial.print(" ");
  prevVal=val;
  if(millis()-prevMillis > 10000){
    rpm=count*6;
    count=0;
    prevMillis = millis();
    Serial.println("10s Update rpm");
  }
  Serial.println(rpm);
}

See the difference?

I see the difference, and I understand why rpm is then replaced by 0. I confirmed that by adding count>0 in that statement.

obviously going to use your way as it's cleaner. I feel like a beginner now! xD
thanks a lot!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.