I am fairly new to Arduino and C++. I am currently trying to create a simple Arduino piano which has 4 different notes playing through a piezzo after a button gets pressed. I am trying to implement a potentiometer to regulate the volume and a small crystal led display to display a random symbol for each note. However I am currently stuck on the last 2 steps. Could anybody tell me how I could fix the issue of my potentiometer not regulating the volume, how I can implement a led display?
int but1 = 2;
int but2 = 3;
int but3 = 4;
int but4 = 5;
int buzzer = 13;
int led1 = 6;
int led2 = 7;
int led3 = 8;
int led4 = 9;
int potPin = A0;
void setup() {
pinMode(but1, INPUT);
pinMode(but2, INPUT);
pinMode(but3, INPUT);
pinMode(but4, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop() {
int volume = analogRead(potPin);
int mappedVolume = map(volume, 0, 1023, 0, 255);
int b1 = digitalRead(but1);
int b2 = digitalRead(but2);
int b3 = digitalRead(but3);
int b4 = digitalRead(but4);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
if (b1 == 1) {
tone(buzzer, 300, mappedVolume);
digitalWrite(led1, HIGH);
}
if (b2 == 1) {
tone(buzzer, 400, mappedVolume);
digitalWrite(led2, HIGH);
}
if (b3 == 1) {
tone(buzzer, 500, mappedVolume);
digitalWrite(led3, HIGH);
}
if (b4 == 1) {
tone(buzzer, 600, mappedVolume);
digitalWrite(led4, HIGH);
}
delay(10);
}
Please see the description of the tone()
function - there is no volume control on it.
PS Please edit your post #3 and insert the code in the code tags
@digitsathome - Do this...
... and you will see one extra close brace that will stop your compiler.
The buttons work, but the tone() needed help. The third parameter of tone() is "duration" not volume. The piezo has no volume, it is stationary or moving, off or on. Always format your code and paste it using the < CODE > button.
int but1 = 2;
int but2 = 3;
int but3 = 4;
int but4 = 5;
int buzzer = 13;
int led1 = 6;
int led2 = 7;
int led3 = 8;
int led4 = 9;
int potPin = A0;
void setup() {
Serial.begin(115200);
pinMode(but1, INPUT);
pinMode(but2, INPUT);
pinMode(but3, INPUT);
pinMode(but4, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop() {
// int volume = analogRead(potPin);
// int mappedVolume = map(volume, 0, 1023, 0, 255);
int b1 = digitalRead(but1);
int b2 = digitalRead(but2);
int b3 = digitalRead(but3);
int b4 = digitalRead(but4);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
// tone(pin, frequency, duration)
if (b1 == 1) {
// tone(buzzer, 300, mappedVolume);
tone(buzzer, 300, 20);
digitalWrite(led1, HIGH);
}
if (b2 == 1) {
// tone(buzzer, 400, mappedVolume);
tone(buzzer, 400, 20);
digitalWrite(led2, HIGH);
}
if (b3 == 1) {
// tone(buzzer, 500, mappedVolume);
tone(buzzer, 500, 20);
digitalWrite(led3, HIGH);
}
if (b4 == 1) {
// tone(buzzer, 600, mappedVolume);
tone(buzzer, 600, 20);
digitalWrite(led4, HIGH);
}
delay(10);
}
The toneAC library can set a volume, and it works. It is not a full smooth volume control, but it is possible to design a fading "ping" and other sounds.
https://bitbucket.org/teckel12/arduino-toneac/wiki/Home
The Wokwi simulator has a piano: mini-piano.ino - Wokwi ESP32, STM32, Arduino Simulator
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.