This is a program I made that sounds a tone when a button is pressed on the danger shield. The frequensy of the tone depends on the position of the slider next to the button pressed.
It also shows a number from 0 to 9 (that corresponds to the position of the active slider) on the 7-segment display.
The program is inspired by this video on youtube:
But I couldn't find the program used in the video anywhere, so here it is.
Also a big thanks to PaulS and Newbie for their help with debugging and explaining to me.
The code:
//The serial bytes for the numbers 0-9 to show up at the sevenseg display.
const byte ledCharSet[10] = {
B00111111,B00000110,B01011011,B01001111,B01100110,B01101101,B01111101,B00000111,B01111111,B01101111
};
int toneFreq; //The frequensy of the tone coming out of the speaker.
int sliderPosNumber; //A number 0-9, the position of the active slider.
int prevButton1State = HIGH; //These are to prevent the buttons from interrupting each other. With this noTone () is only called at the moment you realese the button,
int prevButton2State = HIGH;//not the whole time it's realeased.
int 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 //These are pins for output to the shift reg. controlling the sevenseg. Don't know if they have to be #define but they were in the code i c/p:ed from.
#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() {
int button1State = digitalRead(button1Pin);
if (button1State == LOW) //LOW == pressed.
{
int slider1Val = analogRead(slider1Pin);
toneFreq = map(slider1Val, 0, 1023, 200, 600); //Converts the number from the slider to a number between 200 and 600 for the tone() function to use as frequensy.
tone(buzzerPin, toneFreq); //Outputs a tone to the buzzer. The frequensy depends on the position of the slider.
sliderPosNumber = map(slider1Val, 0, 1030, 0, 10); //Converts the slider position to a number between 0 and 9. It should be 0-1023 to 0-9, but the numbers are rounded down
//and for some reson my Ard. only detects the slider going up to 1021
digitalWrite(LATCH, LOW); //Tells the shift register that this is the beginning of a new segment of binary code.
shiftOut(DATA, CLOCK, MSBFIRST, ~(ledCharSet[sliderPosNumber])); //Takes a code from the array at the top and sends to the shift reg.
digitalWrite(LATCH, HIGH); //Tells the shift reg. that this is the end of the code.
}
if (button1State != prevButton1State) //If it detects a change from pressed to realesed (LOW to HIGH) on button1, it stops the tone.
{
if (button1State == HIGH)
{
noTone(buzzerPin);
}
}
prevButton1State = button1State; //So the code above can detect button releases.
//This code is repeated twice for button&slider 2&3.
int button2State = digitalRead(button2Pin);
if (button2State == LOW)
{
int 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);
}
if (button2State != prevButton2State)
{
if (button2State == HIGH)
{
noTone(buzzerPin);
}
}
prevButton2State = button2State;
int button3State = digitalRead(button3Pin);
if (button3State == LOW)
{
int 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);
}
if (button3State != prevButton3State)
{
if (button3State == HIGH)
{
noTone(buzzerPin);
}
}
prevButton3State = button3State;
}