# 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={dot,dash};
int letB={dash,dot,dot,dot};
int letC={dash,dot,dash,dot};
int letD={dash,dot,dot};
int letE={dot};
int letF={dot,dot,dash,dot};
int letG={dash,dash,dot};
int letH={dot,dot,dot,dot};
int letI={dot,dot};
int letJ={dot,dash,dash,dash};
int letK={dash,dot,dash};
int letL={dot,dash,dot,dot};
int letM={dash,dash};
int letN={dash,dot};
int letO={dash,dash,dash};
int letP={dot,dash,dash,dot};
int letQ={dash,dash,dot,dash};
int letR={dot,dash,dot};
int letS={dot,dot,dot};
int letT={dash};
int letU={dot,dot,dash};
int letV={dot,dot,dot,dash};
int letW={dot,dash,dash};
int letX={dash,dot,dot,dash};
int letY={dash,dot,dash,dash};
int letZ={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 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 