char val; // variable to receive data from the serial port
const byte Relay1 = 2; // LED connected to pin 2 (on-board LED)
const byte Relay2 = 3;
void setup()
{
pinMode(Relay1, OUTPUT); // pin 2 () as OUTPUT
pinMode(Relay2, OUTPUT); // pin 3 (on-board LED) as OUTPUT
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
Serial.begin(9600); // start serial communication at 115200bps
}
void loop()
{
if ( Serial.available() > 0 ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
if ( val == 'stop' ) // if 'a' was received led 2 is switched off
{
digitalWrite(Relay1, HIGH); // turn Off pin 2
}
if ( val == 'start' ) // if 'A' was received led 2 on
{
digitalWrite(Relay1, LOW); // turn ON pin 2
}
if ( val == 'off' ) // if 'b' was received led 3 is switched off
{
digitalWrite(Relay2, HIGH); // turn Off pin 3
}
if ( val == 'on' ) // if 'B' was received led 3 on
{
digitalWrite(Relay2, LOW); // turn ON pin 3
} //else (ledpin = 3, LOW) //set led pin 3 to low state
}
}
Posting Komentar