Synthesizer help

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.

yall there?

Really?

    // turn LED on:
    tone(7, F3, Q);
  } else {
    // turn LED off:
    tone(12, F3, Q);

aarg:
Really?

    // turn LED on:

tone(7, F3, Q);
  } else {
    // turn LED off:
    tone(12, F3, Q);

Copied from site, fergot ta edit comments

 // 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 */

Well you forgot again.

  /* 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 */


}

Three strikes, I'm out of here. Good luck with your project.

  /* if (buttonState2 == LOW) {
       //turn LED on:
       tone(7, G6, Q);
     } else {
       // turn LED off:
       noTone(8);
     }

aarg:
Three strikes, I'm out of here. Good luck with your project.

EDITED:
I FREAKING changed that! hol' up:

/*
  Synthesizer

  This is code for a synthesizer made by @GingkathFox,
  based off of code from a website
  (Can't remeber what it was) and the "Button" example.
  Last Edit: 10:34 12/28/2017 by GingkathFox
*/
#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


//#include <notes.h>
//now, if I try to make a '.h' file with the note defines, it says "error compiling for board Arduino/Genuino Uno"
// 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
//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.... yeah.
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 buzzer on
       tone(7, G6, Q);
     } else {
       // turn buzzer off:
       noTone(8);
     }
    //END screaming code */


}

This is an international forum. Please use plain English that everyone has a fair chance to understand.

fixed.
and, how do I set different buttons to play different tones on the same buzzer? I only have one

Synthesizer.ino (3.62 KB)

If you only have one button you can't use several buttons. If you mean something else how about writing in complete sentences.

Oh and it would aid understanding if the comments in your code bore some relationship to the actual code.

Steve

@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.

i'll use the internal pullups.

It worked! what did I do wrong with the "if"?

here's the schematics:
https://drive.google.com/file/d/1IozHzMKfKCxuDN6-mz80ikpKDe7oV9uS/view?usp=sharing

Well, after you removed the extra ; there were three things:

  • INPUT_PULLUP. Otherwise your button pushes are unreliable.
  • else. Otherwise, you turn on the sound and immediately turn it off.
  • noTone() with the correct pin number. Otherwise, you're turning off sound on a pin that never got turned on.

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!

I created notes.h (Holds all the note freqs) and zipped it up, but when I tried to compile, Arduino output this message:

Arduino: 1.8.4 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\[REDACTED]lmms\Programming\Synthesizer\Synthesizer.ino:112:19: fatal error: notes.h: No such file or directory

 #include <notes.h>

                   ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Uno.

I put it in Arduino, so I dunno what happened. it compiles just fine when i un comment the defines though.

Notes.zip (763 Bytes)

Synthesizer.ino (3.8 KB)

Well, here is the code you posted:

  if (buttonState == LOW) {
    // play tone
    tone(7, F3, Q);

  } else {
    // stop tone:
    noTone(8);//digitalWrite(LEDPin, LOW);
  }

Turning the tone on pin 7 and then turning it off pin 8 is not going to work.