Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. unsigned char relaySwitch = D8;
  2. int currentSensor = A0;
  3. bool switchOn = false;
  4.  
  5.  
  6. void toggleSwitch();
  7. double getCurrentValue();
  8.  
  9.  
  10. void setup() {
  11. pinMode(relaySwitch, OUTPUT);
  12. Serial.begin(9600);
  13. }
  14.  
  15. // the loop function runs over and over again forever
  16. void loop() {
  17.  
  18. if(Serial.available() > 0)
  19. {
  20. char incByte;
  21. incByte = Serial.read();
  22.  
  23. if(incByte == 't')
  24. {
  25. toggleSwitch();
  26. }
  27.  
  28. }
  29.  
  30. Serial.println("\nSwitch: ");
  31. Serial.println(switchOn);
  32. Serial.println("\nCurrent: ");
  33. Serial.println(getCurrentValue());
  34. delay(2000);
  35.  
  36.  
  37. }
  38.  
  39. void toggleSwitch()
  40. {
  41. if(switchOn == false)
  42. {
  43. digitalWrite(relaySwitch,HIGH);
  44. switchOn = true;
  45. }
  46. else
  47. {
  48. digitalWrite(relaySwitch,LOW);
  49. switchOn = false;
  50. }
  51. }
  52.  
  53. double getCurrentValue()
  54. {
  55.  
  56. return double(analogRead(currentSensor));
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement