Hello,
Arduino Project with Two buttons/A Piezo/An LED
I have a project with two parts
- Push a button -> turn on an LED and a Piezo with a starting pitch of 100 (working)
- 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);
Serial.begin(9600);
}
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);
noTone(2);
onOffState = 0;
}
oldButton = 1;
}
// if button is low and old button is high
else if (!button && oldButton)
{
// the button was released
oldButton=0;
}
}
// WHERE DO YOU GO YOU SILLY PIECE OF CODE!!!!!!!!!
//pitchState = digitalRead(pitchControl);
//
// if (pitchState == LOW)
// {
// startPitch = startPitch + 300;
// Serial.print("Start Pitch: ");
// Serial.println(startPitch);
// tone(2, startPitch);
// }