Advertisement
Guest User

Arduino Light Switch

a guest
Feb 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. /*
  2. ProTrinket_Lightswithc Program by Duncan Flint
  3.  
  4. This arduino sketch controls five RF lightswitches using an RF transmitter. It utilizes a switch connected to pin 5. It is designed for a pro trinket
  5. Last modified 4/20/2015 (Modification only included clarifying comments)
  6. */
  7.  
  8. #include <RCSwitch.h>
  9.  
  10. int switchPin = 5;
  11. int rfPin = 3;
  12. RCSwitch mySwitch = RCSwitch(); //Declares new object of classs RCSwitch
  13. boolean lightsOn = false;
  14. int numCyclesOn = 0;
  15. int numCyclesOff = 0;
  16.  
  17. void setup()
  18. {
  19.   pinMode(switchPin, INPUT);
  20.   Serial.begin(9600);
  21.   mySwitch.enableTransmit(rfPin); // Enables transmission on pin 3 of arduino
  22.   mySwitch.setPulseLength(173); // Needed for setup of RF transmitter
  23. }
  24.  
  25. void loop()
  26. {
  27.   int switchReading = digitalRead(switchPin); // Check if lightswitch is currently on or off
  28.   Serial.print(switchReading);
  29.   if (switchReading == 1) // Switch reading a 1, that means the switch is in the "off" position
  30.   {
  31.     if ((numCyclesOff == 0) && (lightsOn == true)) // This if statement means that the lights are only turned off if this is the first cycle that the switch has been in the "off" position
  32.     {
  33.       numCyclesOn = 0;
  34.       mySwitch.send(87356, 24); // Outlet 1 off. These codes are from capturing the RF transmissions made by the remote using an RF receiver
  35.       mySwitch.send(87500, 24); // Outlet 2 off
  36.       mySwitch.send(87820, 24); // Outlet 3 off
  37.       mySwitch.send(89356, 24); // Outlet 4 off
  38.       mySwitch.send(95500, 24); // Outlet 5 off
  39.       lightsOn = false;
  40.       numCyclesOff++;
  41.     }
  42.     else
  43.     {
  44.       numCyclesOff++;
  45.     }
  46.   }
  47.   else if (switchReading == 0) // Switch reading a 0, that means the switch is in the "on" position
  48.   {
  49.     if ((numCyclesOn == 0) && (lightsOn == false)) // This if statement means that the lights are only turned on if this is the first cycle that the switch has been in the "on" position
  50.     {
  51.       numCyclesOff = 0;
  52.       mySwitch.send(87347, 24); // Outlet 1 on
  53.       mySwitch.send(87491, 24); // Outlet 2 on
  54.       mySwitch.send(87811, 24); // Outlet 3 on
  55.       mySwitch.send(89347, 24); // Outlet 4 on
  56.       mySwitch.send(95491, 24); // Outlet 5 on
  57.       lightsOn = true;
  58.       numCyclesOn++;
  59.     }
  60.     else
  61.     {
  62.       numCyclesOn++;
  63.     }
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement