Go Down

Topic: Program initializing a variable with 1 even when 0 is mentoned (Read 235 times) previous topic - next topic


const int switchPin = 26;
const int ledPin = 10;

int state;
int lastState;
int ledState;
int pressCounter = 0;

void setup()
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);

void loop()
  state = digitalRead(switchPin);
  if(state != lastState)
    if(state == HIGH)
      pressCounter += 1;
    {//Do nothing
    lastState = state;
  if(pressCounter % 5 == 0)
    ledState = HIGH;
    Serial.println("Hurray! You've pressed 5 times");
    ledState = LOW;

digitalWrite(ledPin, ledState);

The pressCounter variable is always getting initialized with 1 even when I have specified the initial value to be 0. Where am I going wrong?
This program turns on an led when the switch is pressed five times.


Well the first time you print pressCounter is after you increment it.... why not put a print in setup() so you can verify that it starts out as 0?
My hovercraft is full of eels.

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

Go Up