push button interrupt

Hi everybody

I have got a small max7219 based 8x8 scrolling display, and I am trying to incorporate a push button interrupt to increase the brightness of the LEDs. The scrolling element of the code is fine, but I cannot get the bush button interrupt to work - please advise!

The code is shown below:

many thanks

#include <avr/pgmspace.h>
#include <LedControl.h>

const int DIN = 12;            //DataIn pin (18)
const int CLK = 11;            //Clock pin (17)
const int LOAD = 10;           //Load pin (16)
const int numDevices = 1;      //Number of MAX7219 LED Driver Chips (1-8)
const int buttonPin = 2;     // the number of the pushbutton pin
int intensity = 0;
volatile int buttonState = LOW;
const long scrollDelay = 150;

prog_uchar font5x7 [] PROGMEM = {      //Numeric Font Matrix (Arranged as 7x font data + 1x kerning data)
    B00000000,	//Space (Char 0x20)
    B00000000,
    B00000000,
    B00000000,
    B00000000,
    B00000000,
    B00000000,
    6,

    B01000000,	//1
    B11000000,
    B01000000,
    B01000000,
    B01000000,
    B01000000,
    B11100000,
    4,

    B11111000,	//5
    B10000000,
    B11110000,
    B00001000,
    B00001000,
    B10001000,
    B01110000,
    6,

   B01110000,	//9
    B10001000,
    B10001000,
    B01111000,
    B00001000,
    B00010000,
    B01100000,
    6,

    B00000000,	//:
    B11000000,
    B11000000,
    B00000000,
    B11000000,
    B11000000,
    B00000000,
    3, 
};

prog_uchar scrollText[] PROGMEM ={
    " 9:51 "};

// * GLOBAL VARIABLES
unsigned long bufferLong [14] = {0};  //Buffer for scrolling text
LedControl lc=LedControl(DIN,CLK,LOAD,numDevices);
    
void setup(){
 
    // initialize the pushbutton pin as an input:
    attachInterrupt(buttonPin, stateChange, RISING);
  
    for (int x=0; x<numDevices; x++){
        lc.shutdown(x,false);       //The MAX72XX is in power-saving mode on startup
        lc.setIntensity(x,intensity);       // Set the brightness to default value
        lc.clearDisplay(x);         // and clear the display
    }
}

void loop(){ 
    scrollMessage(scrollText);
  }

void stateChange()
{
  buttonState = !buttonState;
  intensity++;
}

// Scroll Message
void scrollMessage(prog_uchar * messageString) {
    int counter = 0;
    int myChar=0;
    do {
        // read back a char 
        myChar =  pgm_read_byte_near(messageString + counter); 
        if (myChar != 0){
            loadBufferLong(myChar);
        }
        counter++;
    } 
    while (myChar != 0);
}

// Load character into scroll buffer
void loadBufferLong(int ascii){
    if (ascii >= 0x20 && ascii <=0x7f){
        for (int a=0;a<7;a++){                      // Loop 7 times for a 5x7 font
            unsigned long c = pgm_read_byte_near(font5x7 + ((ascii - 0x20) * 8) + a);     // Index into character table to get row data
            unsigned long x = bufferLong [a*2];     // Load current scroll buffer
            x = x | c;                              // OR the new character onto end of current
            bufferLong [a*2] = x;                   // Store in buffer
        }
        byte count = pgm_read_byte_near(font5x7 +((ascii - 0x20) * 8) + 7);     // Index into character table for kerning data
        for (byte x=0; x<count;x++){
            rotateBufferLong();
            printBufferLong();
            delay(scrollDelay);
        }
    }
}
// Rotate the buffer
void rotateBufferLong(){
    for (int a=0;a<7;a++){                      // Loop 7 times for a 5x7 font
        unsigned long x = bufferLong [a*2];     // Get low buffer entry
        byte b = bitRead(x,31);                 // Copy high order bit that gets lost in rotation
        x = x<<1;                               // Rotate left one bit
        bufferLong [a*2] = x;                   // Store new low buffer
        x = bufferLong [a*2+1];                 // Get high buffer entry
        x = x<<1;                               // Rotate left one bit
        bitWrite(x,0,b);                        // Store saved bit
        bufferLong [a*2+1] = x;                 // Store new high buffer
    }
}  
// Display Buffer on LED matrix
void printBufferLong(){
  for (int a=0;a<7;a++){                    // Loop 7 times for a 5x7 font
    unsigned long x = bufferLong [a*2+1];   // Get high buffer entry
    byte y = x;                             // Mask off first character
    lc.setColumn(3,a,y);                       // Send row to relevent MAX7219 chip
    x = bufferLong [a*2];                   // Get low buffer entry
    y = (x>>24);                            // Mask off second character
    lc.setColumn(2,a,y);                       // Send row to relevent MAX7219 chip
    y = (x>>16);                            // Mask off third character
    lc.setColumn(1,a,y);                       // Send row to relevent MAX7219 chip
    y = (x>>8);                             // Mask off forth character
    lc.setColumn(0,a,y);                       // Send row to relevent MAX7219 chip
  }
}

The most obvious question is how IS your switch wired? You are not using the internal pullup resistor, so you wiring is more complicated than it needs to be.

You only call setIntensity() once. That does NOT bind the intensity to the variable intensity. Future changes to the value in intensity are not going to accomplish anything until you call setIntensity() again.

thank you, i was being stupid - calling setIntensity again has got it operating as I want, thanks! I've also since switched to using the internal pull-up resistor as well

thanks again