Powering an outlet to power Christmas lights.

Hello. I am brand new to Arduino and electrical coding, etc. My work desk is extremely decked out for Christmas, and my boss jokingly said now I just needed my Christmas lights to blink a Christmas carol in morse code - so I decided to take on the challenge. I tried to do a lot of Googling, and I found some youtube videos for the code as well as some websites about creating an outlet controlled by Arduino. I am having issues with the lights blinking. I’m not sure if it is the code or something else. The LED on the Arduino board as well as on the relay blink…but the actual light strand does not.

For physical setup, see attached document for a visual reference. I attached a power cord to an outlet, with the ground and neutral wires going directly to the outlet, and the hot wire going into C on the relay and then out of NO to the outlet. From the relay, I connected the “S” to Pin8, “+” to 5V, and “-” to Gnd on the Arduino board. When all was set and done, I connected power cord that was attached to the outlet into the wall as well as attached a power cord to the Arduino board.

For my code:

int ledPin = 8; //value is the output pin the led is connected to
int dot = 2000; //value is the delay time - how long light is on or off
int dash = dot*3;

//define alpha variables as arrays
int letA[2]={dot,dash};
int letB[4]={dash,dot,dot,dot};
int letC[4]={dash,dot,dash,dot};
int letD[3]={dash,dot,dot};
int letE[1]={dot};
int letF[4]={dot,dot,dash,dot};
int letG[3]={dash,dash,dot};
int letH[4]={dot,dot,dot,dot};
int letI[2]={dot,dot};
int letJ[4]={dot,dash,dash,dash};
int letK[3]={dash,dot,dash};
int letL[4]={dot,dash,dot,dot};
int letM[2]={dash,dash};
int letN[2]={dash,dot};
int letO[3]={dash,dash,dash};
int letP[4]={dot,dash,dash,dot};
int letQ[4]={dash,dash,dot,dash};
int letR[3]={dot,dash,dot};
int letS[3]={dot,dot,dot};
int letT[1]={dash};
int letU[3]={dot,dot,dash};
int letV[4]={dot,dot,dot,dash};
int letW[3]={dot,dash,dash};
int letX[4]={dash,dot,dot,dash};
int letY[4]={dash,dot,dash,dash};
int letZ[4]={dash,dash,dot,dot};

//what you want your message to be
String message="hello";

//initialize setup - what you want to run once
void setup()
{
    pinMode(ledPin, OUTPUT);
    message.toUpperCase();
}

//what you want it to do - what you want to run more than once
void loop()
{  
  for (int i=0; i < message.length(); i++)   //for every character in the message
    {
        if (message[i]=='A'){letter(letA,2);} //call function 'letter'
        else if (message[i]=='B'){letter(letB,4);}
        else if (message[i]=='C'){letter(letC,4);}
        else if (message[i]=='D'){letter(letD,3);}
        else if (message[i]=='E'){letter(letE,1);}
        else if (message[i]=='F'){letter(letF,4);}
        else if (message[i]=='G'){letter(letG,3);}
        else if (message[i]=='H'){letter(letH,4);}
        else if (message[i]=='I'){letter(letI,2);}
        else if (message[i]=='J'){letter(letJ,4);}
        else if (message[i]=='K'){letter(letK,3);}
        else if (message[i]=='L'){letter(letL,4);}
        else if (message[i]=='M'){letter(letM,2);}
        else if (message[i]=='N'){letter(letN,2);}
        else if (message[i]=='O'){letter(letO,3);}
        else if (message[i]=='P'){letter(letP,4);}
        else if (message[i]=='Q'){letter(letQ,4);}
        else if (message[i]=='R'){letter(letR,3);}
        else if (message[i]=='S'){letter(letS,3);}
        else if (message[i]=='T'){letter(letT,1);}
        else if (message[i]=='U'){letter(letU,3);}
        else if (message[i]=='V'){letter(letV,4);}
        else if (message[i]=='W'){letter(letW,3);}
        else if (message[i]=='X'){letter(letX,4);}
        else if (message[i]=='Y'){letter(letY,4);}
        else if (message[i]=='Z'){letter(letZ,4);}
        else if (message[i]==' '){delay(dot*4);}  //or delay if a space
    }
}

//Function
void letter(int *, int array_size) //function name = letter, * is for inputting any variable, size goes with variable array size
{
    for(int i=0; i < array_size; i++)  //this is saying to cycle thru all dots/dashes in the alpha variable
    {
        digitalWrite(ledPin, HIGH); //turn light on
        delay(i); //stay on for this length
        digitalWrite(ledPin, LOW); //turn light off
        delay(dot); //stay off for this length...time between dots/dashes of same letter is equal to one dot
    }
    delay(dot*2); //after every letter, you must pause the time of 3 dots, but one dot was at end of for loop so only pause 2 more
}

No matter if I had the hot wire from the outlet attached to NO or NC, it seemed that the string of lights would only turn on if the first digitalWrite was LOW, which was opposite of what I read should be the case. And in either case, the string just turns on…doesn’t turn off or blink :frowning:

Thanks for any help in advance.

Arduino Outlet.doc (261 KB)

       delay(i); //stay on for this length

i is your index variable, not a time in milliseconds.

Thank you.

I fixed

//Function
void letter(int *, int array_size)
...
 delay(i); //stay on for this length

to be

//Function
void letter(int *let, int array_size)
...
delay(let[i])

Now it all works :slight_smile: