Advertisement
PDP11

Monitor_1B_Spark

Apr 6th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. /*
  2. -----------------------------------
  3. Webpage = MonitorSwitch.html
  4. Monitors the state of switch1. If switch1 is pressed then the value of variable sw1 changes.
  5. The webpage monitors the state of sw1 by sending a $.get request.
  6. If sw1 is 1 then the webpage sends $.post to the core to switch led1 ON.
  7. If sw1 is 0 then the webpage sends $.post to the core to switch led1 OFF.
  8.  
  9. Builds on the code for controlling LEDs on the Sparkcore from the Webpage
  10. -----------------------------------
  11. */
  12.  
  13. // name the pins
  14. int led1 = D0;
  15. int led2 = D1;
  16. int switch1 = D2;
  17. int sw1 = 0;
  18.  
  19. // This routine runs only once upon reset
  20. void setup()
  21. {
  22. //Register our Spark function here
  23. Spark.function("led", ledControl);
  24. Spark.function("state", swState);
  25. Spark.variable("sw1", &sw1, INT);
  26.  
  27. // Configure the pins to be outputs
  28. pinMode(led1, OUTPUT);
  29. pinMode(led2, OUTPUT);
  30. pinMode(switch1, INPUT);
  31.  
  32. // Initialize both the LEDs to be OFF
  33. digitalWrite(led1, LOW);
  34. digitalWrite(led2, LOW);
  35. }
  36.  
  37.  
  38. // This routine loops forever
  39. void loop()
  40. {
  41.  
  42. if(digitalRead(switch1) == LOW)
  43. {
  44. if(sw1 == 1)
  45. {
  46. sw1 = 0;
  47. digitalWrite(led2,LOW);
  48. }
  49. else
  50. {
  51. sw1 = 1;
  52. digitalWrite(led2,HIGH);
  53. }
  54. while(digitalRead(switch1) == LOW)
  55. delay(10);
  56. }
  57. //else
  58. //digitalWrite(led2,HIGH);
  59.  
  60.  
  61. }
  62.  
  63.  
  64.  
  65. // This function gets called whenever there is a matching API request
  66. // the command string format is l<led number>,<state>
  67. // for example: l1,HIGH or l1,LOW
  68. // l2,HIGH or l2,LOW
  69. int ledControl(String command)
  70. {
  71. int state = 0;
  72. //find out the pin number and convert the ascii to integer
  73. int pinNumber = command.charAt(1) - '0';
  74. //Sanity check to see if the pin numbers are within limits
  75. if (pinNumber < 0 || pinNumber > 1) return -1;
  76.  
  77. // find out the state of the led
  78. if(command.substring(3,7) == "HIGH") state = 1;
  79. else if(command.substring(3,6) == "LOW") state = 0;
  80. else return -1;
  81.  
  82. // write to the appropriate pin
  83. digitalWrite(pinNumber, state);
  84. return 1;
  85. }
  86.  
  87. // This function gets called whenever there is a matching API request
  88. // the command string format is sw<switch number>,<state>
  89. // for example: sw1,HIGH or sw1,LOW
  90. // sw2,HIGH or sw2,LOW
  91. int swState(String command)
  92. {
  93. int state = 0;
  94. //find out the pin number and convert the ascii to integer
  95. int pinNumber = command.charAt(2) - '0';
  96. //Sanity check to see if the pin numbers are within limits
  97. if (pinNumber < 0 || pinNumber > 1) return -1;
  98.  
  99. // find out the state of the led
  100. if(command.substring(4,8) == "HIGH") sw1 = 1;
  101. else if(command.substring(4,7) == "LOW") sw1 = 0;
  102. else return -1;
  103.  
  104. return 1;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement