Controlling Sketches with Serial Commands (Mac)

Complete newbie here, in fact I haven't received my Arduino or LPD8806's yet, but was hoping to get a head start on what may be possible.

Ideally I'd like to be able to have an AppleScript, thru whatever means, send the Arduino a serial character/command to trigger a sketch. Haven't gathered yet if the Arduino can store multiple sketches that can be individually triggered, or if it holds just one that can be segmented and access the segments.

Any help greatly appreciated!

Thanks,

Carl

Haven't gathered yet if the Arduino can store multiple sketches that can be individually triggered, or if it holds just one that can be segmented and access the segments.

Just a single sketch. The serial line can be used to choose what functions in the sketch you execute if you write the sketch right but it is just one sketch.

Thanks, very good to know. Hoping my UNO will hold all I need it to do in one sketch. Don't suppose there would be a bit of sample code showing how a sketch is setup with the serial inputs to trigger different sections?

My application will be to control 10m of LPD8806's with many different patterns triggered by an AppleScript sending, possibly thru Terminal, the serial character to select the portion of the sketch for that particular effect sequence.

Carl

There is an example here using the switch / case structure http://arduino.cc/en/Tutorial/SwitchCase2

For sending data to the arduino from apple script use serialPort X, you can find that here:- http://alteredhorizons.infiniteserve.com/home/MacSoft.html

Excellent. Thanks. This should be fun.

Carl

Might someone have a sketch that shows how a it can be broken up into sections that can be started using a serial character/command to trigger each section?

For example if the serial command was 1 then the led would blink once etc.

Thanks,

Carl

Might someone have a sketch that shows how a it can be broken up into sections

That is what the switch case does, is it not?

Just replace the turn on an LED with your own function.

Doh, now there's a senior moment. That script is exactly what I was looking for.

Many thanks!

Carl

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



void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.  
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For 
    // example 'a' = 97, 'b' = 98, and so forth:

    switch (inByte) {
    case 'a':    
      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 '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
      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(1000);              // wait for a second
      digitalWrite(13, LOW);    // set the LED off
    delay(1000);              // wait for a second
      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
      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;
    
    default:
      // turn all the LEDs off:
      for (int thisPin = 13; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    } 
  }
}

Just trying to get the LED to blink once on an “a” serial input, twice on a “b” input etc.
The Uno is responding to the serial input as the RX LED flashes when I send an input but no go on the blink.

Any help getting this to work?

Thanks,

Carl

As you have written it it will only do the stuff in the switch section once when a byte arrives from the serial port. Is that what you find? Do you want it to continue to do the stuff even when nothing new has been received?

If so then take the switch statements outside the when the serial stuff is tested.

Note that as this is then in the loop one flash and two flashes will look the same because for one flash repeated just looks like two flashes repeating.

What's happening is I only see the RX led blink that it has received the serial input. The led on pin 13, built in led, does nothing.

The goal is to have the led blink just once on the "a" input, twice on the "b" input etc. Any ideas?

Thanks,

Carl

change:- int inByte = Serial.read(); to char inByte = Serial.read();

Tried some variations of this:

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



void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.  
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For 
    // example 'a' = 97, 'b' = 98, and so forth:

    switch (inByte) {
    case '1':    
      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 '2':    
      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
      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 '3':    
      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
      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
      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;
    
    default:
      // turn all the LEDs off:
      for (int thisPin = 13; thisPin < 13; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    } 
  }
}

Still no go. Curious about the last bit of code:

default:
// turn all the LEDs off:
for (int thisPin = 13; thisPin < 13; thisPin++) {
digitalWrite(thisPin, LOW);

Is that correct for the internal led?

Thanks,

Carl

You are missing a :-
Serial.begin(9600);
in the setup();

for (int thisPin = 13; thisPin < 13; thisPin++) {

For all values of thisPin, starting at 13, while thisPin is less than 13, do this, then increment thisPin by 1.

Hmmm, no, I don’t think that will loop many times.

You see that whole big area below the input field in the Serial Monitor? Any data sent to the serial port using Serial.print() or Serial.println() will appear there. Try printing some stuff…

Bingo. Plugged in the Serial.begin(9600); and all works fine. Off and running now!

Thanks a bunch,

Carl