hw week 5

I created a led lit cigarette box that is controlled by a tilt sensor which I attached to the top of the box which turns the led light on when the top of the box is tilted open. The sensor was a bit buggy and led flickered on and off quite a bit.

int SensorPin = 2;
int LEDPin = 3;

int LEDstate = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 50;

void setup()
{
pinMode(SensorPin, INPUT);
digitalWrite(SensorPin, HIGH);
pinMode(LEDPin, OUTPUT);
}

void loop()
{
int switchstate;

reading = digitalRead(SensorPin);

if (reading != previous) {

time = millis();
}

if ((millis() – time) > debounce) {

switchstate = reading;

if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(LEDPin, LEDstate);

previous = reading;
}

hw5.2 hw5