Go Down

Topic: Loops (Read 90 times) previous topic - next topic

BB4DAWIN

Dec 03, 2014, 05:56 am Last Edit: Dec 03, 2014, 05:59 am by BB4DAWIN
I am writing a program where the beginning statement is a conditional statement to initiate a loop...is there a code that will only check this and not return to it again...?

In other words, is there a way to tell arduino to initialize a program based on an input but then change that input in the program itslef?

another possibility is:

Is there a way to have a conditional statement for an input that will last a certain amount of time?

econjack

If I read the question correctly, sure. For example:

Code: [Select]

int flag1;

void setup() {
   flag1 = InitializeSensor1();  // We assume the function returns an int
   if (flag1 != 0) {
      flag1 = 100;              // Where 100 might mean something in loop()
   } else  {
      flag1 = 0;                // Perhaps the sensor is bad
   }
   // rest of setup()
}
     


The call to InitilizeSensor1() sets the value of flag1 to some value that makes sense for the program or 0 if the sensor is not working. Now you can use flag1 in loop() is the sensor is "good" or bypass it if it's "bad".

BB4DAWIN

Thank you for the quick response, I guess I should have given some context...

I have four input switches for motor control, I want the program to start if one of them is detected as HIGH if not nothing should happen...so after setting the setup() how would I call it in loop() ?

Robin2

I think the intent behind @econjack's example is to test your motor settings in setup() and set a flag accordingly.

Then the code in loop() could be

Code: [Select]
void loop() {
    if (startOK == false) {
       return;
    }
   // rest of your code for when startOK is true
}


...R

Grumpy_Mike

The other way to do it would be to have a while loop in the start up function that only exits when the start condition is met.

BB4DAWIN

How would that work?....Also, could it be possible to use a SwitchCase program for this scenario?

Robin2

How would that work?....Also, could it be possible to use a SwitchCase program for this scenario?
You have been given two suggestions. Which one are you referring to?

What are you thinking of doing with Switch Case?
I thought you just wanted a simple GO / NO-GO test to start your program (or not).

...R

Grumpy_Mike

How would that work?
What do you mean?
It works by stopping the program exiting the start up function until you are ready.

Do you mean how would you code this? Well it is a while loop:-
http://arduino.cc/en/Tutorial/WhileLoop

You test for your start conditions in it.

Go Up