-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuzzer_functions
More file actions
42 lines (36 loc) · 1007 Bytes
/
buzzer_functions
File metadata and controls
42 lines (36 loc) · 1007 Bytes
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
#include <Arduino.h>
// Buzzer variables
int buzzerPin;
bool buzzerState = false;
unsigned long previousMillis = 0;
void setupBuzzer(int pin) {
buzzerPin = pin;
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
}
void updateBuzzer(int state) {
unsigned long currentMillis = millis();
int interval;
switch(state) {
case 1: // UP movement
interval = 5; // 5ms interval
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
buzzerState = !buzzerState;
digitalWrite(buzzerPin, buzzerState);
}
break;
case 2: // DOWN movement
interval = 15; // 15ms interval
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
buzzerState = !buzzerState;
digitalWrite(buzzerPin, buzzerState);
}
break;
default: // No movement (state 0)
digitalWrite(buzzerPin, LOW);
buzzerState = false;
break;
}
}