-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilt_detector.ino
40 lines (32 loc) · 1.28 KB
/
tilt_detector.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
// code reference:
// https://www.instructables.com/Arduino-Tilt-Sensor-Tutorial-Interfacing-Tilt-Ball/
int ledPin = 12;
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH; // the previous reading from the tilt sensor
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup(){
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
sensorValue = digitalRead(sensorPin);
// If the switch changed, due to noise or pressing:
if (sensorValue == lastTiltState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
lastTiltState = sensorValue;
}
digitalWrite(ledPin, lastTiltState);
Serial.println(sensorValue);
delay(50); // Erin edit: this was 500 before, changed to make faster reaction to tilt
}