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.
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
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.
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?
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.
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
}
}
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
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!