What is my increment code missing?

Hello all :slight_smile:

I am trying to make a code that have output 1 high, and when pressing a tactile button it goes low, and output 2 is high. This continues up to 4, then it resets back to 0. But it will not count..

Can you help me telling me where my mistake is? :slight_smile:

const int pin0 = 6;
const int pin1 = 2;     
const int pin2 = 3;      
const int pin3 = 5;
const int button = 7;


     
int buttonState = 0;

void setup() { 
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin0, OUTPUT);
  pinMode(button, INPUT);

Serial.begin(9600);

}
  

void loop() {

int buttonNumber = 0; 
  
  if (button == HIGH) {
    buttonNumber++;
    }
  if (buttonNumber == 4)
  buttonNumber = 1;
  
 
  buttonState = digitalRead(button);


  if (buttonNumber == 0) {    
    Serial.println("State0 High");
    digitalWrite(pin0, HIGH);
  } else {
   Serial.println("State0 Low");
    digitalWrite(pin0, LOW);
  }

  
  if (buttonNumber == 1) {    
    digitalWrite(pin1, HIGH);
    Serial.println("State1 High");
  } else {
    Serial.println("State1 Low");
    digitalWrite(pin1, LOW);
  }

  if (buttonNumber == 2) { 
   Serial.println("State2 High");   
    digitalWrite(pin2, HIGH);
  } else {
    Serial.println("State2 Low");
    digitalWrite(pin2, LOW);
  }


  if (buttonNumber == 3) { 
   Serial.println("State3 High");   
    digitalWrite(pin3, HIGH);
  } else {
    Serial.println("State3 Low");
    digitalWrite(pin3, LOW);
  }

  
}

Can you help me telling me where my mistake is?

Yes. You said "this doesn't work". That's useless information. The program does something. While its running, you do something. You expect the code to do something.

We don't know what any of those things are.

How IS your switch wired?

Creating a new variable, buttonNumber, on every pass through loop() is probably NOT what you want to be doing. Make it static or global.

The code is stuck on output 0. That means that the increment does not work.

The serial said that only the first was high, rest is low. Does not matter how I press the button.

I was hoping there was a programming error you could see since this is really simple code with not much to it..

I tried to remember that global variable. I had problems with a similar code in the past, I was told to use global, but I did not find that thread again.

But then I should move

int buttonNumber = 0;

under void setup() and instead of int use global int? Don't know how to type it though..

The switch is not hooked up ringt now. I am applying 5V to the input, so no pulldown.. I am just testing now

But then I should move
Code: [Select]

int buttonNumber = 0;

under void setup() and instead of int use global int? Don't know how to type it though..

Just move the statement, with NO changes.

It still doesn't work..

Serial shows;
State0 High
State1 Low
State2 Low
State3 Low
State0 High
State1 Low
State2 Low
State3 Low

No matter what I do with input 7... +5V to the pin does nothing..

Your basic concept is OK, right , but have these variables

button
buttonState
buttonNumber

functions / roles all mixed up and in wrong places

Restructure your code, and start with this basic if() construct

loop()
{
is button pressed ?
do this ( set state to x)
else
not pressed , do this ( keep state in Y )
}

keep in mid that loop() is really not helping and will run the basic if() construct continually
you need to modify to detect the button presses changes only once by adding / placing "buttonState".

const int pin0 = 6;
const int pin1 = 2;     
const int pin2 = 3;     
const int pin3 = 5;
const int button = 7;


     
int buttonState = 0;

void setup() {
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin0, OUTPUT);
  pinMode(button, INPUT);

Serial.begin(9600);

}
 

void loop() {

int buttonNumber = 0;
 
// const int button = 7;

// buttonState = digitalRead(button);

  if (button == HIGH) {
    buttonNumber++;
    }
  if (buttonNumber == 4)
  buttonNumber = 1;
 
 
  buttonState = digitalRead(button);


  if (buttonNumber == 0) {   
    Serial.println("State0 High");
    digitalWrite(pin0, HIGH);
  } else {
   Serial.println("State0 Low");
    digitalWrite(pin0, LOW);
  }

 
  if (buttonNumber == 1) {   
    digitalWrite(pin1, HIGH);
    Serial.println("State1 High");
  } else {
    Serial.println("State1 Low");
    digitalWrite(pin1, LOW);
  }

  if (buttonNumber == 2) {
   Serial.println("State2 High");   
    digitalWrite(pin2, HIGH);
  } else {
    Serial.println("State2 Low");
    digitalWrite(pin2, LOW);
  }


  if (buttonNumber == 3) {
   Serial.println("State3 High");   
    digitalWrite(pin3, HIGH);
  } else {
    Serial.println("State3 Low");
    digitalWrite(pin3, LOW);
  }

 
}

I didn't quite understand what you ment 232...

You said to reconstruct the code, but isn't my loop the way to show?

void loop(){

if (buttonNumber == 0 ) {   
    Serial.println("State0 High");
    digitalWrite(pin0, HIGH);
  } else {
   Serial.println("State0 Low");
    digitalWrite(pin0, LOW);
  }
}

I did some minor changes, moved the int buttonNumber = 0; before setup,
But the code will not count. Tried to set the buttonNumber to 2, and then the next output worked, so it is just the counter that is giving me a hard time, the rest works..

const int pin0 = 6;
const int pin1 = 2;     
const int pin2 = 3;     
const int pin3 = 5;
const int button = 7;


     
int buttonState = 0;
int buttonNumber = 2;


void setup() {
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin0, OUTPUT);
  pinMode(button, INPUT);
}
 

void loop() {
buttonState = digitalRead(button);


  if (buttonState == HIGH) {
    buttonNumber++;
    }
  if (buttonNumber == 4)
  buttonNumber = 1;
 
 
 


  if (buttonNumber == 0) {   
      digitalWrite(pin0, HIGH);
  } else {
      digitalWrite(pin0, LOW);
  }

 
  if (buttonNumber == 1) {   
    digitalWrite(pin1, HIGH);
    
  } else {
    digitalWrite(pin1, LOW);
  }

  if (buttonNumber == 2) {    
    digitalWrite(pin2, HIGH);
  } else {   
    digitalWrite(pin2, LOW);
  }


  if (buttonNumber == 3) {     
    digitalWrite(pin3, HIGH);
  } else {   
    digitalWrite(pin3, LOW);
  }

 
}

OK, I am not favor to tell HOW to code.
I was not suggesting anything near "abandoning loop() " , just reconstructing its logic.
I just give hints and expect the user to figure it out.
If that is not OK with you just ignore my posts.

You did not do just "minor changes" - you now actually read the button in the loop() as I was trying to tell you.

Now your read pin 7 and then change the buttonNumber...
Should that be your counter?

I do not understand why you keep troubleshooting (convoluted) code which obviously does not do what you want and are reluctant to minimize the task you are performing and make them work first. Such as doing button detect as I suggested.

You have the build in LED so why don't you turn it on/off after each button press?
Then add counting the button presses etc.

I would suggest you Google MorganS posts - he recently posted very nice code to "read multiple buttons".

buttonNumber is a really lousy name for a variable that purports to hold the number of times a switch has been pressed.

You REALLY need to look at the state change detection example. Incrementing the stupidly-named variable when the switch pin IS high is NOT what you seem to want to do.