Switch Statement

Well still playing with led's max7219 and now I added a button... to change the led pattern. It seems to running to default only, as if my variable is not changing.

#include "binary.h"  //Use this include for writing a byte in binary
#include "LedControl.h"//Use this include for controling LED's

const int buttonPin = 2;
const int dataPin = 11;
const int clockPin = 13;
const int devicePin = 10;
const int maxChips = 1;
int buttonVal = 0; //holds the number of times button was pushed.
int buttonState;
int lastState;
LedControl lc=LedControl(dataPin,clockPin,devicePin,maxChips); 

void setup()
{

  lc.shutdown(0,false);
  lc.setIntensity(0,8);  
  pinMode(buttonPin,INPUT);
  buttonState = digitalRead(buttonPin);
  
    if(buttonState == HIGH && lastState == LOW)
  {
    if(buttonVal > 3)
    {
      buttonVal == 0;
    }
      buttonVal++;
  }
  
}  

void loop()

{
  
  switch (buttonVal)
  {
    
  case 1:
    lc.setRow(0,0,B01011000); 
    break;

  case 2:
    lc.setRow(0,0,B00011111); 
    break;
  
  case 3:
    lc.setRow(0,0,B10101010);
    break;
    
  default:
    lc.setRow(0,0,B11111111); 
    
  }
  
}

Well there it is, a nicer more professional looking format, using auto format, and I eliminated the obvious comments. Funny, now that I am getting the hang of it,, all comments seems obvious when you code properly.
I am not entirely sure, though I think it is ok, to put the buttonState bit in setup.
All help is appreciated from the experts out there. I have learned lots and will keep on learning.
Thank you in advance for any and all help.

as if my variable is not changing.

You mean your "switch" variable "buttonVal"?
You've hit the nail squarely on the head.
Where do you change its value?

oh my it is in setup,which only runs once ? so the value can never change, regardless of how many times the button is pushed ?

      buttonVal == 0;

That doesn't do anything useful.

it is in setup,which only runs once ? so the value can never change, regardless of how many times the button is pushed ?

That's right. Move the button reading and updating of buttonState into the loop() function.

LOL @ Gammon. Thanks for the link. I did a few tweaks, however results are still the same. I even made a buttonCount function to no avail. New code as follows.

#include "binary.h"  //Use this include for writing a byte in binary
#include "LedControl.h"//Use this include for controling LED's

const int buttonPin = 2;
const int dataPin = 11;
const int clockPin = 13;
const int devicePin = 10;
const int maxChips = 1;
int buttonVal = 0; //holds the number of times button was pushed.
int buttonState;
int lastState;
LedControl lc=LedControl(dataPin,clockPin,devicePin,maxChips); 

void setup()
{

  lc.shutdown(0,false);
  lc.setIntensity(0,8);  
  pinMode(buttonPin,INPUT);
  buttonState = digitalRead(buttonPin);

}  

void loop()

{
  buttonCount();

  switch (buttonVal)
  {

  case 1:
    lc.setRow(0,0,B01011000); 
    break;

  case 2:
    lc.setRow(0,0,B00011111); 
    break;

  case 3:
    lc.setRow(0,0,B10101010);
    break;

  default:
    lc.setRow(0,0,B11111111); 

  }

}

void buttonCount()
{
  
  if(buttonState == HIGH && lastState == LOW)
  {
    if(buttonVal > 3)
    {
      buttonVal = 0;
    }

    buttonVal++;
  }
}

I am wondering do I require the lastState?

&& lastState == LOW)

lastState is always LOW - you never update it,

buttonCount should use a static variable that it returns. It should NOT be diddling with a global variable.

I did some changes, followed the advice best as I understood, and it still is only reading default.

#include "binary.h"  //Use this include for writing a byte in binary
#include "LedControl.h"//Use this include for controling LED's

const int buttonPin = 2;
const int dataPin = 11;
const int clockPin = 13;
const int devicePin = 10;
const int maxChips = 1;
int buttonVal = 0; //holds the number of times button was pushed.
LedControl lc=LedControl(dataPin,clockPin,devicePin,maxChips); 

void setup()
{

  lc.shutdown(0,false);
  lc.setIntensity(0,8);  
  pinMode(buttonPin,INPUT);


}  

void loop()

{
  buttonCount();

  switch (buttonVal)
  {

  case 1:
    lc.setRow(0,0,B01011000); 
    break;

  case 2:
    lc.setRow(0,0,B00011111); 
    break;

  case 3:
    lc.setRow(0,0,B10101010);
    break;

  default:
    lc.setRow(0,0,B11111111); 

  }

}

void buttonCount()
{
  static int buttonState;
  if(buttonState == HIGH )
  {
    if(buttonVal > 3)
    {
      buttonVal = digitalRead(buttonPin);
    }

    buttonVal++;
  }
}

if this is correct then it leads me to believe I have actually hooked up the switch wrong somehow. I will see about posting a schematic, thought it will be hand drawn since I do not have tools to draw such.

void buttonCount()
{
  static int buttonState;
  if(buttonState == HIGH )
...

buttonState never changes so there is no point to testing its value. Thus there is no point to the entire function.

Check out expresspcb.com, very easy to learn/use for schematic capture. Here's an example.

You are still diddling with a global variable in a function that should return a value.

Alright I got it fixed,,,,, wasn't it to my surprise, every button press down and every release is a "HIGH" I was for some reason expecting down (closed switch) as HIGH and up(openswitch)as LOW. This is not the case. Interesting. Thanks for the help everyone... now to complicate it a little for me... more buttons, more combinations.
EDIT: wanted to post the code

#include "binary.h"  //Use this include for writing a byte in binary
#include "LedControl.h"//Use this include for controling LED's

const byte buttonPin = 2;
const int dataPin = 11;
const int clockPin = 13;
const int devicePin = 10;
const int maxChips = 1;
int buttonState = 0;
int lastState = 0;
int buttonVal = 0;

LedControl lc=LedControl(dataPin,clockPin,devicePin,maxChips); 

void setup()
{

  lc.shutdown(0,false);
  lc.setIntensity(0,8);  
  pinMode(buttonPin,INPUT);


}  

void loop()

{
  
 buttonState = digitalRead(buttonPin);
if (buttonState != lastState)
 {
   if(buttonState == HIGH)
   {
     buttonVal++;
   }
 }
  
  if (buttonVal > 3)
  {
   buttonVal = 0; 
  }
  switch (buttonVal)
  {

  case 1:
    lc.setRow(0,0,B00000001); 
    break;

  case 2:
    lc.setRow(0,0,B00000011); 
    break;

  case 3:
    lc.setRow(0,0,B00000111);
    break;

  default:
    lc.setRow(0,0,B00000000); 

  }
  lastState = buttonState ;
}

Thanks again everyone.

every button press down and every release is a "HIGH"

Unlikely, as this would imply that the button is on when pressed and on when not pressed. Either you have a very odd piece of hardware or something else is going on.

Nah it was my poor coding, without this...

 if(buttonState == HIGH)

... I think it is detecting a change, and then adding one to buttonVal on press and release.