/**
Mini piano for Arduino.
You can control the colorful buttons with your keyboard:
After starting the simulation, click anywhere in the diagram to focus it.
Then press any key between 1 and 8 to play the piano (1 is the lowest note,
8 is the highest).
Copyright (C) 2021, Uri Shaked. Released under the MIT License.
*/
#include <stdio.h>
#include "FreeRTOS.h"
#include "ToneESP32.h"
#include "pitches.h"
#define SPEAKER_PIN 2
bool enablePiano = false;
TaskHandle_t singTaskHandle;
const uint8_t buttonPins[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_AS4, NOTE_GS4, NOTE_FS4, NOTE_DS4, NOTE_CS4,
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
typedef struct {
int toneLevel;
int quarters;
} MyTone;
const MyTone singTones[] = {
{NOTE_C4,1},{NOTE_C4,1},{NOTE_C4,1},{NOTE_E4,1},
{NOTE_D4,1},{NOTE_D4,1},{NOTE_D4,1},{NOTE_F4,1},
{NOTE_E4,1},{NOTE_E4,1},{NOTE_D4,1},{NOTE_D4,1},{NOTE_C4,2}};
void singTask(void* params){
int singLen = sizeof(singTones)/sizeof(singTones[0]);
int pitch = 0;
Serial.println("singTask");
for(;;){
for(int i=0; i<singLen; i++){
pitch = singTones[i].toneLevel;
tone(SPEAKER_PIN,pitch);
//vTaskDelay(1000 / portTICK_PERIOD_MS);
delay(250*singTones[i].quarters);
noTone(SPEAKER_PIN);
delay(250);
}
delay(1000);
enablePiano= true;
}
}
void setup() {
// Wait for USB Serial to become available
while (!Serial) {
delay(10);
}
Serial.begin(9600);
Serial.println("Piano");
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
xTaskCreate(singTask,
"singTask",
2048,
(void *)singTones,
configMAX_PRIORITIES-1,
&singTaskHandle
);
}
void loop() {
while(!enablePiano)
delay(1000);
vTaskSuspend(singTaskHandle);
//vTaskDelete(singTaskHandle);
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
pitch = buttonTones[i];
Serial.print("pitch: ");
Serial.println(pitch);
break;
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
delay(100);
}
Loading
franzininho-wifi
franzininho-wifi