SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. const int xPin = 9;     // X output of the accelerometer
  4. const int yPin = 10;     // Y output of the accelerometer
  5. int ledg = 7;           //pin for blue LED
  6. int ledr = 6;           //pin for red LED
  7. int ledb = 8;           //pin for green LED
  8. int audibleAlarm = 11;  //pin for audible alarm
  9. int sensPot = 14;       // potentiometer to set sensitivity level
  10. int timePot = 15;
  11. int button = 17;        //button to increment cycle count
  12.  
  13. int state = HIGH;      // the current state of the output pin  //some debounce stuff
  14. int reading;           // the current reading from the input pin
  15. int previous = LOW;    // the previous reading from the input pin
  16.  
  17. int newvalX = 0;       //read old and new values from accelerometer to compare them
  18. int oldvalX = 0;
  19. int newvalY = 0;
  20. int oldvalY = 0;
  21. unsigned long previousMillisr = 0;        // last update time for red LED
  22. unsigned long previousMillisg = 0;        // last update time for blue LED
  23. unsigned long previousMillisAlarm = 0;
  24. unsigned long previousPulse = 0;
  25. unsigned long endalarm = 0;        // will store last time the alarm cycle was updated
  26. unsigned long endWarnAlarm = 0;
  27. unsigned long previousMillisOff = 0;
  28. long debounce = 200;               // the debounce time
  29. long time = 0;                     //time it takes to do simple things without proper coffee supplementation
  30. long trimTime;
  31. long alarmLoopTime = 20000;       //time from first warn to reset
  32.  
  33. int ledStater = LOW;             //Starting state of common Anode LEDs
  34. int ledStateb = LOW;             //Reverse for common Cathode
  35. int ledStateg = LOW;
  36. int alarmState = LOW;
  37.  
  38. int sens;                         //
  39. int loopcount = 0;                //times the warn cycle has incremented
  40. int warnLoop = 0;                 //for warn loop timing
  41. int loopCountPrint;
  42. int buttonCount = 2;    //default button count (loopcount 2)
  43. int cycleCount = 3;     //initial cycleCount setting (subtract 1 for actual setting as incremented for timing)
  44.  
  45. const long interval = 4000;       //blink interval for green LED
  46. const long intervalr = 100;       //blink interval for red/blue LEDs
  47. const long intervalAlarm = 1000;  //audible alarm interval
  48. const long warnTime = 4000;       //time for warn (flashing red/blue)
  49. const long intervalOff = 1000;
  50. bool startloop = false;
  51. bool warn = false;
  52.  
  53. void setup() {
  54.   // initialize serial communications:
  55.   Serial.begin(9600);
  56.   // initialize the pins connected to the accelerometer
  57.   // as inputs:
  58.   pinMode(xPin, INPUT);
  59.   pinMode(yPin, INPUT);
  60.   pinMode(ledb, OUTPUT);
  61.   pinMode(ledr, OUTPUT);
  62.   pinMode(ledg, OUTPUT);
  63.   pinMode(audibleAlarm, OUTPUT);
  64.   pinMode(button, INPUT);
  65. }
  66.  
  67. void loop() {
  68.  
  69.  
  70.   /*
  71.     unsigned long currentPulse = millis();  //run loop slower
  72.  
  73.     if (currentPulse - previousPulse > 100)
  74.     {
  75.       previousPulse = currentPulse;
  76.  
  77.   */
  78.   // variables to read the pulse widths:
  79.   int pulseX, pulseY;
  80.  
  81.   // read pulse from x- and y-axes:
  82.   pulseX = pulseIn(xPin, HIGH);
  83.   pulseY = pulseIn(yPin, HIGH);
  84.  
  85.  
  86.   if (digitalRead(button) == HIGH && millis() - time > debounce) {
  87.     buttonCount = buttonCount + 1;
  88.     if (buttonCount == 6) {
  89.       buttonCount = 1;
  90.     }
  91.     time = millis();
  92.   }
  93.   if (buttonCount == 1) {
  94.     cycleCount = 2;
  95.   }
  96.   if (buttonCount == 2) {
  97.     cycleCount = 3;
  98.   }
  99.   if (buttonCount == 3) {
  100.     cycleCount = 4;
  101.   }
  102.   if (buttonCount == 4) {
  103.     cycleCount = 5;
  104.   }
  105.   if (buttonCount == 5) {
  106.     cycleCount = 6;
  107.   }
  108.  
  109.   reading = digitalRead(button);
  110.  
  111.   if ((buttonCount == 5) && (reading == HIGH && previous == LOW)) { //chirp to indicate high setting
  112.     digitalWrite(audibleAlarm, HIGH);
  113.   }
  114.   else {
  115.     digitalWrite(audibleAlarm, alarmState);
  116.   }
  117.  
  118.   if (warn == true) {
  119.     unsigned long startWarnAlarm = millis();
  120.     if (warnLoop == 1) {
  121.       endWarnAlarm = startWarnAlarm;
  122.       warnLoop = warnLoop + 1;
  123.       loopcount = loopcount + 1;
  124.     }
  125.     if (startWarnAlarm - endWarnAlarm > warnTime) {
  126.       warn = false;
  127.       warnLoop = 0;
  128.     }
  129.   }
  130.  
  131.   if (startloop == true)  //If notok has been called, start a timed loop
  132.   {
  133.     unsigned long startalarm = millis();
  134.     if (loopcount == 1) {
  135.       endalarm = startalarm;
  136.       loopcount = loopcount + 1;
  137.     }
  138.  
  139.     if (startalarm - endalarm > alarmLoopTime) {
  140.       startloop = false;
  141.       loopcount = 0;
  142.       digitalWrite(audibleAlarm, LOW);
  143.       Serial.println("Reset Alarm Timer");
  144.     }
  145.   }
  146.  
  147.   int range;
  148.   newvalX = pulseX;
  149.   newvalY = pulseY;
  150.   sens = analogRead(sensPot);
  151.   range = (sens / 6) + 20;
  152.  
  153.   trimTime = analogRead(timePot);
  154.   alarmLoopTime = ((trimTime * 100) + 20000);
  155.  
  156.   if ((oldvalX != 0) && (oldvalY != 0))                         //Make sure the sensor is there and spitting out numbers(probably not needed)
  157.   {
  158.     if (((newvalX - oldvalX > range) || (newvalY - oldvalY > range)) && (loopcount < cycleCount))   //the sensitivity required for the alarm to trip
  159.     {
  160.       warn = true;
  161.     }
  162.     else if (loopcount < cycleCount)                                    //If the visual alarm has not triped x amount of times, everything is ok
  163.     {
  164.       allok();
  165.     }
  166.     else if (loopcount >= cycleCount)                                  //If the visual alarm has tripped x amount of times, audible and visual alarm on
  167.     {
  168.       allAlarm();
  169.     }
  170.   }
  171.  
  172.   if (warn == true) {
  173.     notok();
  174.   }
  175.  
  176.   if (loopcount != cycleCount) {         //Make sure the audible alarm gets turned off
  177.     alarmState = LOW;
  178.   }
  179.  
  180.   loopCountPrint = loopcount;
  181.  
  182.   if (loopCountPrint != 0) {            //Subtract one to avoid others confusion, but increase your own
  183.     loopCountPrint = loopCountPrint - 1;
  184.   }
  185.  
  186.   oldvalX = newvalX;
  187.   oldvalY = newvalY;
  188.  
  189.   Serial.print(" LoopCount=");
  190.   Serial.print(loopCountPrint);
  191.   Serial.print("  ");
  192.   Serial.print(" CycleCount=");
  193.   Serial.print(cycleCount - 1);
  194.   Serial.print("  ");
  195.   Serial.print(pulseX);
  196.   Serial.print("  ");
  197.   Serial.print(pulseY);
  198.   Serial.print(" SL=");
  199.   Serial.print(startloop);
  200.   //  Serial.print(" SA=");
  201.   //  Serial.print(millis() / 1000);     //show seconds
  202.   //  Serial.print(" EA=");
  203.   //  Serial.print(endalarm / 1000);
  204.   Serial.print("  Sensitivity=");
  205.   Serial.print(range);
  206.   Serial.print( "  AlarmTimer=");
  207.   Serial.print(alarmLoopTime / 1000);
  208. //  Serial.print("  LedStateg=");
  209. //  Serial.print(ledStateg);
  210.   Serial.println();
  211.  
  212. }
  213.  
  214. //}
  215.  
  216. void notok() { //sensor is triggered flash red/blue
  217.  
  218.   unsigned long currentMillis = millis();
  219.  
  220.   if (currentMillis - previousMillisr >= intervalr) {
  221.     // save the last time you blinked the LED
  222.     previousMillisr = currentMillis;
  223.  
  224.  
  225.     // if the LED is off turn it on and vice-versa:
  226.     if (ledStater == LOW) {
  227.       ledStater = HIGH;
  228.       ledStateb = LOW;
  229.     } else {
  230.       ledStater = LOW;
  231.       ledStateb = HIGH;
  232.     }
  233.  
  234.     // set the LED with the ledState of the variable:
  235.     digitalWrite(ledr, ledStater);
  236.     digitalWrite(ledb, ledStateb);
  237.     digitalWrite(ledg, LOW);             //make sure green LED is off
  238.  
  239.   }
  240.   startloop = true;
  241.   warnLoop = warnLoop + 1;
  242. }
  243.  
  244. void allok()  //sensor not triggered, flash green LED
  245. {
  246.   unsigned long currentMillisg = millis();
  247.  
  248.  
  249.   if (currentMillisg - previousMillisg >= interval) {       //LED off interval
  250.     previousMillisg = currentMillisg;
  251.  
  252.     ledStateg = HIGH;
  253.  
  254.  
  255.     digitalWrite(ledg, ledStateg);
  256.     digitalWrite(ledr, LOW);
  257.     digitalWrite(ledb, LOW);
  258.   }
  259.   if (ledStateg == HIGH) {
  260.     unsigned long currentMillisOff = millis();
  261.  
  262.     if (currentMillisOff - previousMillisg >= intervalOff) {  //LED on interval
  263.       previousMillisg = currentMillisOff;
  264.       ledStateg = LOW;
  265.  
  266.       digitalWrite(ledg, ledStateg);
  267.       digitalWrite(ledr, LOW);
  268.       digitalWrite(ledb, LOW);
  269.     }
  270.   }
  271. }
  272.  
  273. void allAlarm() //flash red/green sound alarm
  274. {
  275.   unsigned long currentMillisAlarm = millis();
  276.  
  277.   if (currentMillisAlarm - previousMillisAlarm >= intervalAlarm) {
  278.     // save the last time you blinked the LED
  279.     previousMillisAlarm = currentMillisAlarm;
  280.  
  281.     Serial.println("Alarm Sounds");
  282.     if (alarmState == HIGH) {
  283.       alarmState = LOW;
  284.     } else {
  285.       alarmState = HIGH;
  286.     }
  287.     digitalWrite(audibleAlarm, alarmState);
  288.   }
  289.  
  290.   unsigned long currentMillis = millis();
  291.   if (currentMillis - previousMillisr >= intervalr) {
  292.     // save the last time you blinked the LED
  293.     previousMillisr = currentMillis;
  294.  
  295.  
  296.     // if the LED is off turn it on and vice-versa:
  297.     if (ledStater == LOW) {
  298.       ledStater = HIGH;
  299.       ledStateb = LOW;
  300.     } else {
  301.       ledStater = LOW;
  302.       ledStateb = HIGH;
  303.     }
  304.  
  305.     // set the LED with the ledState of the variable:
  306.     digitalWrite(ledr, ledStater);
  307.     digitalWrite(ledb, ledStateb);
  308.     digitalWrite(ledg, LOW);
  309.   }
  310. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top