Help
cancel
Showing results for
Did you mean:
Highlighted
Copilot Lvl 2
Message 1 of 2

## Emergency code help!

Solved! Go to Solution.

Hi everyone,

I desperately need this code to work before Saturday. It's morse code for a 5 metre LED strip (about 150 LEDs) and  currently, the light light up 1 by 1, rather than all at once, which is ruining the morse code. Please please help!

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 120

//morse_code by LED

//Define the LED Pin
#define DATA_PIN_1 5 //speaker 1
#define DATA_PIN_2 6 //speaker 2
//Define unit length in ms
#define UNIT_LENGTH 200 //this is the time between the dots and the dashes

String morseWord1 = "";
String morseWord2 = "";

// Define the array of leds
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];

//Build a struct with the morse code mapping
static const struct {const char letter, *code;} MorseMap[] =
{
{ 'A', ".-" },
{ 'B', "-..." },
{ 'C', "-.-." },
{ 'D', "-.." },
{ 'E', "." },
{ 'F', "..-." },
{ 'G', "--." },
{ 'H', "...." },
{ 'I', ".." },
{ 'J', ".---" },
{ 'K', ".-.-" },
{ 'L', ".-.." },
{ 'M', "--" },
{ 'N', "-." },
{ 'O', "---" },
{ 'P', ".--." },
{ 'Q', "--.-" },
{ 'R', ".-." },
{ 'S', "..." },
{ 'T', "-" },
{ 'U', "..-" },
{ 'V', "...-" },
{ 'W', ".--" },
{ 'X', "-..-" },
{ 'Y', "-.--" },
{ 'Z', "--.." },
{ ' ', " " }, //Gap between word, seven units

{ '1', ".----" },
{ '2', "..---" },
{ '3', "...--" },
{ '4', "....-" },
{ '5', "....." },
{ '6', "-...." },
{ '7', "--..." },
{ '8', "---.." },
{ '9', "----." },
{ '0', "-----" },

{ '.', "·–·–·–" },
{ ',', "--..--" },
{ '?', "..--.." },
{ '!', "-.-.--" },
{ ':', "---..." },
{ ';', "-.-.-." },
{ '(', "-.--." },
{ ')', "-.--.-" },
{ '"', ".-..-." },
{ '@', ".--.-." },
{ '&', ".-..." },
};

void setup()
{
Serial.begin(9600); //for debugging issues
//leds1 = speaker 1 = pin 5
//leds2 = speaker 2 = pin 6

}

