I need help again. I have 2 distinct loops or sets of code to run depending on the initial value of a sensor. So
If sensorvalue < 500 perform program loop 1
If sensorvalue > 500 perform program loop 2
Once it starts executing a loop it must never return. The 2 loops contain different data sets and multiple if statements, so using a main IF sensorvalue decision doent work.
i am thinking that a case statement is suited but I am confused by the need for a Break, as I dontbwant to break out of the loop once entered.
Can I have multiple void loop(), and how would I enter each one?
You can't have two loop() functions, but you can write and call other functions.
...
void loop()
{
...
If (sensorvalue < 500)
{
function1();
}
else
{
function2();
}
}
void function1()
{
// write first function here
}
void function2()
{
// write second function here
}
A block of code/function is one thing. A loop is something else, entirely. Which do you have?
Once it starts executing a loop it must never return. The 2 loops contain different data sets and multiple if statements, so using a main IF sensorvalue decision doent work.
Again, a terminology issue. If you want one thing to happen forever if on reset the sensor reports one thing, and something else to happen if the sensor reports something else, then simply don't read the sensor again.
i am thinking that a case statement is suited but I am confused by the need for a Break, as I dontbwant to break out of the loop once entered.
A switch statement is just a shorthand notation for a series of if/else if statements. If the it/else if construct won't work, then switch won't either.
Can I have multiple void loop(), and how would I enter each one?
No, but you can have multiple functions that never return that appear to accomplish the same thing.
But, really, why do you want to do something forever based on a condition at reset? If the condition changes, shouldn't the Arduino's actions?
Ok lets say the input is just a toggle switch. On startup is its in position A then it executes one set of code and vice versa..
My 2 sets of code work individually, but cant get them to live together.
Once the program enters that block of code, it must loop continuously within that subloop. Only on reset or power up does it go to the void setup() again.
...
void loop()
{
...
If (sensorvalue < 500)
{
function1();
}
else
{
function2();
}
}
void function1()
{
while (1)
{
// write first function here
}
}
void function2()
{
while (1)
{
// write second function here
}
}
Thanks, but it seems to behave the same, it reaches the fuctions but does not execute any IF or WHILE statements in that function AND it does not stay in the loop, it goes back again to void loop()
In the following code, I test to see if the function call works but it does not execute the WHILE line.
It should print
650 (sensorValue) once only
reachedbig
big
big
big etc
instead it outputs
650
reached big
650
reached big
650
reached big
etc
int sensorValue = 0;
void setup()
{
// setup serial - diagnostics - port
Serial.begin(9600);
analogReference(INTERNAL);
}
void loop()
{
while (millis() < 5000) {
delay(1000);
sensorValue = analogRead(1);}
Serial.println(analogRead(1));
if (sensorValue < 450){
small();
}
else {
big();
}
}
void big()
{
Serial.println("reached big"); // check to see if program reached this point
while (sensorValue < 450)
{
Serial.println("big");}//if program reaches this point it should print "big"
}
void small()
{
Serial.println("reached small"); // check to see if program reached this point
while (sensorValue > 450) //if program reaches this point it should print "small"
{
Serial.println("small");}
}
I'd guess that he wants to "debounce" the sensor readings, storing the average of what looks like four readings into sensorValue. Right now it just delays the whole program.
I'd either say
delay(5000);
sensorValue = analogRead(1);
or
int sensorValueTotal = 0;
for(int i = 0; i < 4 i++) {
delay(1000);
sensorValueTotal += analogRead(1);
}
int averageReading = sensorValueTotal / 4;
and then use averageReading to compare against in the if statements.
There is actually more code during the millis time, an If statement makes a decision for the rest of the program.
Yes the first delay 91000) is to let the sensor settle.
Thanks