Go Down

Topic: Simple button counter that increments by 5 (Read 1 time) previous topic - next topic

ash901226

try this, its my code, see how i did the press thing
Code: [Select]

uint8_t Switch = 2;
uint8_t Led =13;

boolean LedState =LOW;
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;
int Counter=0;

unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;

void setup()
{
  pinMode(Switch,INPUT);
  digitalWrite(Switch,HIGH);
  pinMode(Led,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  }
  if ((millis() - LastDebounceTime) > DebounceDelay)
  {
    if (CurrentSwitch != LastSwitchState)
    {
      if (CurrentSwitch == LOW)
      {
        Counter=Counter+5;
        Serial.println(Counter);
      }
    }
    LastSwitchState=CurrentSwitch;
  }
  LastSwitchDebounce = CurrentSwitch;
}

Webca

It works great, but i need it to start from zero and not from 5 as it is now. Do you know how i can make that happen?

ash901226

this part
Code: [Select]
int Counter=0;
change to this
Code: [Select]
int Counter=5;

Webca

If i changed it to 5 it started at 10, but you showed me what figure to change, and if i changed it to -5 it started at zero as i wanted.

Thank you "ash901226" for solving my problem:)

ash901226

welcome.

i think among a few thing that people should know when starting o learn how to program arduino is the blink without delay , debounce and state change... and flag based programming . this all have a powerful effect toward solving most problem

Go Up