0

Good evening,

I am writing Arduino code to flash multiple LEDS at different sequences from different pins. Everything is working well, however I cant determine how to add in a Morse Code A flash sequence.

Ideally the sequence would go 1s, on 1s off, 3s on, 3s off. then repeat continuously, this would be for a MO (A) bouy

Below is the format I have used for all other lights and works well with 8 different sequences.

// A is 1s, B is 2.5s, C is 4s, D is 5s, F is 6s, G is 8s, H is 10s , 

#define aLed 2
#define bLed 3
#define cLed 4
#define dLed 5
#define fLed 6
#define gLed 7
#define hLed 8


void setup() {
  pinMode(aLed, OUTPUT); //pin2 Output
  pinMode(bLed, OUTPUT); //pin3 Output
  pinMode(cLed, OUTPUT); //pin4 Output
  pinMode(dLed, OUTPUT); //pin5 Output
  pinMode(fLed, OUTPUT); //pin6 Output
  pinMode(gLed, OUTPUT); //pin7 Output
  pinMode(hLed, OUTPUT); //pin8 Output
 

}

void loop() {
  BlinkA(1000);
  BlinkB(2500);
  BlinkC(4000); 
  BlinkD(5000);
  BlinkF(6000);
  BlinkG(8000);
  BlinkH(10000);
 
}
 void BlinkA (int interval){
   static long prevMill = 0;
   if ((millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(aLed, !digitalRead(aLed));
 } 
}
void BlinkB (int interval){
   static long prevMill = 0;
   if (((long)millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(bLed, !digitalRead(bLed));
  }
 }
  void BlinkC (int interval){
   static long prevMill = 0;
   if ((millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(cLed, !digitalRead(cLed));
 } 
}
 void BlinkD (int interval){
   static long prevMill = 0;
   if ((millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(dLed, !digitalRead(dLed));
 } 
}
 void BlinkF (int interval){
   static long prevMill = 0;
   if ((millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(fLed, !digitalRead(fLed));
 } 
}
 void BlinkG (int interval){
   static long prevMill = 0;
   if ((millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(gLed, !digitalRead(gLed));
 } 
}
 void BlinkH (int interval){
   static long prevMill = 0;
   if ((millis() - prevMill) >= interval){ 
    prevMill = millis(); 
    digitalWrite(hLed, !digitalRead(hLed));
 } 
}

7
  • to format the code listing, put ~~~ on a blank line before the code ... and same on a blank line after the code
    – jsotola
    Nov 18, 2021 at 4:17
  • what is your question?
    – jsotola
    Nov 18, 2021 at 4:25
  • Would this answer be helpful? arduino.stackexchange.com/a/48588/37523
    – VE7JRO
    Nov 18, 2021 at 4:55
  • Re “1s on, 1s off, 3s on, 3s off”: That would be keying at 1.2 words per minute! Nobody, not even a complete beginner, goes that slow. If you want someone to be able to read the “A” before they get bored, you should not go below ≈ 5 WPM (240 ms on, 240 ms off, 720 ms on, 720 ms off). Nov 18, 2021 at 8:54
  • 1

1 Answer 1

2

multiple blinking LEDs at different intervals, (not all periodic)...

I changed your question hopefully with out changing its meaning.

Based on this question, I would suggest using a finite state machine. Assuming you are using differently named "BlankI()" functions for each LED, consider adding state machine features to 1 of these functions to control the "not periodic" LED. Consider a state machine with 4 state one for each action:

  1. LED On for 1 second.
  2. LED Off for 1 second.
  3. LED On for 3 seconds.
  4. LED Off for 3 seconds.

Use a counter (either global or static to preserve its value between calls to that function) to track the states. Use a switch/case coding pattern to set the "prevMill" to the appropriate time delay for the current state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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