-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuzzedBuzzer.ino
109 lines (83 loc) · 2.01 KB
/
BuzzedBuzzer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//BuzzedBuzzer by Simple Switch Labs, LLC
//Uses readings from a cheap alcohol sensor to decide when to make a buzzing noise
//looks for increasing alcohol fumes to trigger the buzz
//See http://buzzedbuzzer.com for schematics and detailed build instructions
//Created December 2012
//This code is licensed Creative Commons 3.0 ShareAlike, Attribution
//See https://creativecommons.org/licenses/by-sa/3.0/ for detailed licensing information
#include "pins.h"
unsigned int baseline = 0;
int readings[STORAGE];
unsigned int rollingaverage = 0;
void setup()
{
pinMode(TRANSMIT, OUTPUT);
pinMode(SENSOR, INPUT);
pinMode(BUZZER, OUTPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
//establish a baseline
delay(1000); //let readings stabilize a moment
get_baseline();
}
void loop()
{
take_a_reading();
if((readings[0] >= (baseline + 50)) && (readings[0] >= rollingaverage + 20)) //Look for increasing alcohol signal above baseline. Stop buzzing as soon as alcohol starts to drop.
{
buzz();
}
else
{
nobuzz();
}
delay(150);
}
void take_a_reading()
{
for(int i=(STORAGE - 1);i>0;i--)
{
readings[i] = readings[i-1];
//Serial.print(readings[i]);
//Serial.print(", ");
}
readings[0] = analogRead(SENSOR);
Serial.print("latest = ");
Serial.print(readings[0],DEC);
rollingaverage = 0;
for(int i=1;i<STORAGE;i++)
{
rollingaverage += readings[i];
}
rollingaverage /= (STORAGE - 1);
Serial.print(" rollingavg = ");
Serial.println(rollingaverage);
}
void buzz()
{
digitalWrite(BUZZER,HIGH);
digitalWrite(LED,HIGH);
Serial.println("BUZZ!!!");
}
void nobuzz()
{
digitalWrite(BUZZER,LOW);
digitalWrite(LED,LOW);
Serial.println("nobuzz");
}
void get_baseline()
{
for(int i=0;i<STORAGE;i++)
{
readings[i]=analogRead(SENSOR);
/// Serial.println(readings[i]);
delay(100);
}
for(int i=0;i<STORAGE;i++)
{
baseline += readings[i];
}
baseline /= STORAGE;
Serial.print("baseline= ");
Serial.println(baseline);
}