Pushbutton increment/decrement counter

Hey everyone,

I am working on a project for one of my classes where I am making a simplified parking space availability counter with a simple RGB display. The board I am using is an Arduino Mega2560 with an Adafruit matrix shield, connected to a 16x32 RGB Matrix panel. For testing purposes, I am using a breadboard with two mini pushbuttons to simulate a car entering or exiting a lot. The last issue I am having is I am getting a constant increment after startup without giving any inputs. For example, I have my spaces variable initialized at 100. When I power up my board it will display 100 and then continuously increment up after each loop. I am convinced it is not an issue with my push buttons because when I press and hold the increment button it starts counting faster, and when I do the same with the decrement it starts alternating back and fourth (example: 101,100,101,100,.....). I've included a copy of my code bellow. Any help, recommendations, and feedback would be greatly appreciated. Thank you!

#include <RGBmatrixPanel.h>

#define CLK 11 // USE THIS ON ARDUINO UNO, CLK 11 on MEGA
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2

const int buttonPin1 = 40;
const int buttonPin2 = 41;

byte button1_State = LOW;
byte button2_State = LOW;

RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

//number of parking spots
int x = 100;

void setup() {

matrix.begin();

// fix the screen with violet
matrix.fillScreen(matrix.Color333(7, 0, 7));
delay(500);

//fill the screen with black
matrix.fillScreen(matrix.Color333(0, 0, 0));

//yellow text
matrix.setCursor(8, 0);
matrix.setTextSize(1);
matrix.setTextColor(matrix.Color333(7, 4, 0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(7, 4, 0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(7, 4, 0));
matrix.print('A');

//violet text
matrix.setCursor(1, 9); // next line
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('L');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('I');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('O');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('N');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('S');
delay(2000);

//fill the screen with black
matrix.fillScreen(matrix.Color333(0, 0, 0));

//declare what pins are (Input or Output)
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
Serial.begin(9600);

}

void loop() {

button1_State = digitalRead(buttonPin1);
button2_State = digitalRead(buttonPin2);

// counter increment if the pushbutton 1 is pressed.
if (button1_State == HIGH ) {
x++;
}

// counter decrement if the pushbutton 2 is pressed.
else if (button2_State == HIGH ) {
x--;
}

//no action
else {
x = x;
}

matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high

if (x > 0) {
//green text
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print(x);

matrix.setCursor(1, 8);  // next line
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('S');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('P');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('T');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('S');

}

else {
//red text
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print(x);

matrix.setCursor(1, 8);  // next line
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('S');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('P');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('T');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('S');

}

delay(1000);

//fill the screen with black
matrix.fillScreen(matrix.Color333(0, 0, 0));

}

button switches are typically connected between a pin a ground and requires the pin be configured as INPUT_PULLUP which enables an internal pullup resistor making the pin HIGH when the pin is no pressed and LOW when pressed.

the code performs an action (i.e. x++) when the input is HIGH, which as I said above, is the state when the button is not pressed.

but don't you also want the code to only x++ once when the button is pressed. so the code needs to track the button state and check for a state change and only x++ when there's a state change and the input if LOW

Okay so I need to reconfigure my buttons as INPUT_PULLUPs on my bread board? For tracking state change would I need to create another variable that picks up the previous state?

yes and yes

and don't forget to test for LOW not HIGH

I've made those changes, now it's not reacting at all to the PB inputs. Here is what I have now.

#include <RGBmatrixPanel.h>

#define CLK 11 // USE THIS ON ARDUINO MEGA
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2

const int buttonPin1 = 40;
const int buttonPin2 = 41;

byte button1_State = HIGH;
byte button2_State = HIGH;

byte button1prev_State = HIGH;
byte button2prev_State = HIGH;

RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

//number of parking spots
int x = 100;

void setup() {

matrix.begin();

// fix the screen with violet
matrix.fillScreen(matrix.Color333(7, 0, 7));
delay(500);

//fill the screen with black
matrix.fillScreen(matrix.Color333(0, 0, 0));

//yellow text
matrix.setCursor(8, 0);
matrix.setTextSize(1);
matrix.setTextColor(matrix.Color333(7, 4, 0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(7, 4, 0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(7, 4, 0));
matrix.print('A');

//violet text
matrix.setCursor(1, 9); // next line
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('L');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('I');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('O');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('N');
matrix.setTextColor(matrix.Color333(4, 0, 7));
matrix.print('S');
delay(2000);

//fill the screen with black
matrix.fillScreen(matrix.Color333(0, 0, 0));

//declare what pins are (Input or Output)
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
Serial.begin(9600);

}

void loop() {

button1_State = digitalRead(buttonPin1);
button2_State = digitalRead(buttonPin2);

// counter increment if the pushbutton 1 is pressed.
if (button1_State != button1prev_State){
if (button1_State == LOW ) {
x++;
}
}

// counter decrement if the pushbutton 2 is pressed.
if (button2_State != button2prev_State){
if (button2_State == LOW ) {
x--;
}
}

matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high

if (x > 0) {
//green text
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print(x);

matrix.setCursor(1, 8);  // next line
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('S');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('P');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('T');
matrix.setTextColor(matrix.Color333(4, 7, 0));
matrix.print('S');

}

else {
//red text
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print(x);

matrix.setCursor(1, 8);  // next line
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('S');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('P');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('T');
matrix.setTextColor(matrix.Color333(7, 0, 0));
matrix.print('S');

}

delay(500);

//fill the screen with black
matrix.fillScreen(matrix.Color333(0, 0, 0));

}

Posting your code properly in tags (control-T in the ide to get snazzy indents first, then control-shift-C to save for forum, control-V into post) would make it a lot easier to read, but I'm not seeing where you save the current state as previous. Might be missing it though...

edit: see the state change detect example in the ide (File > Examples > 2 Digital) and here. (It has the pin wired to ground, note.)

It's always a good idea to test concepts (like checking the state change of a pin as here) in code of its own so that the other stuff in the sketch (all that matrix stuff here) doesn't get in the way. Then put that tested and understood new concept into the code at large.

one issue is the prev state needs to be updated

    if (button2_State != button2prev_State){
        button2prev_State = button2_State;
        if (button2_State == LOW ) {
            x--;
        }
    }

are you sure the buttons are wired to pins 40 & 41?
can you verify with a small program that simply prints the state of the pin

and post code using "</>"

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