Variable Update with Button - Bug or mistake

Hi,

I don't know if it is bug aerror in my script. I have push button on PIN2 with a capacitor and a pull-up resistor.

When the button is pressed In want to switch variable "sequenceNo" between 1 and 2. What is weird, is that in my debug output I get this.

Button Pressed
New Sequence No: 21
Button Pressed
New Sequence No: 21

Script:

//Parameters for sequence No:
const int button = 2;  // Select button on pin 2
int switchState;
int sequenceNo = 1; // Default sequence

void setup() {
  // put your setup code here, to run once:

// Sequence Button Setup
pinMode(button, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
 getSequenceNo();
}

// Functions for Led Strips 
void getSequenceNo() {
 switchState = digitalRead(button);
 if (switchState == LOW) {
  Serial.println ("Button Pressed");
  if (sequenceNo == 1) {
    sequenceNo = 2;
    }
  else {
    sequenceNo == 1;
    }
  Serial.print ("New Sequence No: ");
  Serial.println (Serial.print(sequenceNo));
}
}

Thanks.

Just have

Serial.println (sequenceNo);

What’s this ?

else {
  sequenceNo == 1;
}

should be

else {
  sequenceNo = 1;
}

and

Serial.println(Serial.print(sequenceNo));

should be

Serial.println(sequenceNo);
1 Like

Thanks for helping. It solved my problem.

Pascal

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.