const int relayPin = 2; //the base of the transistor attach to
int comdata = 0;
void setup()
{
pinMode(relayPin, OUTPUT); //initialize the LED as output
Serial.begin(9600); // start serial port at 9600 bps:
while (! Serial);
Serial.println("Please input your command to control this Lamp:"); //print message on serial monitor
}
void loop()
{
//read string from serial monitor
if(Serial.available()) // check if data has been sent from the computer
{ comdata= Serial.read();
if(comdata == 49)
{
digitalWrite(relayPin, HIGH);//turn the lamp on
Serial.println("light is on");
}
else if(comdata == 48)
{
digitalWrite(relayPin, LOW);//turn the lamp off
Serial.println("light is off");
}
}
}