Advertisement
andrewb

crisisalert01.ino

Sep 19th, 2014
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. int left_led = 7;
  2. int right_led = 6;
  3. int button_in = 5;
  4. int button_out = 0;
  5. int alarm = A0;
  6.  
  7. int alarm_tone = 0;
  8. const int alarm_high = 1000;
  9. const int alarm_low = 1600;
  10.  
  11. bool flash_on = false;
  12. bool alarm_on = false;
  13. bool yn = true;
  14. int alarm_active = 0;
  15.  
  16. // Time count - 1000milli = 1sec
  17. unsigned long MLEN = 500;
  18. unsigned long ct = 0;
  19. unsigned long at = 0;
  20.  
  21. // Other functions used below
  22. void changeTone();
  23. void flashOpposite();
  24. void turnOn();
  25. void turnOff();
  26.  
  27. // Must pre-define these functions
  28. int setState(String state);
  29.  
  30. // Setup function
  31. void setup() {
  32.     // Function available from web
  33.     Spark.function("state", setState);
  34.     // Variable from the web
  35.     Spark.variable("alarm_tone", &alarm_tone, INT);
  36.     Spark.variable("alarm_active", &alarm_active, INT);
  37.     // Lights
  38.     pinMode(left_led, OUTPUT);
  39.     digitalWrite(left_led, LOW);
  40.     pinMode(right_led, OUTPUT);
  41.     digitalWrite(right_led, LOW);
  42.     // Button
  43.     pinMode(button_in, INPUT);
  44.     pinMode(button_out, OUTPUT);
  45.     digitalWrite(button_out, LOW);
  46.     // Alarm
  47.     pinMode(alarm, OUTPUT);
  48. }
  49.  
  50. void loop() {
  51.     // Visual
  52.     if (flash_on == true) {
  53.         if (ct <= millis()) {
  54.             flashOpposite();
  55.         }
  56.     }
  57.    
  58.     // Audio
  59.     if (alarm_on == true) {
  60.         if (at <= millis()) {
  61.             changeTone();
  62.         }
  63.     }
  64.    
  65.     // Check buton
  66.     int btnChk = digitalRead(button_in);
  67.     if (btnChk > 0) { turnOff(); }
  68. }
  69.  
  70. void changeTone() {
  71.     at = millis() + MLEN;
  72.    
  73.     if (alarm_tone == alarm_high) { alarm_tone = alarm_low; }
  74.     else { alarm_tone = alarm_high; }
  75.    
  76.     analogWrite(alarm, alarm_tone);
  77. }
  78.  
  79. void flashOpposite() {
  80.     ct = millis() + MLEN;
  81.    
  82.     if (yn == true) {
  83.         yn = false;
  84.         digitalWrite(left_led, HIGH);
  85.         digitalWrite(right_led, LOW);
  86.     } else {
  87.         yn = true;
  88.         digitalWrite(left_led, LOW);
  89.         digitalWrite(right_led, HIGH);
  90.     }
  91. }
  92.  
  93. void turnOn() {
  94.     alarm_on = true;
  95.     flash_on = true;
  96.     alarm_active = 1;
  97.     digitalWrite(button_out, HIGH);
  98. }
  99.  
  100. void turnOff() {
  101.     alarm_on = false;
  102.     flash_on = false;
  103.     alarm_active = 0;
  104.     digitalWrite(left_led, LOW);
  105.     digitalWrite(right_led, LOW);
  106.     digitalWrite(button_out, LOW);
  107.     analogWrite(alarm, 0);
  108. }
  109.  
  110. int setState(String state) {
  111.     if (state.equals("alarm")) {
  112.         turnOn();
  113.         return 1;
  114.     }
  115.     else if (state.equals("off")) {
  116.         turnOff();
  117.         return 0;
  118.     }
  119.     return -1;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement