Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. // ------------
  2. // Blink an LED - Modified for WeFeel Light - A visual display of the current internet mood
  3. // ------------
  4.  
  5. /*-------------
  6.  
  7. We've heavily commented this code for you. If you're a pro, feel free to ignore it.
  8.  
  9. Comments start with two slashes or are blocked off by a slash and a star.
  10. You can read them, but your device can't.
  11. It's like a secret message just for you.
  12.  
  13. Every program based on Wiring (programming language used by Arduino, and Particle devices) has two essential parts:
  14. setup - runs once at the beginning of your program
  15. loop - runs continuously over and over
  16.  
  17. You'll see how we use these in a second.
  18.  
  19.  
  20. -------------*/
  21.  
  22.  
  23. // First, we're going to make some variables.
  24. // This is our "shorthand" that we'll use throughout the program:
  25.  
  26. int led1 = D0; // Instead of writing D0 over and over again, we'll write led1
  27. // You'll need to wire an LED to this one to see it blink.
  28.  
  29. int led2 = D7; // Instead of writing D7 over and over again, we'll write led2
  30. // This one is the little blue LED on your board. On the Photon it is next to D7, and on the Core it is next to the USB jack.
  31.  
  32. enum emotionalState {
  33. EMOTIONAL_STATE_JOY,
  34. EMOTIONAL_STATE_LOVE,
  35. EMOTIONAL_STATE_ANGER,
  36. EMOTIONAL_STATE_CANT_CONNECT
  37. };
  38.  
  39. // returns after 1 second
  40. void controlEmotionalLed(int state) {
  41. switch (state) {
  42. case EMOTIONAL_STATE_JOY:
  43. digitalWrite(led1, HIGH);
  44. digitalWrite(led2, HIGH);
  45. delay(1000);
  46. break;
  47.  
  48. case EMOTIONAL_STATE_LOVE:
  49. digitalWrite(led1, HIGH);
  50. digitalWrite(led2, HIGH);
  51. delay(200);
  52.  
  53. digitalWrite(led1, LOW);
  54. digitalWrite(led2, LOW);
  55. delay(800);
  56. break;
  57.  
  58. case EMOTIONAL_STATE_ANGER:
  59. digitalWrite(led1, LOW);
  60. digitalWrite(led2, LOW);
  61. delay(1000);
  62. break;
  63.  
  64. case EMOTIONAL_STATE_CANT_CONNECT:
  65. default:
  66. int i;
  67. for (i = 0; i < 5; i++) {
  68. digitalWrite(led1, HIGH);
  69. digitalWrite(led2, HIGH);
  70. delay(100);
  71. digitalWrite(led1, LOW);
  72. digitalWrite(led2, LOW);
  73. delay(100);
  74. }
  75.  
  76. break;
  77. }
  78. }
  79.  
  80. void gotEmotion(const char *event, const char *data) {
  81. static int i = 0;
  82. i++;
  83. Serial.print(i);
  84. Serial.print(event);
  85. Serial.print(", data: ");
  86. if (data)
  87. Serial.println(data);
  88. else
  89. Serial.println("NULL");
  90. }
  91.  
  92. // Having declared these variables, let's move on to the setup function.
  93. // The setup function is a standard part of any microcontroller program.
  94. // It runs only once when the device boots up or is reset.
  95.  
  96. void setup() {
  97.  
  98. // We are going to tell our device that D0 and D7 (which we named led1 and led2 respectively) are going to be output
  99. // (That means that we will be sending voltage to them, rather than monitoring voltage that comes from them)
  100.  
  101. // It's important you do this here, inside the setup() function rather than outside it or in the loop function.
  102.  
  103. pinMode(led1, OUTPUT);
  104. pinMode(led2, OUTPUT);
  105.  
  106. if (Particle.subscribe("hook-response/get_emotion", gotEmotion, MY_DEVICES)) {
  107. Serial.println("subscribed!");
  108. } else {
  109. Serial.println("error: subscription failed");
  110.  
  111.  
  112. }
  113.  
  114.  
  115. }
  116.  
  117. // Next we have the loop function, the other essential part of a microcontroller program.
  118. // This routine gets repeated over and over, as quickly as possible and as many times as possible, after the setup function is called.
  119. // Note: Code that blocks for too long (like more than 5 seconds), can make weird things happen (like dropping the network connection). The built-in delay function shown below safely interleaves required background activity, so arbitrarily long delays can safely be done if you need them.
  120.  
  121. void loop() {
  122. controlEmotionalLed(EMOTIONAL_STATE_JOY);
  123. controlEmotionalLed(EMOTIONAL_STATE_JOY);
  124. controlEmotionalLed(EMOTIONAL_STATE_JOY);
  125. controlEmotionalLed(EMOTIONAL_STATE_JOY);
  126. controlEmotionalLed(EMOTIONAL_STATE_JOY);
  127.  
  128. controlEmotionalLed(EMOTIONAL_STATE_LOVE);
  129. controlEmotionalLed(EMOTIONAL_STATE_LOVE);
  130. controlEmotionalLed(EMOTIONAL_STATE_LOVE);
  131. controlEmotionalLed(EMOTIONAL_STATE_LOVE);
  132. controlEmotionalLed(EMOTIONAL_STATE_LOVE);
  133.  
  134. controlEmotionalLed(EMOTIONAL_STATE_CANT_CONNECT);
  135. controlEmotionalLed(EMOTIONAL_STATE_CANT_CONNECT);
  136. controlEmotionalLed(EMOTIONAL_STATE_CANT_CONNECT);
  137. controlEmotionalLed(EMOTIONAL_STATE_CANT_CONNECT);
  138. controlEmotionalLed(EMOTIONAL_STATE_CANT_CONNECT);
  139.  
  140. controlEmotionalLed(EMOTIONAL_STATE_ANGER);
  141. controlEmotionalLed(EMOTIONAL_STATE_ANGER);
  142. controlEmotionalLed(EMOTIONAL_STATE_ANGER);
  143. controlEmotionalLed(EMOTIONAL_STATE_ANGER);
  144. controlEmotionalLed(EMOTIONAL_STATE_ANGER);
  145.  
  146. // wait 10 seconds after reset before sending the first trigger
  147. static unsigned long nextTrigger = 10 * 1000;
  148.  
  149. if (nextTrigger < millis()) {
  150. // polling Webhook every 2 minutes is 720 API calls/day
  151. nextTrigger = millis() + 2*60*1000;
  152.  
  153. Serial.println("Requesting Emotion");
  154. Particle.publish("get_emotion");
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement