1

I have some level of programming background, however never with Arduino's. I've just started hacking at the examples - I'm not even sure what language I'm currently writing. I presume it's C++.

Anyway, I figured I'd try out the pin 13 LED with the BlinkWithoutDelay example. I've got that running fine (which I was very excited about), however I've tried to modify it to blink out SOS in Morse code, but it doesn't appear to be working.

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
//unsigned long interval = 1000;           // interval at which to blink (milliseconds)

// Number of times to blink
const long blinkMax = 10;
// Counter for number of times blinked
unsigned long blinkCount = 0;

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  int loopCount = 0;
  // S O S
  int values[] = {1000, 1000, 1000, 200, 200, 200, 1000, 1000, 100};
  blink(values[loopCount]);
  loopCount = loopCount + 1;
}

void blink(int interval)
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;   
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}

As you can see, I've assigned the time values (1000 for long and 200 for short) to an array and had the loop function call my blink function each time. Every time it loops, it increments and goes to the next value.

I know it's able to pull values from the array fine, as I've tried manually modifying loopCount just before the blink() and it blinks at the specified timeout. I presume the loopCount increment is fine (I also did loopCount++; - no luck).

If someone could point me in the right direction, this would be great. Thanks!

1

Since it's blink without delay, the next loop will happen immediately. Not after the blink function is don't. So loopCount will increment continuously. You don't limit loopCount to 9, so it will after a few milliseconds try to access, e.g. values[12] which doesn't exist. (see @jfpoilpret's comment)

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
int blinkValues[] = {1000, 1000, 1000, 200, 200, 200, 1000, 1000, 100};
int loopCount = 0;


// constants won't change :
//unsigned long interval = 1000;           // interval at which to blink (milliseconds)

// Number of times to blink
const long blinkMax = 10;
// Counter for number of times blinked
unsigned long blinkCount = 0;

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  blink();
}

void blink()
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= blinkValues[loopCount]) {
    previousMillis = currentMillis;   
    loopCount = (loopCount + 1) % 9;
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}

Also note that the blink function will toggle the led, after the interval. So you need to add intervals, for how long the led should be off between flashes. But I leave that for you to figure out.

  • 2
    Your comment about loopCount is incorrect: it will never increment as it is a simple local variable inside loop(). – jfpoilpret Apr 21 '15 at 18:15
  • You're right. I missed that one. – Gerben Apr 21 '15 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.