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;
}