Hello,
First of all I am new to Arduino and this is the first actual project that I am doing (other than blinking an LED)!
I am trying to use my arduino and a PowerSwitch Tail to control a desk lamp so that when I type a 'H' and press enter into the serial monitor it will turn on and when I type a 'L' and press enter it will turn off. I have written my code below.
My actual problem is that when I type 'H' in the serial monitor and press enter, the indicator LED turns on and I hear the relay clicking but the lamp plugged into the other end isn't turning on. And when I type 'L' and press enter the relay clicks again and the LED turns off. I am using an Arduino UNO and the PowerSwitch Tail 2 240V kit. I have the +in of the PowerSwitch Tail to the 5V pin on the Arduino, I have the -in to the signal pin on the Arduino (pin 11) and the ground on the PowerSwitch tail to the ground pin on the Arduino.
I hope one of you can help me solve this problem.
Thanks, Matt
int currentState;
int prevState;
int powerPin = 11;
void setup() {
Serial.begin(9600);
pinMode(powerPin, OUTPUT);
digitalWrite(powerPin, HIGH); // Cut the power when it starts up
if(Serial.available() > 0) { // If any serial data is avaialable:
prevState = Serial.read(); // Set it as prevState
}
Serial.println("Type 'H' and press enter to turn on the power and type 'L' and press enter to turn off the power.");
Serial.println(" ");
Serial.println(" ");
}
void loop() {
currentState = Serial.read(); // Now set the currentState
if (currentState != prevState) { // If a message has been sent:
if(currentState == 76) { // And it is a 'L':
digitalWrite(powerPin, HIGH); // Send the signal
Serial.println("Power is off"); // And say that the power is off
Serial.println(" ");
}
if(currentState == 72) { // Or if it is a 'H':
digitalWrite(powerPin, LOW); // Don's send the signal
Serial.println("Power is on");// And say that the power is on
Serial.println(" ");
}
}
prevState = currentState; // Reset the state so that it works the next time through the loop