Pages: [1]   Go Down
Author Topic: Two loops or branches help needed  (Read 1182 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
...
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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");}
}
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   while (millis() < 5000) {
    delay(1000);
   sensorValue = analogRead(1);}
What is this supposed to be doing?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
delay(5000);
sensorValue = analogRead(1);

or
Code:
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.
« Last Edit: February 26, 2012, 08:32:52 pm by thomasbaart » Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Oops. Missed that.
Logged

Pages: [1]   Go Up
Jump to: