Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- const int xPin = 9; // X output of the accelerometer
- const int yPin = 10; // Y output of the accelerometer
- int ledg = 7; //pin for blue LED
- int ledr = 6; //pin for red LED
- int ledb = 8; //pin for green LED
- int audibleAlarm = 11; //pin for audible alarm
- int sensPot = 14; // potentiometer to set sensitivity level
- int timePot = 15;
- int button = 17; //button to increment cycle count
- int state = HIGH; // the current state of the output pin //some debounce stuff
- int reading; // the current reading from the input pin
- int previous = LOW; // the previous reading from the input pin
- int newvalX = 0; //read old and new values from accelerometer to compare them
- int oldvalX = 0;
- int newvalY = 0;
- int oldvalY = 0;
- unsigned long previousMillisr = 0; // last update time for red LED
- unsigned long previousMillisg = 0; // last update time for blue LED
- unsigned long previousMillisAlarm = 0;
- unsigned long previousPulse = 0;
- unsigned long endalarm = 0; // will store last time the alarm cycle was updated
- unsigned long endWarnAlarm = 0;
- unsigned long previousMillisOff = 0;
- long debounce = 200; // the debounce time
- long time = 0; //time it takes to do simple things without proper coffee supplementation
- long trimTime;
- long alarmLoopTime = 20000; //time from first warn to reset
- int ledStater = LOW; //Starting state of common Anode LEDs
- int ledStateb = LOW; //Reverse for common Cathode
- int ledStateg = LOW;
- int alarmState = LOW;
- int sens; //
- int loopcount = 0; //times the warn cycle has incremented
- int warnLoop = 0; //for warn loop timing
- int loopCountPrint;
- int buttonCount = 2; //default button count (loopcount 2)
- int cycleCount = 3; //initial cycleCount setting (subtract 1 for actual setting as incremented for timing)
- const long interval = 4000; //blink interval for green LED
- const long intervalr = 100; //blink interval for red/blue LEDs
- const long intervalAlarm = 1000; //audible alarm interval
- const long warnTime = 4000; //time for warn (flashing red/blue)
- const long intervalOff = 1000;
- bool startloop = false;
- bool warn = false;
- void setup() {
- // initialize serial communications:
- Serial.begin(9600);
- // initialize the pins connected to the accelerometer
- // as inputs:
- pinMode(xPin, INPUT);
- pinMode(yPin, INPUT);
- pinMode(ledb, OUTPUT);
- pinMode(ledr, OUTPUT);
- pinMode(ledg, OUTPUT);
- pinMode(audibleAlarm, OUTPUT);
- pinMode(button, INPUT);
- }
- void loop() {
- /*
- unsigned long currentPulse = millis(); //run loop slower
- if (currentPulse - previousPulse > 100)
- {
- previousPulse = currentPulse;
- */
- // variables to read the pulse widths:
- int pulseX, pulseY;
- // read pulse from x- and y-axes:
- pulseX = pulseIn(xPin, HIGH);
- pulseY = pulseIn(yPin, HIGH);
- if (digitalRead(button) == HIGH && millis() - time > debounce) {
- buttonCount = buttonCount + 1;
- if (buttonCount == 6) {
- buttonCount = 1;
- }
- time = millis();
- }
- if (buttonCount == 1) {
- cycleCount = 2;
- }
- if (buttonCount == 2) {
- cycleCount = 3;
- }
- if (buttonCount == 3) {
- cycleCount = 4;
- }
- if (buttonCount == 4) {
- cycleCount = 5;
- }
- if (buttonCount == 5) {
- cycleCount = 6;
- }
- reading = digitalRead(button);
- if ((buttonCount == 5) && (reading == HIGH && previous == LOW)) { //chirp to indicate high setting
- digitalWrite(audibleAlarm, HIGH);
- }
- else {
- digitalWrite(audibleAlarm, alarmState);
- }
- if (warn == true) {
- unsigned long startWarnAlarm = millis();
- if (warnLoop == 1) {
- endWarnAlarm = startWarnAlarm;
- warnLoop = warnLoop + 1;
- loopcount = loopcount + 1;
- }
- if (startWarnAlarm - endWarnAlarm > warnTime) {
- warn = false;
- warnLoop = 0;
- }
- }
- if (startloop == true) //If notok has been called, start a timed loop
- {
- unsigned long startalarm = millis();
- if (loopcount == 1) {
- endalarm = startalarm;
- loopcount = loopcount + 1;
- }
- if (startalarm - endalarm > alarmLoopTime) {
- startloop = false;
- loopcount = 0;
- digitalWrite(audibleAlarm, LOW);
- Serial.println("Reset Alarm Timer");
- }
- }
- int range;
- newvalX = pulseX;
- newvalY = pulseY;
- sens = analogRead(sensPot);
- range = (sens / 6) + 20;
- trimTime = analogRead(timePot);
- alarmLoopTime = ((trimTime * 100) + 20000);
- if ((oldvalX != 0) && (oldvalY != 0)) //Make sure the sensor is there and spitting out numbers(probably not needed)
- {
- if (((newvalX - oldvalX > range) || (newvalY - oldvalY > range)) && (loopcount < cycleCount)) //the sensitivity required for the alarm to trip
- {
- warn = true;
- }
- else if (loopcount < cycleCount) //If the visual alarm has not triped x amount of times, everything is ok
- {
- allok();
- }
- else if (loopcount >= cycleCount) //If the visual alarm has tripped x amount of times, audible and visual alarm on
- {
- allAlarm();
- }
- }
- if (warn == true) {
- notok();
- }
- if (loopcount != cycleCount) { //Make sure the audible alarm gets turned off
- alarmState = LOW;
- }
- loopCountPrint = loopcount;
- if (loopCountPrint != 0) { //Subtract one to avoid others confusion, but increase your own
- loopCountPrint = loopCountPrint - 1;
- }
- oldvalX = newvalX;
- oldvalY = newvalY;
- Serial.print(" LoopCount=");
- Serial.print(loopCountPrint);
- Serial.print(" ");
- Serial.print(" CycleCount=");
- Serial.print(cycleCount - 1);
- Serial.print(" ");
- Serial.print(pulseX);
- Serial.print(" ");
- Serial.print(pulseY);
- Serial.print(" SL=");
- Serial.print(startloop);
- // Serial.print(" SA=");
- // Serial.print(millis() / 1000); //show seconds
- // Serial.print(" EA=");
- // Serial.print(endalarm / 1000);
- Serial.print(" Sensitivity=");
- Serial.print(range);
- Serial.print( " AlarmTimer=");
- Serial.print(alarmLoopTime / 1000);
- // Serial.print(" LedStateg=");
- // Serial.print(ledStateg);
- Serial.println();
- }
- //}
- void notok() { //sensor is triggered flash red/blue
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillisr >= intervalr) {
- // save the last time you blinked the LED
- previousMillisr = currentMillis;
- // if the LED is off turn it on and vice-versa:
- if (ledStater == LOW) {
- ledStater = HIGH;
- ledStateb = LOW;
- } else {
- ledStater = LOW;
- ledStateb = HIGH;
- }
- // set the LED with the ledState of the variable:
- digitalWrite(ledr, ledStater);
- digitalWrite(ledb, ledStateb);
- digitalWrite(ledg, LOW); //make sure green LED is off
- }
- startloop = true;
- warnLoop = warnLoop + 1;
- }
- void allok() //sensor not triggered, flash green LED
- {
- unsigned long currentMillisg = millis();
- if (currentMillisg - previousMillisg >= interval) { //LED off interval
- previousMillisg = currentMillisg;
- ledStateg = HIGH;
- digitalWrite(ledg, ledStateg);
- digitalWrite(ledr, LOW);
- digitalWrite(ledb, LOW);
- }
- if (ledStateg == HIGH) {
- unsigned long currentMillisOff = millis();
- if (currentMillisOff - previousMillisg >= intervalOff) { //LED on interval
- previousMillisg = currentMillisOff;
- ledStateg = LOW;
- digitalWrite(ledg, ledStateg);
- digitalWrite(ledr, LOW);
- digitalWrite(ledb, LOW);
- }
- }
- }
- void allAlarm() //flash red/green sound alarm
- {
- unsigned long currentMillisAlarm = millis();
- if (currentMillisAlarm - previousMillisAlarm >= intervalAlarm) {
- // save the last time you blinked the LED
- previousMillisAlarm = currentMillisAlarm;
- Serial.println("Alarm Sounds");
- if (alarmState == HIGH) {
- alarmState = LOW;
- } else {
- alarmState = HIGH;
- }
- digitalWrite(audibleAlarm, alarmState);
- }
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillisr >= intervalr) {
- // save the last time you blinked the LED
- previousMillisr = currentMillis;
- // if the LED is off turn it on and vice-versa:
- if (ledStater == LOW) {
- ledStater = HIGH;
- ledStateb = LOW;
- } else {
- ledStater = LOW;
- ledStateb = HIGH;
- }
- // set the LED with the ledState of the variable:
- digitalWrite(ledr, ledStater);
- digitalWrite(ledb, ledStateb);
- digitalWrite(ledg, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement