How to update and save variable value

Hello there,

Please forgive my dumb question, I am rather new to the topic. What I am trying to create is a treadmill trip comuter based on an IR sensor (yeah, my treadmill displays wrong data and can not be calibrated).
When a white object appears on a treadmill band I want the variable 'trip' to be updated and saved. Yet all I am getting now in the serial monitor is 4 4 4 4 rather then 4 8 16 20
Please give me a suggestion, not a straignt answer as I want to learn myself.

int irSensor = 3;
bool irValue = HIGH;
int length = 4;
int trip = 0;

void setup() {
  Serial.begin(9600);
  pinMode(irSensor, INPUT);
}

void loop() {
  irValue = digitalRead(irSensor);
  if (irValue == LOW)
  {
    Serial.println(trip+length);
  }
  else
  {
    Serial.println();
  }
  delay(100);
}

Admirable.

You print the value

    Serial.println(trip+length);

but nowhere do you assign that new value to a variable for it to remember.

a7

1 Like

If you want the value to change, you must write code to change it. Right now, you do nothing with either variable.

Hello,
I completely understand that. Could you give me a piece of advice by which function I could get value updated after sum?
Something like this does not help either. Still getting 4 4 4

void loop() {
  int trip;
  irValue = digitalRead(irSensor);
  if (irValue == LOW)
  {
    trip += length;
    Serial.println(trip);
  }
  else
  {
    Serial.println();
  }
  delay(100);
}

@Delta_G has the answer.

In this case, your trip is a local variable, made newly each loop iteration and furthermore having no initial value that can be counted on.

So getting 4 4 4 was somewhat lucky. There is no reason to hope that the value woukd be zero.

This is something the compiler, I think, woukd have warned you about, use of an uninitialised variable blah blah blah…

if only warnings were not suppressed by default.

In the IDE, go to the preferences and dial up all verbosity and warnings. Now when you compile you get extra help when it notices you've maybe done something you did not exactly mean, even if it is not a show-stopping error.

Just read the red ink. Some warnings are just that, to draw your attention to something fishy but you know better. Other times what is pointed out is a mistake it helped you to not make.

HTH

a7

Hello Delta,

No matter I create it as global or local unfortunately it's not getting updated. What I'm trying to understand is what function could help me to change the value after each loop. I have an assumption though, is it writeIntIntoEEPROM?

No.

Post the complete sketch where you tried making trip either global, or local with the static qualifier added, that did not work.

Generally, when you say you've tried something, don't leave us to assume that you tried what we suggested, and did it correctly. New code, new post on this thread with that code, and a description or reminder of how it fails to be all you want.

a7

1 Like

A little C trick, what is in ( ) is executed like a little line of code,

so Serial.println( trip += length ); // does both lines above!

Hello Delta_G
Long time no see. I have managed to figure out that issue with global variable and now I am able to measure distance properly.
The blocking point now is speed measuring. Basically I am trying to count it based on time between white sticker is detected. Yet I am even getting values below zero sometimes. Is PulseIn in the wrong place? Do I have to use interruptions instead? This thing looked kinda complicated to me.
Kindly give me a piece of advice. Thank you!

// white sticker to be put on a black treadmill band in front of IR sensor
int irSensor = 3; // IR sensor digital input pin number 
bool irValue = HIGH;
int length = 6; // treadmill band length 
int trip; // total distance 
int speed; // current speed between sticker detected 
int revo_time; // time between sticker is detected

void setup() {
  Serial.begin(9600);
  pinMode(irSensor, INPUT);
}

void loop() {
  irValue = digitalRead(irSensor);
  if (irValue == LOW)
  {
    Serial.println(trip += length); // print total distance
    
    revo_time = pulseIn(irSensor, LOW);
    Serial.println(speed = length / revo_time ); // print speed
    delay(500);  //long delay when sticker is deteced so it is not detected twice on a low speed 
  }
  else
  {
    delay(5); // sticker not detected, check again in 5ms 
  }
  
}

 delay(500);  //long delay when sticker is deteced so it is not detected twice on a low speed 
 

This is a very crude way to do what you want.

You need to detect when the target becomes pressed rather than when it is detected
See the StateChangeDetection example in the IDE

Hello,
Sorry I do not get it. Why 'pressed'? It is not really a button. It's just a white sticker to be detected on a treadmill band. Once it's found there is a 500ms delay not to shoot IR beam into it more then once.
In any case the issue now is the time measurement between two events when the sticker is detected

My apologies. I used part of one of my standard replies and edited it but left in the reference to "pressed"

The post should read

You need to detect when the target becomes detected rather than when it is detected
See the StateChangeDetection example in the IDE

1 Like

ditto to what UKHeliBob wrote,

"a button press" is a form of sensor Input

We teach with buttons/switches because they are simple and cheap. I don't even use a button, I just [;ig a jumper into the pinhole and ground that in a GND hole or on the USB plug housing simce it's grounded.

Button is synonymous to sensor input though details do vary.
You might as well get used to it, you will see it again!

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