void loop()
{
// blink1 = speaker 1 = pin 5
// blink2 = speaker 2 = pin 6
// odd numbers in morseWords use blink1 function - speaker1
// even numbers in morseWors use blink2 function - speaker2

//Initialization delat - 1min
//Serial.println("Initialization");
//delay(10000);

//Sentence 1
morseWord1 = encode ( "i can i i have everything else" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord1);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 2
morseWord2 = encode ( "years have a year to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "to me to me to me to me to me" );
Serial.println(morseWord2);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 3
morseWord1 = encode ( "you i everything else" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord1);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 4
morseWord2 = encode ( "years have years to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "to me to me to me" );
Serial.println(morseWord2);
morseWord2 = encode ( "to me to" );
Serial.println(morseWord2);
morseWord2 = encode ( "me to me" );
Serial.println(morseWord2);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 5

morseWord1 = encode ( "i i can i i i everything else" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord1);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 6
morseWord2 = encode ( "years have years" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "to me to me a thousand" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "years to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "to me to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 7
morseWord1 = encode ( "i" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord1);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 8
morseWord2 = encode ( "years have a thousand" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "years to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "a year a thousand" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "years to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "to me to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "to me to me to me to" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 9
morseWord1 = encode ( "you i i i i i everything else" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord1);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(2800);

//Sentence 10
morseWord2 = encode ( "Years have zero to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "in a thousand years" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "i could not understand what" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "you are to me to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
morseWord2 = encode ( "to me to me" );
// Show into the serial monitor the morse code of the previous sentense
Serial.println(morseWord2);
delay(200); // if you uncommend this it ill add 100ms delay after the first speaker

delay(1000);

Serial.println("The End");
while(1)
{

}

}

/*
{
for (int i=0; i< NUM_LEDS; i++)
{
leds2[i] = CRGB::Red;
FastLED.show();
}
}
*/

{
for(int i=0; i<=morseWord.length(); i++)
{
switch( morseWord[i] )
{
case '.': //dit
// Turn the LED on, then pause
for (int i=0; i <NUM_LEDS; i++)
{
leds2[i] = CRGB::Blue;
FastLED.show();
}
delay( UNIT_LENGTH );

// Now turn the LED off, then pause
for (int i=0; i <NUM_LEDS; i++)
{
leds2[i] = CRGB::Black;
FastLED.show();
//delay(500);
}
delay( UNIT_LENGTH );
break;

case '-': //dah
// Turn the LED on, then pause
for (int i=0; i <NUM_LEDS; i++)
{
leds2[i] = CRGB::Blue;
FastLED.show();
}
delay( UNIT_LENGTH*3 );

// Now turn the LED off, then pause
for (int i=0; i <NUM_LEDS; i++)
{
leds2[i] = CRGB::Black;
FastLED.show();
//delay(500);
}
delay( UNIT_LENGTH *3);
break;

case ' ': //gap
delay( UNIT_LENGTH );
}
}
}

{
for(int i=0; i<=morseWord.length(); i++)
{
switch( morseWord[i] )
{
case '.': //dit
// Turn the LED on, then pause
for (int i=0; i <NUM_LEDS; i++)
{
leds1[i] = CRGB::Blue;
FastLED.show();
}
delay( UNIT_LENGTH );

// Now turn the LED off, then pause
for (int i=0; i <NUM_LEDS; i++)
{
leds1[i] = CRGB::Black;
FastLED.show();
//delay(500);
}
delay( UNIT_LENGTH );
break;

case '-': //dah
// Turn the LED on, then pause
for (int i=0; i<NUM_LEDS; i++)
{
leds1[i] = CRGB::Blue;
FastLED.show();
}
delay( UNIT_LENGTH*3 );

// Now turn the LED off, then pause
for (int i=0; i<NUM_LEDS; i++)
{
leds1[i] = CRGB::Black;
FastLED.show();
//delay(500);
}
delay( UNIT_LENGTH *3);
break;

case ' ': //gap
delay( UNIT_LENGTH );
}
}

}

String encode(const char *string)
{
size_t i, j;
String morseWord = "";

for( i = 0; string[i]; ++i )
{
for( j = 0; j < sizeof MorseMap / sizeof *MorseMap; ++j )
{
if( toupper(string[i]) == MorseMap[j].letter )
{
morseWord += MorseMap[j].code;
break;
}
}
morseWord += " "; //Add tailing space to seperate the chars
}

return morseWord;
}

String decode(String morse)
{
String msg = "";

int lastPos = 0;
int pos = morse.indexOf(' ');
while( lastPos <= morse.lastIndexOf(' ') )
{
for( int i = 0; i < sizeof MorseMap / sizeof *MorseMap; ++i )
{
if( morse.substring(lastPos, pos) == MorseMap[i].code )
{
msg += MorseMap[i].letter;
}
}

lastPos = pos+1;
pos = morse.indexOf(' ', lastPos);

// Handle white-spaces between words (7 spaces)
while( morse[lastPos] == ' ' && morse[pos+1] == ' ' )
{
pos ++;
}
}

return msg;
}

Solution
Commander Lvl 1
Message 2 of 2

## Re: Emergency code help!

Move the FastLED.Show() call outside the loop.

Right now the code is setting one LED then showing it: i.e. one-by-one. You need to set ALL the LEDs to the desired values, then call Show().

For example, change:

```for (int i=0; i <NUM_LEDS; i++)
{
leds1[i] = CRGB::Blue;
FastLED.show();
}```

to this:

```for (int i=0; i <NUM_LEDS; i++)
{
leds1[i] = CRGB::Blue;
}
FastLED.show();```

I trust this is neither homework nor a paid assignment ...

New solutions

Top Kudoed Posts