const int ledPin = 11;//the led attached to digital pin 11 of Arduino Uno const int tiltsensorPin = 2;//the tilt sensor attached to digital pin 2 of Arduino Uno int tiltsensorValue;//variable 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(tiltsensorPin, INPUT);//set tiltsensorPin as INPUT digitalWrite(tiltsensorPin, HIGH);//set tiltsensorPin as HIGH pinMode(ledPin, OUTPUT);//initialize the ledPin as an output Serial.begin(115200);//initialize serial communication at 115200 bps } void loop(){ tiltsensorValue = digitalRead(tiltsensorPin); // If the switch changed, due to noise or pressing: if (tiltsensorValue == 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 = tiltsensorValue; } digitalWrite(ledPin, lastTiltState); Serial.println(tiltsensorValue); delay(500); }