Problem with counter not sending output commands

Hi people,

I've been breaking my head over this for two days now. As a start for my self made 4x4 LED matrix. The idea is to be able to move a single lit LED around the matrix, in all four directions controlled with four buttons. I decided to add a horizontal counter (H) and later also a vertical one. The problem occurs in the first horizontal line. (the only line I have written code for so far)

My code basically works like this; Press the left (L) button and from the starting LED goes one spot left, same for right. A counter is used to keep track of where the LED is and when it reaches the border of the matrix after three presses, the next press resets the counter and the LED is back at the starting point (top left). When I press the button to the right (this is -1, -2, etc for the counter) everything seems to work fine. Also when I press the L button the lit LED goes back one spot. Only when I get to the starting LED (number 1) and keep going left, the other LED's won't light up untill I am back at four presses and the counter resets to 0.

So yeah, right works fine, left only does when counter is below 0. I simply can not get my head around what I'm doing wrong. Someone please enlighten me!

Gillian de la Chambre

here is my code:

const int  buttonLPin = A0;    // the pin that the pushbutton is attached to
const int  buttonRPin = A1;
const int led1 = 52;       // the pin that the LED is attached to
const int led5 = 44;
const int led9 = 37;
const int led13 = 28;

// Variables will change:
int buttonPushCounterH = 0;   // counter for the number of button presses
int buttonStateL = 0;         // current state of the button
int lastButtonStateL = 0;  // previous state of the button
int buttonStateR = 0;
int lastButtonStateR = 0;


void setup() {
  // initialize the button pin as a input:
  pinMode(buttonLPin, INPUT);
  pinMode(buttonRPin, INPUT);
  // initialize the LED as an output:
  pinMode(led1, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led13, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {

// read the pushbutton input pin:
  buttonStateL = digitalRead(buttonLPin);
  buttonStateR = digitalRead(buttonRPin);
  
{
 if(buttonPushCounterH == 0){
  digitalWrite(led1, HIGH);
  digitalWrite(led5, LOW);}
  else { digitalWrite(led1, LOW);}
 }
  
  
 if(buttonStateL != lastButtonStateL) {
   if(buttonStateL == HIGH);
    buttonPushCounterH++;}                    // Increment Count by 1
     Serial.print("Button has been pressed ");
      Serial.print(lastButtonStateL);
      Serial.println(" times");
       delay(150); 
       
   if(buttonStateR != lastButtonStateR) {    // if SW2 is pressed perform action described in loop
    if(buttonStateR == HIGH);
    buttonPushCounterH--;                     // Decrement Count by 1
     Serial.print("Button has been pressed ");
      Serial.print(lastButtonStateL);
      Serial.println(" times");
       delay(150); 
   }

  lastButtonStateL == buttonStateL;
  lastButtonStateR == buttonStateR;
  // save the current state as the last state,
  //for next time through the loop

 

  if (buttonPushCounterH >= 4) {
    buttonPushCounterH = 0;}
  if (buttonPushCounterH <= -4) {
    buttonPushCounterH = 0;}

          
{  if (buttonPushCounterH == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led13, HIGH);
  } else {
    digitalWrite(led13, LOW);
  }
  if (buttonPushCounterH == 2) {
    digitalWrite(led13, LOW);
    digitalWrite(led9, HIGH);
  } else {
    digitalWrite(led9, LOW);
  }
  if (buttonPushCounterH == 3) {
    digitalWrite(led9, LOW);
    digitalWrite(led5, HIGH);
  } else {
    digitalWrite(led5, LOW);
 }
 }
{if (buttonPushCounterH == -1) {
    digitalWrite(led1, LOW);
    digitalWrite(led5, HIGH);
  } else {
    digitalWrite(led5, LOW);
  }
  if (buttonPushCounterH == -2) {
    digitalWrite(led5, LOW);
    digitalWrite(led9, HIGH);
  } else {
    digitalWrite(led9, LOW);
  }
  if (buttonPushCounterH == -3) {
    digitalWrite(led9, LOW);
    digitalWrite(led13, HIGH);
  } else {
    digitalWrite(led13, LOW);
 }
 }
 }

You have a fair few unnecessary pairs of braces, that confuse things a little. (They won't be causing problems, though, except for readability.)

These won't be helping:-

if (buttonStateL == HIGH);
.
.
if (buttonStateR == HIGH);

(Lose the semi-colons from the ends.)

Incidentally, if you hit Ctrl-T in the IDE to auto-format, those errors become immediately apparent because the following lines aren't indented.

Anyway, fix them, then see if your program does what you want. Let us know if there's still a problem.

Edit: I just spotted these. They won't be helping, either:-

lastButtonStateL == buttonStateL;
lastButtonStateR == buttonStateR;

I guess you meant:-

lastButtonStateL = buttonStateL;
lastButtonStateR = buttonStateR;

Edit2:
Also, I notice that the layout of these two is different:-

if (buttonStateL != lastButtonStateL)
{
    if (buttonStateL == HIGH)
        buttonPushCounterH++;
}                    // Increment Count by 1
Serial.print("Button has been pressed ");
Serial.print(lastButtonStateL);
Serial.println(" times");
delay(150);

if (buttonStateR != lastButtonStateR)     // if SW2 is pressed perform action described in loop
{
    if (buttonStateR == HIGH)
        buttonPushCounterH--;                     // Decrement Count by 1
    Serial.print("Button has been pressed ");
    Serial.print(lastButtonStateL);
    Serial.println(" times");
    delay(150);
}

In the first, did you mean to put the serial prints and delay inside the braces, like in the second?
(Yet another argument for using Ctrl-T. It would have made this stand out more.)

Oh and in the second of the last two conditionals that I quoted, you have:-Serial.print(lastButtonStateL);but I think you meant:-Serial.print(lastButtonStateR);

I think I'm done now. :slight_smile:

Hi,
How have you got your 4x4 LED matrix wired.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you written simple code to make sure it works?

Thanks.. Tom... :slight_smile: