5 minute on-off cycles for loop? HELP

Hi all,

Im new to arduino, please excuse my ignorance for this simple question but i dont have much programing experience...

So Ive been working with a simple loop I created that is used to control a digital switch and turn lights on and off rapidly for a sculpture I built...here is a video of the sculpture sculpture in action... http://www.youtube.com/watch?v=oSPfgxPDU9Y Each of the two panels is controlled by a dedicated arduino, one runs a repeating patter(with short delay) the other runs a continuous Random pattern

The lights blink in morse code, I was able to hack another simple morse code-code i found on the internet for this project....but now im trying to cycle the light boxes on and off...and i dont know how to do that.....

ideally I want to make a loop that will run my morse-code sequence for 5 minutes, then turn the lights off for 10 minutes, then on again for 5 min...etc...

Im guessing there should be a way to run my existing loop for 5 minutes, then add a 10 minute delay....but im not sure how to do that at the moment....

any help or suggestions would be greatly appreciated !!!! ~don :D

My current code for one of the 2 circuits( the one with a repeating pattern)...

int pin = 12;

void setup() { pinMode(pin, OUTPUT); }

void loop() { dot(); dot(); dash(); dash(); dash(); dash(); dash(); dash(); dash(); dot(); dash(); dash(); dash(); dash(); dot(); dash(); dash(); dot(); dot(); dot(); dash(); dash(); dash(); dash(); dot(); dot(); dot(); dash(); dash(); dash(); dot(); dot(); dot(); dot(); dash(); dot(); dot(); dot(); dot(); dot(); dash(); dash(); dash(); dot(); dot(); delay(1000); }

void dot() { digitalWrite(pin, HIGH); delay(100); digitalWrite(pin, LOW); delay(100); }

void dash() { digitalWrite(pin, HIGH); delay(400); digitalWrite(pin, LOW); delay(100); }

Dots last 200ms, dashes 500ms plus the 1 second delay at the end of "loop".

So work out how many go into 5 minutes and loop it that many times with a "for" loop.

You've got 21 dots and 23 dashes, so 15.7 seconds, + 1 = 16.7seconds per sequence.

There are 300 seconds in 5 minutes, so run your loop 18 times.

Stick a delay (10 * 60 * 1000) at the end of "loop".

Job done.

AWOL,
THANKS! for the tip, i think you nailed it…but i cant figure out how to use the “for” statement in my sketch…i looked up “for” on the arduino cc website, but I didnt understand how to use it… i also dont know exactly were to put the code in my sketch…

so far the best I could do was this “for” statement:

for (int x=0; x <= 18; x+)

but i dont know how to define my morse code sequence as “x”

…this might be completely wrong, but I would like my sequence to run 18 times…

if there is any way you could tell me where to put the “for” statement on my code, and the 10 minute delay, i would really appreciate it!

Thanks, Don

In this case, you don’t need the “loop variable”, x.

for (x=0;x<18;x++) {
your code goes here and doesn’t use x
}

x is just counting up how many times the morse code sequence is repeated.

You also need the 10 minute delay after the for loop.

10 minutes is 10 x 60 x 1000 = 600,000 milliseconds. Look at delay() but be aware of what the size of the parameter to delay is, and compare to 600,000 (that is a hint to learn about how many bytes variables take up in memory, and what the largest number you can store in those variables could be, and what you do when you need a bigger number);

Hey Dedler

I once made a morse code generator with Arduino - Feel free to use any part of it.

byte text[] = "All your base are belong to us";         // Transmittet text

// Morse code generator for the Arduino
// Transmitted text is placed in the first line
// Tempo sets the speed of a dot etc.
// Morde code was tanken from http://en.wikipedia.org/wiki/Morse_code
// Text is looping and prior to transmitting the active pin blinks rapidly
// Version 1.0, made by Fletcher Chr

int ledPin = 13;                       // Active led pin
int tempo = 500;                       // timing of one dot


void setup () {
 pinMode(ledPin, OUTPUT);
}

void start(){                         // rapid blink at startup
 for(int i = 0; i < 50; i++){
   digitalWrite(ledPin, HIGH);
   delay(25);
   digitalWrite(ledPin, LOW);
   delay(25);
 }
 delay(7 * tempo);
}

void dot() {                          // we need more dots
 digitalWrite(ledPin, HIGH);
 delay(1 * tempo);
 digitalWrite(ledPin, LOW);
 delay(1 * tempo);
}

void dash() {                        // a dash and a splash
 digitalWrite(ledPin, HIGH);
 delay(3 * tempo);
 digitalWrite(ledPin, LOW);
 delay(1 * tempo);
}

void morse(byte letter) {           // time to transmit
//  Serial.println(letter, DEC);
 if (letter == 'a' or letter == 'A') {dot(); dash();}
 if (letter == 'b' or letter == 'B') {dash(); dot(); dot(); dot();}
 if (letter == 'c' or letter == 'C') {dash(); dot(); dash(); dot();}
 if (letter == 'd' or letter == 'D') {dash(); dot(); dot();}
 if (letter == 'e' or letter == 'E') {dot();}
 if (letter == 'f' or letter == 'F') {dot(); dot(); dash(); dot();}
 if (letter == 'g' or letter == 'G') {dash(); dash(); dot();}
 if (letter == 'h' or letter == 'H') {dot(); dot(); dot(); dot();}
 if (letter == 'i' or letter == 'I') {dot(); dot();}
 if (letter == 'j' or letter == 'J') {dot(); dash(); dash(); dash();}
 if (letter == 'k' or letter == 'K') {dash(); dot(); dash();}
 if (letter == 'l' or letter == 'L') {dot(); dash(); dot(); dot();}
 if (letter == 'm' or letter == 'M') {dash(); dash();}
 if (letter == 'n' or letter == 'N') {dash(); dot();}
 if (letter == 'o' or letter == 'O') {dash(); dash(); dash();}
 if (letter == 'p' or letter == 'P') {dot(); dash(); dash(); dot();}
 if (letter == 'q' or letter == 'Q') {dash(); dash(); dot(); dash();}
 if (letter == 'r' or letter == 'R') {dot(); dash(); dot();}
 if (letter == 's' or letter == 'S') {dot(); dot(); dot();}
 if (letter == 't' or letter == 'T') {dash();}
 if (letter == 'u' or letter == 'U') {dot(); dot(); dash();}
 if (letter == 'v' or letter == 'V') {dot(); dot(); dot(); dash();}
 if (letter == 'w' or letter == 'W') {dot(); dash(); dash();}
 if (letter == 'x' or letter == 'X') {dash(); dot(); dot(); dash();}
 if (letter == 'y' or letter == 'Y') {dash(); dot(); dash(); dash();}
 if (letter == 'z' or letter == 'Z') {dash(); dash(); dot(); dot();}
 if (letter == '1') {dot(); dash(); dash(); dash(); dash();}
 if (letter == '2') {dot(); dot(); dash(); dash(); dash();}
 if (letter == '3') {dot(); dot(); dot(); dash(); dash();}
 if (letter == '4') {dot(); dot(); dot(); dot(); dash();}
 if (letter == '5') {dot(); dot(); dot(); dot(); dot();}
 if (letter == '6') {dash(); dot(); dot(); dot(); dot();}
 if (letter == '7') {dash(); dash(); dot(); dot(); dot();}
 if (letter == '8') {dash(); dash(); dash(); dot(); dot();}
 if (letter == '9') {dash(); dash(); dash(); dash(); dot();}
 if (letter == '0') {dash(); dash(); dash(); dash(); dash();}
 if (letter == ' ') {delay(5 * tempo);}                        // This makes 7 * tempo for space

 // and now for some national/special letters
 // values were foundt with Serial.print(letter, DEC)

 if (letter == 166 or letter == 134) {dot(); dash(); dot(); dash();}     // æ/Æ is recognized as 166/134
 if (letter == 184 or letter == 152) {dash(); dash();dash();dot();}      // ø/Ø is recognized as 184/152
 if (letter == 165 or letter == 133) {dot();dash();dash();dot();dash();} // å/Å is recognized as 165/133

 delay(2 * tempo);      // this makes 3 * tempo for letter end, and 7 * tempo for space
}


void loop () {
 start();
 for (int i = 0; i < sizeof(text); i++) {
   morse(text[i]);
 }
}

-Fletcher

Thanks fletcher for the morse code generator...but i already have my morse code figured out...im trying to manipulate the sequence to run for 5 minutes (or about 18 cycles) and then pause for 10 minutes....any help!??!

Thank you AWOL and brtech, I think ive gotten someting to work....its a bit sloppy, as I just repeated the sequence a bunch of times, then put a big delay at the end of it...but it seems to be running well!!! :D :)

Eww.

Take a look at the “blink without delay” example code. You’d probably end up with something like this:

void message()
{
 dot(); dot(); dash(); dash(); dash(); // 2
 delay(interchar);
 dash(); dash(); dash(); dash(); dot(); // 0
 delay(interchar);
 dash(); dash(); dash(); dash(); dot(); // 9
 delay(interchar);
 dash(); dash(); dot(); dot(); dot();
 // etc
 delay(1000);
}

unsigned long ontime = 5 * 60 * 1000;  // 5 minutes.
unsigned long offtime = 10 * 60 * 1000;// 10 minutes.

void loop()
{
  unsigned long starttime = millis();

  while ((millis() - starttime) < ontime) {
    message();  // keep sending message while we're supposed to be on
  }
  delay(offtime);  // then stay off for a while.
}

(I think that’ll work right even when millis() wraps…)

PS: to be correct Morse code, you need additional pause time between each letter, and more than that between words. (as sort-of shown?) I forget the exact times (one dash time, three dash times?) but you should be able to look them up.

Hi again

This should get you home free at the speed of light…

(Untested but compiled)

-Fletcher

byte text[] = "299792458  ";         // Transmittet text

// Morse code generator for the Arduino
// Transmitted text is placed in the first line
// Tempo sets the speed of a dot etc.
// Morde code was tanken from http://en.wikipedia.org/wiki/Morse_code
// Text is looping during 5 min then stops for 10 min
// Version 1.1, made by Fletcher Chr

int ledPin = 12;                       // Active led pin
int tempo = 500;                       // timing of one dot

const unsigned long MIN_5 = 300000;
const unsigned long MIN_10 = 600000;
unsigned long cyclusTime = 0;


void setup () {
 pinMode(ledPin, OUTPUT);
}

void dot() {                          // we need more dots
 digitalWrite(ledPin, HIGH);
 delay(1 * tempo);
 digitalWrite(ledPin, LOW);
 delay(1 * tempo);
}

void dash() {                        // a dash and a splash
 digitalWrite(ledPin, HIGH);
 delay(3 * tempo);
 digitalWrite(ledPin, LOW);
 delay(1 * tempo);
}

void morse(byte letter) {           // time to transmit
//  Serial.println(letter, DEC);
 if (letter == 'a' or letter == 'A') {dot(); dash();}
 if (letter == 'b' or letter == 'B') {dash(); dot(); dot(); dot();}
 if (letter == 'c' or letter == 'C') {dash(); dot(); dash(); dot();}
 if (letter == 'd' or letter == 'D') {dash(); dot(); dot();}
 if (letter == 'e' or letter == 'E') {dot();}
 if (letter == 'f' or letter == 'F') {dot(); dot(); dash(); dot();}
 if (letter == 'g' or letter == 'G') {dash(); dash(); dot();}
 if (letter == 'h' or letter == 'H') {dot(); dot(); dot(); dot();}
 if (letter == 'i' or letter == 'I') {dot(); dot();}
 if (letter == 'j' or letter == 'J') {dot(); dash(); dash(); dash();}
 if (letter == 'k' or letter == 'K') {dash(); dot(); dash();}
 if (letter == 'l' or letter == 'L') {dot(); dash(); dot(); dot();}
 if (letter == 'm' or letter == 'M') {dash(); dash();}
 if (letter == 'n' or letter == 'N') {dash(); dot();}
 if (letter == 'o' or letter == 'O') {dash(); dash(); dash();}
 if (letter == 'p' or letter == 'P') {dot(); dash(); dash(); dot();}
 if (letter == 'q' or letter == 'Q') {dash(); dash(); dot(); dash();}
 if (letter == 'r' or letter == 'R') {dot(); dash(); dot();}
 if (letter == 's' or letter == 'S') {dot(); dot(); dot();}
 if (letter == 't' or letter == 'T') {dash();}
 if (letter == 'u' or letter == 'U') {dot(); dot(); dash();}
 if (letter == 'v' or letter == 'V') {dot(); dot(); dot(); dash();}
 if (letter == 'w' or letter == 'W') {dot(); dash(); dash();}
 if (letter == 'x' or letter == 'X') {dash(); dot(); dot(); dash();}
 if (letter == 'y' or letter == 'Y') {dash(); dot(); dash(); dash();}
 if (letter == 'z' or letter == 'Z') {dash(); dash(); dot(); dot();}
 if (letter == '1') {dot(); dash(); dash(); dash(); dash();}
 if (letter == '2') {dot(); dot(); dash(); dash(); dash();}
 if (letter == '3') {dot(); dot(); dot(); dash(); dash();}
 if (letter == '4') {dot(); dot(); dot(); dot(); dash();}
 if (letter == '5') {dot(); dot(); dot(); dot(); dot();}
 if (letter == '6') {dash(); dot(); dot(); dot(); dot();}
 if (letter == '7') {dash(); dash(); dot(); dot(); dot();}
 if (letter == '8') {dash(); dash(); dash(); dot(); dot();}
 if (letter == '9') {dash(); dash(); dash(); dash(); dot();}
 if (letter == '0') {dash(); dash(); dash(); dash(); dash();}
 if (letter == ' ') {delay(5 * tempo);}                        // This makes 7 * tempo for space

 // and now for some national/special letters
 // values were foundt with Serial.print(letter, DEC)

 if (letter == 166 or letter == 134) {dot(); dash(); dot(); dash();}     // æ/Æ is recognized as 166/134
 if (letter == 184 or letter == 152) {dash(); dash();dash();dot();}      // ø/Ø is recognized as 184/152
 if (letter == 165 or letter == 133) {dot();dash();dash();dot();dash();} // å/Å is recognized as 165/133

 delay(2 * tempo);      // this makes 3 * tempo for letter end, and 7 * tempo for space
}


