Pages: [1]   Go Down
Author Topic: Loop a Sketch Section  (Read 293 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: November 24, 2012, 02:22:35 pm by ckeyes » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 24, 2012, 02:24:44 pm by AWOL » Logged

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

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

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

Thanks,

Carl
Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just do something like this, for example:

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6904
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 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.
« Last Edit: November 24, 2012, 03:16:04 pm by Arrch » Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this help with the concept?
Code:
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
Logged

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

Does this help with the concept?
Code:
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
Logged

Pages: [1]   Go Up
Jump to: