Go Down

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

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?


thomasbaart

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

Go Up