void loop () {
  if (millis() <= cyclusTime + MIN_5){
    for (int i = 0; i < sizeof(text) - 1; i++) {
      morse(text[i]);
    }
  }
  if (millis() >= (cyclusTime + MIN_5 + MIN_10)){
    cyclusTime = millis();
  } 
 }

Update:
Change to “sizeof(text) - 1”
Compiled and tested.

This:

for (int i = 0; i < sizeof(text); i++)

would be better written:

for (int i = 0; i < strlen(text); i++)

and you’d have to change the type of “text”, to “char” from “byte”.

“sizeof(text)” includes the terminating zero.

[edit]Oops - if you defined “text” as a “char” array, you’d have problems with the digraphs.
My bad.
So, “sizeof(text) - 1”[/edit]

Slightly more compact morse sender (untested, but compiled):

byte text[] = "The quick brown fox jumps over the lazy dog";    // Transmitted text ('byte' allows chars above 127)

typedef struct {
  const byte length:3;
  const byte code:5;
} morseCode;

const morseCode morseLookup [] = {{2, B01},   {4, B1000}, {4, B1010}, {3, B100},  {1, B0},    {4, B0010},
                                  {3, B110},  {4, B0000}, {2, B00},   {4, B0111}, {3, B101},  {4, B0100}, 
                                  {2, B11},   {2, B10},   {3, B111},  {4, B0110}, {4, B1101}, {3, B010},
                                  {3, B000},  {1, B1},    {3, B001},  {4, B0001}, {3, B011},  {4, B1001},
                                  {4, B1011}, {4, B1100},
                                  // now digits 0..9
                                  {5, B11111}, {5, B01111}, {5, B00111}, {5, B00011}, {5, B00001},
                                  {5, B00000}, {5, B10000}, {5, B11000}, {5, B11100}, {5, B11110},
                                  //now digraphs
                                  {4, B0101}, // 166 / 134
                                  {4, B1110}, // 184 / 152
                                  {5, B01101} // 165 / 133                                          
                          };
#define DIGIT_START   ('Z' - 'A')
#define DIGRAPH_START (DIGIT_START + ('9'-'0'))
                          
const byte ledPin = 13;

unsigned int tempo = 500;      // single dot time - could be varied

void setup () 
{
  pinMode(ledPin, OUTPUT);
}

void loop () 
{
  for (int i = 0; text [i]; i++) {
    morse(text[i]);
  }
}

void morse(byte letter) 
{
  int index = -1;
  if (letter == ' ') {
    delay(2 * tempo);
    return;
  } 
  if (letter >= 'a' && letter <= 'z') {
    letter -= 0x20;  // convert to upper case
  }  
  if (letter >= 'A' && letter <= 'Z') {
    index = letter - 'A';
  }
  if (letter >= '0' && letter <= '9') {
    index = DIGIT_START + (letter - '0');
  }
  if (letter == 134 || letter == 166) {
    index = DIGRAPH_START;
  }
  if (letter == 152 || letter == 184) {
    index = DIGRAPH_START + 1;
  }
  if (letter == 133 || letter == 165) {
    index = DIGRAPH_START + 2;
  }
  if (index >= 0) {
    const morseCode myCode = morseLookup [index];
    byte len               = myCode.length;
    const byte dashNotDot  = myCode.code;
    byte mask              = 1 << len;
    while (len--) {
      mask >>= 1;
      code (dashNotDot & mask);
    }
  }
}

void code (byte dashNotDot)
{
  digitalWrite (ledPin, HIGH);
  delay (tempo);
  if (dashNotDot) {
    delay (2 * tempo);
  }  
  digitalWrite (ledPin, LOW); 
  delay (tempo);
}

Hi Groove

You are right about the "sizeof(test)-1". The "out of bounch" of the array have been blank, therefore I did not notice any misbehave during testing.

Code in post above have been updated.

-Fletcher

WOW! THANKS SO MUCH EVERY ONE!!!

I really appreciate you all taking the time to help me out with this, ive learned a lot just studying your excellent examples!
:smiley: :smiley: :smiley: