Go Down

Topic: Loop a Sketch Section (Read 434 times) previous topic - next topic

ckeyes

Nov 24, 2012, 08:08 pm Last Edit: Nov 24, 2012, 08:22 pm by ckeyes Reason: 1
Having tried everything I could find or think of I'm hoping someone might be able to point me in the
right direction to set up some loops. Nothing I'm trying will work or compile.

Just trying to have sections of my test sketch loop when serial characters
are received, i.e. "a", "b" etc.

Here's what I have so far, without the loops of course.

Code: [Select]
void setup() {                
 // initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(13, OUTPUT);
 Serial.begin(9600);  
}



void loop() {
 // read the sensor:
 if (Serial.available() > 0) {
   char inByte = Serial.read();
   Serial.print("Command Received ");
   Serial.print(inByte);
   Serial.print("\n\r");
   
   switch (inByte) {
     
   case 'a':
      //while(Serial.available() == 0)  // while there are no new characters...
   {
   
     digitalWrite(13, HIGH);   // set the LED on
   delay(500);              // wait for a second
    digitalWrite(13, LOW);    // set the LED off
   delay(500);              // wait for a second
   }
     break;
   case 'b':    
     digitalWrite(13, HIGH);   // set the LED on
   delay(1000);              // wait for a second
     digitalWrite(13, LOW);    // set the LED off
   delay(1000);              // wait for a second
     break;
     
   case 'c':    
     digitalWrite(13, HIGH);   // set the LED on
   delay(1500);              // wait for a second
     digitalWrite(13, LOW);    // set the LED off
   delay(1500);              // wait for a second
   
     break;
     
     case 'd':    
     digitalWrite(13, HIGH);   // set the LED on
   delay(2000);              // wait for a second
     digitalWrite(13, LOW);    // set the LED off
   delay(2000);              // wait for a second
     
     break;
   
   default:
     // turn all the LEDs off:
     for (int thisPin = 13; thisPin < 13; thisPin++) {
       digitalWrite(thisPin, LOW);
     }
   }
 }
}



Goal is to have each section loop indefinitely till another serial command is received.

Really appreciate any pointers on this!

Thanks,

Carl

AWOL

#1
Nov 24, 2012, 08:20 pm Last Edit: Nov 24, 2012, 08:24 pm by AWOL Reason: 1
Quote
Goal is to have each section loop indefinitely till another serial command is received.

And how is it behaving now?

Quote
Here's what I have so far, without the loops of course

Apart from the really obvious one called "loop()", of course.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ckeyes

When it receives a command it executes the section fine, just need the section to loop
till it receives another input.

Thanks,

Carl

guix

Just do something like this, for example:

Code: [Select]

bool A_enabled;
bool B_enabled;

void loop()
{
 // check for serial input etc
 ...
 switch ( inByte )
 {
   case 'a' :
   {
     A_enabled = !A_enabled; // invert this variable, so if you retype 'a' later it will be set to false
     B_enabled = false;
   }
   case 'b' :
   {
      B_enabled = !B_enabled;
      A_enabled = false;
   }
 }
 ...
 //end of serial input checks

 if ( A_enabled )
   DoSomethingA();

 else if ( B_enabled )
   DoSomethingB();

 ... etc etc
}

liudr

You need to rewrite your code to do the following logic:

have a state variable to indicate what state the machine is in, such as not received anything, received a, received b, etc. Assign it not received anything state first.

Build a while loop, check for serial in the beginning, decide whether the machine needs to change state.
Then have the switch case to do what a certain state does. Then as long as you are not receiving any new command, the state is kept and the action repeats itself.

BTW, guix's solution is good for only a couple of different states.

Arrch

#5
Nov 24, 2012, 08:56 pm Last Edit: Nov 24, 2012, 09:16 pm by Arrch Reason: 1
Code: [Select]
 if (Serial.available() > 0) {
   char inByte = Serial.read();
   ...
   
   switch (inByte) {
   
   ...
   {
 }


If you want it to continuously run the code, then you shouldn't have the code inside the if (Serial.available() > 0) part. Of course, you also couldn't destroy the inByte after each iteration of the loop() function, so it would need to be static or global.

billroy

Does this help with the concept?
Code: [Select]

case 'a':
while (!Serial.available()) {
digitalWrite(13, HIGH);   // set the LED on
delay(500);              // wait for a second
digitalWrite(13, LOW);    // set the LED off
delay(500);              // wait for a second
}
break;



-br

ckeyes


Does this help with the concept?
Code: [Select]

case 'a':
while (!Serial.available()) {
digitalWrite(13, HIGH);   // set the LED on
delay(500);              // wait for a second
digitalWrite(13, LOW);    // set the LED off
delay(500);              // wait for a second
}
break;



-br



Thanks a bunch everyone. This one seems to work perfectly! Now on to the fun stuff of getting
my LPD8806's programmed.

Carl

Go Up