I want to print a value of a variable every second, but the value of the variable changes every 500 ms. I want to print every value of the variable.But anything i do,i keep missing one value. I'm a beginner in Arduino,it may be a silly doubt please help me guys.
Print it when it changes in your code…
Otherwise post your code…
It can be done, but it seems a crazy thing to do, and the memory limitations of the Arduino will limit how long it can run for.
After 10 seconds, the variable will have changed 20 times, but you will have printed only the first 10 values. The other 10 values can be held in memory and printed over the next 10 seconds, but by that time the variable will have changed another 20 times. As time continues, the printing will get further and further behind the variable changes, and more and more values will need to be held in memory. Most Arduinos have a very limited amount of memory, which will soon be filled.
So why do you want to do this? Will the variable eventually stop changing, allowing the printing to catch up?
you have to write variable to buffer every half a second and read that buffer every second. Come back after you run out of memory and elaborate more on what you are trying to do
what about following which prints the value whenever it changes OR every second
unsigned long msecLst;
int valLst;
void
loop ()
{
unsigned long msec = millis ();
int val = analogRead (A1);
if (val != valLst || msec - msecLst >= 1000) {
msecLst = msec;
valLst = val;
Serial.println (val);
}
}
void
setup ()
{
Serial.begin (9600);
}
I'm making robot that plays google dino. How it works is that I'll calculate the speed of the cactus
and find out the time to reach the dino and click the space bar. But what happens is that by time the cactus reach the dino another cactus appear so
the variable that has stored the old time gets updated.thus i miss to click the space bar.
What if I use two variables instead of one i think it would work.
Suppose when the value changes you print that and two spaces, and every second you serial write a newline character ( '\n' )? Then every line is 1 second.
how do you "see" the cactus? (and sometimes there are more than one cactus)
By using LDR
I assume you find the cactus barrier's width, not just the start
Can you relate this back to the question you asked in post #1 please?
This thread could easily go down the path of "what is it? A game of 100 questions". Please respect the forum guidelines and post clear and complete information about your problem. This document describes the kinds of things you should post:
The old variable gets updated before the value has been considered.
This is my first time using forum. I know that i messed up.
Is this the project you are doing?
If so, post the code and specify which part is troubling you.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.