/* Wireless communication Original project from http://www.swanrobotics.com This project demonstrates wireless communication with an APC220 from http://www.dfrobot.com/ Sending and receiving data between a computer and an Arduino wireless. On the computer the Serial Monitor from the Arduino software is used. Control the LED with typing a '1' to turn the LED on and a '0' to turn it off. The switch will send a message when change state. The schematics for this project can be found on http://www.swanrobotics.com This example code is in the public domain. */ // set pins: const int switchPin = 3; // pin number of the switch const int ledPin = 2; // pin number of the LED // variables: int switchState = 0; // variable for reading switch status int intSerialVal = 0; // variable for reading Serial Value // initialize void setup() { // initialize LED pin as output: pinMode(ledPin, OUTPUT); // initialize switch pin as input: pinMode(switchPin, INPUT); // initialize serial wireless communication: Serial.begin(9600); // read initial state of switch switchState = digitalRead(switchPin); } // program loop void loop() { // LED control intSerialVal = Serial.read(); // read user input switch (intSerialVal) { case '0': digitalWrite(ledPin,LOW); // turn LED off Serial.println("Led Off"); // send message about LED break; case '1': digitalWrite(ledPin,HIGH); // turn LED on Serial.println("Led On"); // send message about LED break; } // read switch state and print line if state has changed switch (digitalRead(switchPin)) { // read pin status case HIGH: if (switchState == LOW) { // check if message has to be send Serial.println("Switch On"); // send message about switch switchState = HIGH; // message has been send } break; case LOW: if (switchState == HIGH) { // check if message has to be send Serial.println("Switch Off"); // send message about switch switchState = LOW; // message has been send } break; } }