1
8const int buzzer = A3;
9const int lightSensor = A2;
10int NOTE = 5000;
11const int sitPeriod = 10000;
12boolean sittingDown = false;
13
14unsigned long SitPeriodStart;
15unsigned long currentTime;
16
17void setup() {
18 pinMode(lightSensor, INPUT);
19 pinMode(buzzer, OUTPUT);
20 pinMode(A7, OUTPUT);
21
22 Serial.begin(9600);
23}
24
25void loop() {
26
27 int sensorValue;
28 sensorValue = analogRead(lightSensor);
29
30 Serial.print("sensor value: ");
31 Serial.println(sensorValue);
32
33 currentTime = millis();
34 Serial.println(currentTime);
35
36 if (sensorValue<80 && !sittingDown)
37 {
38 sittingDown = true;
39 SitPeriodStart = currentTime;
40 digitalWrite(A7, LOW);
41 }
42 else if (sensorValue>=80)
43 {
44 sittingDown = false;
45 analogWrite(A7, 1);
46 }
47
48 if (sittingDown && (currentTime - SitPeriodStart >= sitPeriod))
49 {
50 alarm();
51 alarm();
52 alarm();
53 sittingDown = false;
54 }
55}
56
57void alarm() {
58 tone(buzzer, NOTE);
59 delay(150);
60 noTone(buzzer);
61 delay(250);
62 tone(buzzer, NOTE);
63 delay(150);
64 noTone(buzzer);
65 delay(250);
66}