Guys I got this to work, and interestingly the first time around it work only that i have press the switch twice to make it work.
uint8_t Switch = 2;
uint8_t Led =13;
boolean LedState =LOW;
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;
unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;
void setup()
{
pinMode(Switch,INPUT);
digitalWrite(Switch,HIGH);
pinMode(Led,OUTPUT);
}
void loop()
{
int CurrentSwitch = digitalRead(Switch);
if (CurrentSwitch != LastSwitchDebounce)
{
LastDebounceTime = millis();
}
if ((millis() - LastDebounceTime) > DebounceDelay)
{
SwitchDebounce = CurrentSwitch;
SwitchState = digitalRead(Switch);
if (SwitchState != LastSwitchState)
{
if (SwitchState == LOW)
{
LedState = !LedState;
}
}
LastSwitchState=SwitchState;
}
digitalWrite(Led,LedState);
LastSwitchDebounce = CurrentSwitch;
}
Hope you guys could give me some other pointer that i could use to make it more efficient?
i Saw some of the reply saying i could use a library. yes i have Debounce and Bounce library but by using the library i fail to understand how it work only that i know that it work. nothing more then some educational purpose.