say i make a
void getmax()
{
if (raw1 > max1)
{
max1 = raw1;
}
}
how would i make that code block loop for 5 seconds?
say i make a
void getmax()
{
if (raw1 > max1)
{
max1 = raw1;
}
}
how would i make that code block loop for 5 seconds?
There is an arduino function called max that returns the max of two values.
This may be what you want to do:
unsigned long start = millis();
while (millis() - start <= 5000) {
max1 = max(analogIn(pin), max1);
}
// here with max1 containin the highest reading after 5 seconds
WOW THANKS!!!!!!!!!!
that completely solves my problem
so max just does it?
built in?
i noticed when i was typing my variables that max turned a different color
unsigned long start = millis();
while (millis() - start <= 5000) {
max1 = max(analogIn(pin), max1);
}
// here with max1 containin the highest reading after 5 seconds
maybe it only runs once for 5 seconds?
I guess
unsigned long start = millis();
is an initializer? Could it be better that way
[b]unsigned long start;
start = millis();[/b]
while (millis() - start <= 5000) {
max1 = max(analogIn(pin), max1);
}
// here with max1 containin the highest reading after 5 seconds
unsigned long start;
start = millis();
results in the same machine code as
unsigned long start = millis();
Use whichever version seems clearer to you.
sorry...
but how does that loop work???
i need to know, to make modifications.
I hope this helps:
This line records the starting milliseconds since the chip was booted. Since the millis() increments automatically, we can use it to determine the relative time for our loop
unsigned long start = millis();
Check that the current milliseconds are less than 5 seconds than the starting millisecond count. If the number of milliseconds is more than 5000, more than 5 seconds have passed and the loop doesn't execute (exits to the next statement after the '}'). If the difference is less than or equal to 5 seconds, it executes the loop.
( To increase the loop time, increase 5000. To decrease, decrease 5000. (1000 milliseconds = 1 second))
while (millis() - start <= 5000) {
This sets max1 to the larger of the analogpin read and the max1 variable. Note that max1 will grow until it hits peak, and never drop until the board is reset (or other code is added)
max1 = max(analogIn(pin), max1);
Go back and re-check the while statement to see if it should continue.
}
// Next statement here.
OKAY PLZ HELP ME WITH THIS ONE
this fits the title so i dont have to make a new topic
i have this code
void loop()
{
line = getline();
while (line == -10)
{
spinleft();
line = getline();
Serial.println(line, DEC);
}
Serial.println(line, DEC);
}
AND IT DOSENT STOP THE LOOP WHEN THE CONDITION ISINT MET
the getline() statment returns -10 when there is no line,
and -5 to +5 when the line is found...
(line follower)
the code prints the line position to serial but when the line is fount (thus changing the variable line to -5 to +5) IT DOSENT STOP THE LOOP
I think it is working, you just have two loops...
void loop(){
while()
{
}
}
Loop is called repeatedly forever - and even if your test condition isn't met - you are printing the same thing.
Try this:
void loop()
{
line = getline();
while (line == -10)
{
spinleft();
line = getline();
Serial.print("WHILE:");
Serial.println(line, DEC);
}
Serial.print("NO:");
Serial.println(line, DEC);
}
You should see something like:
NO: -5
WHILE: -10
WHILE: -10
WHILE: -10
NO: -5
(Depending of course on what you have under the line follower...)
darnit....
it was working all the time
i just had to call my stopmoving() routine in the main loop..
;D