Я пытаюсь сделать проект Arduino, который имеет 3 функции; играть на фортепианной клавиатуре, воспроизводить произвольную песню, а также играть и записывать для воспроизведения. До сих пор я встречал все три из этих функций. Проблема в том, что у меня есть хранилище на моем Arduino, я полностью исчерпал пространство и больше не могу вставить код musi c внутрь. На данный момент у меня есть только одна песня, но я хочу добавить еще. Должен ли я получить Arduino Mega или редактировать код? -Спасибо!
Вот мой код:
#include "pitches.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3 ,2);
int notes[] = { NOTE_G4, NOTE_D6};
int button_music = 0;
int button2 =0;
int randNumber;
int key1 = 0;
int key2 = 0;
int recorded_button[200];
int recorded_time[200];
int pev_button;
char button = 0;
char button_index = 0;
int note_time;
unsigned long start_time;
char time_index;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(A5, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8, INPUT);
pinMode(9,INPUT);
// put your setup code here
}
void loop() {
button_music = digitalRead(A4);
button2 = digitalRead(A5);
key1 = digitalRead(7);
key2 = digitalRead(6);
lcd.setCursor(0,0);
lcd.print("Press button to");
lcd.setCursor(0,1);
lcd.print("play random song");
if(button_music == HIGH) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("In Progress...");
delay(5000);
randNumber = random(1,4);
Serial.println(randNumber);
if(randNumber == 1) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tetris Theme...");
delay(1000);
}
else if(randNumber == 2) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("USSR Theme...");
USSR_Theme();
delay(1000);
}
else if(randNumber == 3) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pirates of the");
lcd.setCursor(0,1);
lcd.print("Caribbean...");
delay(1000);
}
}
else if(key1 == HIGH){
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Hey You");
// delay(2000);
tone(A0,NOTE_G4,20);
}
else if (key2 == HIGH) {
tone(A0,NOTE_D6,20);
}
// 3rd funtion (record)
else if(digitalRead(9) == 1) {
lcd.clear();
delay(20);
while(digitalRead(8) == 0) {
lcd.setCursor(0, 0); lcd.print("Recording..");
lcd.setCursor(0, 1);
Detect_button();
Play_tone();
}
////////////////////////////////
while (digitalRead(8) == 1) //If the toggle switch is set in Playing mode
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Now Playing..");
for (int i = 0; i < sizeof(recorded_button) / 2; i++)
{
delay((recorded_time[i]) * 10); //Wait for before paying next tune
if (recorded_button[i] == 0)
noTone(A0); //user dint touch any button
else
tone(A0, notes[(recorded_button[i] - 1)]); //play the sound corresponding to the button touched by the user
}
}
////////////////////////////
// }
}
}
void Detect_button(){
key1 = digitalRead(7);
key2 = digitalRead(6);
pev_button = button;
if(key1 != HIGH && key2 != HIGH) {
button = 0;
}
if (key1 ==
HIGH){
button = 1;
}
if (key2 == HIGH){
button = 2;
}
/***record the pressed buttons in a array**/
if (button != pev_button && pev_button != 0)
{
recorded_button[button_index] = pev_button;
button_index++;
recorded_button[button_index] = 0;
button_index++;
}
//**end of recording**//
}
void Play_tone(){
//**record time delay between notes**//
if (button != pev_button)
{
lcd.clear();
note_time = (millis() - start_time) / 10;
recorded_time[time_index] = note_time;
time_index++;
start_time = millis();
}
//**end of recording **//
if (button == 0) {
noTone(A0);
lcd.print("Pause");
}
if (button == 1) {
tone(A0,NOTE_G4,20);
lcd.print("NOTE G4");
}
if (button == 2) {
tone(A0, NOTE_D6, 20);
lcd.print("NOTE D6");
}
}
void USSR_Theme(){
int buzzerPin = 7;
int melody[] = {
NOTE_G4, NOTE_C5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
NOTE_D4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_G4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_B4,
NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_C5,
NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_A4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
int noteDurations[] = {
8, 4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 8, 8, 4, 8, 8, 4, 8, 8, 2,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 6, 16,
4, 6, 16, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
4, 6, 16, 4, 6, 16, 4, 8, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
4, 6, 16, 4, 4, 2, 4, 4, 1
};
for (int thisNote = 0; thisNote < sizeof(melody) / 2; thisNote++) {
int noteDuration = 2000 / noteDurations[thisNote];
tone(A0, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}