#include <SevSeg.h>
// A=2, B=3, C=4, D=5, E=6, F=7, G=8, DP=9: // 7 seg display pin numbers
#define NOTE_C5 523 // Define Note frequency (Hz)
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
SevSeg sevseg; // Create 7-segment display variable
const int button = 0; // Analog pin for determining which button was pressed
const int Buzz = 11; // Buzzer PWM Digital I/O Pin
void setup() {
byte numDigits = 1; // only one digit display
byte digitPins[] = {}; // not important to specify digit pins for one digit
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // set segment pins in order A,B,C,D,E,F,G etc
bool resistorsOnSegments = true; // resistor is in series with segment pins
byte hardwareConfig = COMMON_CATHODE; // segments share common cathode (-) terminal
pinMode(button, INPUT); // Make analog pin an input
sevseg.begin(hardwareConfig, numDigits, digitPins,segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
Serial.begin(9600);
}
void loop() {
const int buttonState = analogRead(button);
Serial.print(buttonState);
Serial.print("\n");
// Detect Button 1
if (1010 >= buttonState >= 993)
{
tone(Buzz, NOTE_C5);
sevseg.setChars("C");
sevseg.refreshDisplay();
} else{
noTone(Buzz);
sevseg.blank();
sevseg.refreshDisplay();
}
// Detect Button 2
if (990 >= buttonState >= 960)
{
tone(Buzz, NOTE_D5);
sevseg.setChars("D");
sevseg.refreshDisplay();
} else{
noTone(Buzz);
sevseg.blank();
sevseg.refreshDisplay();
}
// Detect Button 3
if (945 >= buttonState >= 919)
{
tone(Buzz, NOTE_E5);
sevseg.setChars("E");
sevseg.refreshDisplay();
} else{
noTone(Buzz);
sevseg.blank();
sevseg.refreshDisplay();
}
// Detect Button 4
if (850 >= buttonState >= 816)
{
tone(Buzz, NOTE_F5);
sevseg.setChars("F");
sevseg.refreshDisplay();
} else{
noTone(Buzz);
sevseg.blank();
sevseg.refreshDisplay();
}
// Detect Button 5
if (720 >= buttonState >= 670)
{
tone(Buzz, NOTE_G5);
sevseg.setChars("G");
sevseg.refreshDisplay();
} else{
noTone(Buzz);
sevseg.blank();
sevseg.refreshDisplay();
}
}
So long story short, made small piano with piezo buzzer and 5 buttons. All 5 buttons go to one analog pin to save board space. I used various resistor values for each switch to be compared wiht a 100K ohm resistor as a voltage divider. This is how i determine which button was pressed by checking range of serial read on that pin. It works with the first if statement flawlessly, playing the write note and displaying on screen, and when i let go the display clears and the note stops playing. But after the other if statements nothing works. Think i overloaded it with if statements. Tried all sorts of delay() etc in hopes of giving it time to process but no. Wiring and everything works great and have tested each individual if statement and they each work a a standalone but not all together.