Problem w/ code for sparkfun's danger shield

Hi, I got an Arduino Uno and sparkfun's danger shield yesterday (in Sweden we celebrate christmas on christmas eve) and I've got a small problem with my first project. I couldn't find the code for the project in this video (http://www.youtube.com/watch?v=mLXPCsGSAMQ) so I decided to make a replica of the project. I've also added code for the 7-segment display that makes it show a number (0-9) relative to the position on the slider.

The problem is that when I release a button the tone doesn't stop, instead it continues until I press a new button.

I've copy/pasted some of the code from example projects and it is my first own project so don't be mad if the code's a little messy to read. :)

Well, here is the code, please tell me if you find something that can improve:

const byte ledCharSet[10] = {

  B00111111,B00000110,B01011011,B01001111,B01100110,B01101101,B01111101,B00000111,B01111111,B01101111
};

int slider1Val = 0;
int slider2Val = 0;
int slider3Val = 0;
int toneFreq = 0;
int sliderPosNumber = 0;
boolean button1State = HIGH;
boolean button2State = HIGH;
boolean button3State = HIGH;
boolean prevButton1State = HIGH;
boolean prevButton2State = HIGH;
boolean prevButton3State = HIGH;

const int slider1Pin = A0;
const int slider2Pin = A1;
const int slider3Pin = A2;
const int button1Pin = 12;
const int button2Pin = 11;
const int button3Pin = 10;
const int buzzerPin = 3;
#define LATCH 7
#define CLOCK 8
#define DATA 4

void setup() {
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void loop() {
  boolean button1State = digitalRead(button1Pin);
  if (button1State != prevButton1State) {
    if (button1State == LOW) {
      slider1Val = analogRead(slider1Pin);
      toneFreq = map(slider1Val, 0, 1023, 200, 600);
      tone(buzzerPin, toneFreq);
      sliderPosNumber = map(slider1Val, 0, 1030, 0, 10);
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLOCK, MSBFIRST, ~(ledCharSet[sliderPosNumber]));
      digitalWrite(LATCH, HIGH);
    }
    else {
      noTone(buzzerPin);
    }
  }
  boolean prevButton1State = button1State;
  
  
  boolean button2State = digitalRead(button2Pin);
  if (button2State != prevButton2State) {
    if (button2State == LOW) {
      slider2Val = analogRead(slider2Pin);
      toneFreq = map(slider2Val, 0, 1023, 400, 800);
      tone(buzzerPin, toneFreq);
      sliderPosNumber = map(slider2Val, 0, 1030, 0, 10);
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLOCK, MSBFIRST, ~(ledCharSet[sliderPosNumber]));
      digitalWrite(LATCH, HIGH);
      digitalRead(button2Pin);
    }
    else {
      noTone(buzzerPin);
    }
  }
  boolean prevButton2State = button2State;

  
  boolean button3State = digitalRead(button3Pin);
  if (button3State != prevButton3State) {
    if (button3State == LOW) {
      slider3Val = analogRead(slider3Pin);
      toneFreq = map(slider3Val, 0, 1023, 600, 1200);
      tone(buzzerPin, toneFreq);
      sliderPosNumber = map(slider3Val, 0, 1030, 0, 10);
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLOCK, MSBFIRST, ~(ledCharSet[sliderPosNumber]));
      digitalWrite(LATCH, HIGH);
    }
    else {
      noTone(buzzerPin);
    }
  }
  boolean prevButton3State = button3State;
  
}
  boolean button1State = digitalRead(button1Pin);

The value returned by digitalRead is NOT a boolean.

  boolean prevButton1State = button1State;

This is a local variable with the same name as the global variable.

  if (button1State != prevButton1State) {

This is referencing the global variable that never gets overwritten.

Ok, some supplementary questions then: 1. I thought digitalRead is either HIGH or LOW. Isn't that a boolean value? Should it be an int instead?

  1. What is a global and local varialble?

  2. What do you mean when you say it never gets overwritten?

These may be some dumb questions, but I have mostly looked at other codes and guessed my way from there. And please explain so thoroughly you can, I really want to learn how to make my Arduino do the things I want.

  1. I thought digitalRead is either HIGH or LOW. Isn't that a boolean value? Should it be an int instead?

I guess so !

  1. What is a global and local varialble?

http://en.wikipedia.org/wiki/Global_variable

http://en.wikipedia.org/wiki/Local_variable

  1. What do you mean when you say it never gets overwritten?

as you reference boththe global and local variable th local one gets changed ! imeans that the global one alwways in its assigned state !

I thought digitalRead is either HIGH or LOW. Isn't that a boolean value? Should it be an int instead?

true and false are boolean values. HIGH and LOW are not.

The documentation for any given function will is supposed to tell you what the return type is. http://arduino.cc/en/Reference/DigitalRead Does not. Poor documentation. However, if you look at the source code, you find this in wiring.h:

int digitalRead(uint8_t);

So, the value returned by digitalRead is an int, and should be stored in an int variable.

Global and local refer to the scope of the variable (where in the code the variable can be seen/used). newbie's links are good resources.

I think I understand global and local variables now, I thought you had to use global vars, didn't know of local ;D Is a scope everything between two {}s? Another thing I didn't really understand is why global variables are bad.?

If these are stupid questions I blame it on the time, in Sweden it's 01:30 in the morning. :tired:

Scope refers to the amount of code that can see a variable. The scope of a variable is defined by the block it is defined in, usually delimited by { and }.

Variables defined outside of a class or function are global in scope.

If you want persistent variables, they need to be either global or static.

The problem with global variables is that they can be changed anywhere, deliberately or accidentally. Using the smallest possible scope limits where you have to look for code that can change a variable.

for(int i=0; i<10; i++)
{
   int j = 42; // Very small scope. Not much difficulty
                   // finding the code that changes j.
}

If you have a global variable, and 22 tabs and 8 libraries, a global variable could have been changed anywhere.

If these are stupid questions I blame it on the time

Not stupid at all.

The code is now changed and works. There were some more problems, but thanks to your explaining I could fix them on my own.

The result is in this thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1293452283/0