Basic Two Button Piezo Project


Arduino Project with Two buttons/A Piezo/An LED

I have a project with two parts

  1. Push a button -> turn on an LED and a Piezo with a starting pitch of 100 (working)
  2. Every time the second button is pushed, it is supposed to increase the pitch of the Piezo by 300. (frustrating)

There are more elements to be added to it once I have this part working. I have dropped the code below. I commented out the code I had for increasing the pitch and had it directly over

but the second button had no effect and whenever I pushed the onOff button it increased the pitch rather than the first. 

Can someone tell me where to plop the Pitch Controller code please? 

Thank you

type or paste code here
```//button to control the pitch
const int pitchControl = 5;
int pitchState = 0;
int toner;
int startPitch = 100;

//onOff button
const int onOff = 8;
int button;
int oldButton = 0;
int onOffState = 0;

const int led = 4;

void setup() {
  // put your setup code here, to run once:
  pinMode(pitchControl, INPUT);
  pinMode(onOff, INPUT);
  pinMode(led, OUTPUT);

void loop() {

  button = digitalRead(onOff); // read the state of the onOff Button
  // if the button is high and the old button is low
  if (button && !oldButton)
    // we have a new button press
    if (onOffState == 0) //if the state is off, turn it on
      // turn the LED on and start the Piezo
      digitalWrite(led, HIGH);
      tone(2, startPitch);
      onOffState = 1;    
    else // if the state is on, turn things off 
      digitalWrite(led, LOW);
      onOffState = 0;

    oldButton = 1;
  // if button is low and old button is high
  else if (!button && oldButton)
    // the button was released

//pitchState = digitalRead(pitchControl);
//  if (pitchState == LOW)
//  {
//    startPitch = startPitch + 300;
//    Serial.print("Start Pitch: ");
//    Serial.println(startPitch);
//    tone(2, startPitch);
//  }

I have took a quick look into you sketch. Well I guess to have a button() handler would be suitable. This type of handler provides button debouncing, reading of the current button state and storage of the last button state.

ok thanks. Will have a look

Hi, I made a Wokwi simulation of your project:
Turn down the volume of your computer and press the play-button in the middle-upper of the screen.
In setup() I have added a test for the piezo and the led. Those work. But your sketch needs some work.

Wow... Thank you so much. That is amazing!
Thank you for all the effort put in.


Here's a way of doing it..

#include <mechButton.h>

#define TONE_PIN     11
#define START_FREQ   50
#define FREQ_STEP    100
#define ONOFF_BTN    2
#define PITCH_BTN    3

mechButton  onOffBtn(2);
mechButton  pitchBtn(3);
bool        sounding;
int         pitch;

void setup(void) {

   sounding = false;                      // No sound now.
   pitch    = START_FREQ;                 // We'll start here.
   onOffBtn.setCallback(onOffClicked);    // Setup callbacks for the on/off button.
   pitchBtn.setCallback(pitchClicked);    // Setup callback for the change pitch button.

void onOffClicked(void) {

   if (!onOffBtn.trueFalse()) {  // If the button has been grounded..
      if (sounding) {            // If we are playing the sound..
         noTone(TONE_PIN);       // Shut off the sound.
      } else {
         pitch = START_FREQ;     // Reset the pitch for the start.
         tone(TONE_PIN,pitch);   // Fire up the sound.
      sounding = !sounding;      // Toggle whether we are sounding or not.

void pitchClicked(void) {

   if (!pitchBtn.trueFalse()) {        // If the button has been grounded..
      if (sounding) {                  // If we are playing the sound..
         pitch = pitch + FREQ_STEP;    // Step up the sound..
         tone(TONE_PIN,pitch);         // Set the new sound.

void loop(void) { idle(); } // All we need to do is call idle90 in loop().

If you want to try it this way you'll need to install LC_baseTools from the IDE to get this to compile.

-jim lee