Go Down

Topic: Two loops or branches help needed (Read 1 time) previous topic - next topic

Lsnyman

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?

Any help would be appreciated..

dxw00d

You can't have two loop() functions, but you can write and call other functions.

Code: [Select]
...
void loop()
{
  ...
  If (sensorvalue < 500)
  {
    function1();
  }
  else
  {
    function2();
  }
}

void function1()
{
  // write first function here
}

void function2()
{
  // write second function here
}

PaulS

Quote
I have 2 distinct loops or sets of code

A block of code/function is one thing. A loop is something else, entirely. Which do you have?

Quote
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.

Quote
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.

Quote
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?

Lsnyman

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.



dxw00d

Code: [Select]
...
void loop()
{
  ...
  If (sensorvalue < 500)
  {
    function1();
  }
  else
  {
    function2();
  }
}

void function1()
{
  while (1)
  {
  // write first function here
  }
}

void function2()
{
  while (1)
  {
  // write second function here
  }
}


Would achieve your requirement.

PaulS

Code: [Select]
  If (sensorvalue < 500)
If? In C? Never worked for me...

Lsnyman

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

Code: [Select]
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");}
}

Lsnyman

Nevermind
I just saw my own stupidity.
I swapped <and>
Duhh


PaulS

Code: [Select]
   while (millis() < 5000) {
    delay(1000);
   sensorValue = analogRead(1);}

What is this supposed to be doing?


#9
Feb 27, 2012, 02:31 am Last Edit: Feb 27, 2012, 02:32 am by thomasbaart Reason: 1
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
Code: [Select]
delay(5000);
sensorValue = analogRead(1);


or
Code: [Select]
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.

Lsnyman

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

dxw00d


Code: [Select]
  If (sensorvalue < 500)
If? In C? Never worked for me...


Oops. Missed that.

Go Up