Added buzzer into Button example, works fer one button, but idk how to add multiple buttons.
//Defining notes
#define C0 16.35
#define Db0 17.32
#define D0 18.35
#define Eb0 19.45
#define E0 20.60
#define F0 21.83
#define Gb0 23.12
#define G0 24.50
#define Ab0 25.96
#define LA0 27.50
#define Bb0 29.14
#define B0 30.87
#define C1 32.70
#define Db1 34.65
#define D1 36.71
#define Eb1 38.89
#define E1 41.20
#define F1 43.65
#define Gb1 46.25
#define G1 49.00
#define Ab1 51.91
#define LA1 55.00
#define Bb1 58.27
#define B1 61.74
#define C2 65.41
#define Db2 69.30
#define D2 73.42
#define Eb2 77.78
#define E2 82.41
#define F2 87.31
#define Gb2 92.50
#define G2 98.00
#define Ab2 103.83
#define LA2 110.00
#define Bb2 116.54
#define B2 123.47
#define C3 130.81
#define Db3 138.59
#define D3 146.83
#define Eb3 155.56
#define E3 164.81
#define F3 174.61
#define Gb3 185.00
#define G3 196.00
#define Ab3 207.65
#define LA3 220.00
#define Bb3 233.08
#define B3 246.94
#define C4 261.63
#define Db4 277.18
#define D4 293.66
#define Eb4 311.13
#define E4 329.63
#define F4 349.23
#define Gb4 369.99
#define G4 392.00
#define Ab4 415.30
#define LA4 440.00
#define Bb4 466.16
#define B4 493.88
#define C5 523.25
#define Db5 554.37
#define D5 587.33
#define Eb5 622.25
#define E5 659.26
#define F5 698.46
#define Gb5 739.99
#define G5 783.99
#define Ab5 830.61
#define LA5 880.00
#define Bb5 932.33
#define B5 987.77
#define C6 1046.50
#define Db6 1108.73
#define D6 1174.66
#define Eb6 1244.51
#define E6 1318.51
#define F6 1396.91
#define Gb6 1479.98
#define G6 1567.98
#define Ab6 1661.22
#define LA6 1760.00
#define Bb6 1864.66
#define B6 1975.53
#define C7 2093.00
#define Db7 2217.46
#define D7 2349.32
#define Eb7 2489.02
#define E7 2637.02
#define F7 2793.83
#define Gb7 2959.96
#define G7 3135.96
#define Ab7 3322.44
#define LA7 3520.01
#define Bb7 3729.31
#define B7 3951.07
#define C8 4186.01
#define Db8 4434.92
#define D8 4698.64
#define Eb8 4978.03
// DURATION OF THE NOTES
#define BPM 140 // you can change this value changing all the others
#define H 2*Q //half 2/4
#define Q 60000/BPM //quarter 1/4
#define E Q/2 //eighth 1/8
#define S Q/4 // sixteenth 1/16
#define W 4*Q // whole 4/4
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the LED pin
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(7, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
tone(7, F3, Q);
} else {
// turn LED off:
tone(12, F3, Q);
}
//Screams in the code after!
/* if (buttonState2 == LOW) {
// turn LED on:
// tone(7, G6, Q);
// } else {
// // turn LED off:
// tone(12, F3, Q);
// }
END screaming code */
}
Also, the buzzer lags for about a second after I press the button, and lags again for half a second after said button is released. is that just lag from the button communicating w/ the board?
also, the buzzer likes to keep playing after button release on first start, until you click it again.
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == LOW) {
// play tone
tone(7, F3, Q);
//digitalWrite(LEDPin, HIGH);
} else {
// stop tone:
noTone(8);
//digitalWrite(LEDPin, LOW); this was for a led i was gonna add, but it didn't work.
}
//Screams in the code after!
/* if (buttonState2 == LOW) {
//turn LED on:
tone(7, G6, Q);
} else {
// turn LED off:
noTone(8);
}
//END screaming code */
/* if (buttonState2 == LOW) {
//turn LED on:
tone(7, G6, Q);
} else {
// turn LED off:
noTone(8);
Please tidy up your code, remove all commented out lines that don't apply or are not used. It makes it difficult to troubleshoot. Then repost the entire revised sketch as it now stands. Describe the problems.
Skipped "define" stuff cause that don't cause problems
//setting pinz
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the LED pin
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
// setting states
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0; //this is supposed to be the state for the second button, soo.... ya.
void setup() {
// initialize the buzzer pin as an output:
pinMode(7, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == LOW) {
// play tone
tone(7, F3, Q);
} else {
// stop tone:
noTone(8);//digitalWrite(LEDPin, LOW);
}
//Screams in the code after!
/* if (buttonState2 == LOW) {
//turn LED on:
tone(7, G6, Q);
} else {
// turn LED off:
noTone(8);
}
//END screaming code */
}
@slipstick Here's what I'm doing:
I'm making a synthesizer from scratch (Sorta), and I have 5 buttons for the "keys", one buzzer as the "speaker", and my arduino Uno. what I need it to do is assign a button to a note, and whenever the button is pressed, the buzzer would play the assigned note. I already have the first button up and running(W/ some lag, but that's ok), and I've been trying to get the second button to play a note, but whenever I press the first button, both notes activate. What I need help with is making the buttons completley independent of each other, so they don't clash. would u need the scematics? (R.I.P word, killed by Gingka)
You aren't really using the duration parameter to the tone() function. You're turning the tone off as soon as all the buttons are released. Theoretically.
Try this:
void setup() {
// initialize the buzzer pin as an output:
pinMode(7, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
}
void loop() {
// read the states of the pushbutton values:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed. If it is, the buttonState is LOW:
if (buttonState == LOW) {
tone(7, F3);
}
else if (buttoneState2 == LOW) {
tone(7, G3);
}
else
noTone(7);
}
Assuming you want to use the internal pull-up resistors. You haven't really said how you have the buttons wired.
how do I use the analog pins to pitchbend with a 2-pin pot? I saw one on the site(Analog Serial Read), but it had three pins. @Jimmus wasn't the noTone() on the right pin already? (Pin 7) also, thanks for pointing out the problems with my if!