Hello everyone, this is a project that will use pitches.h in Arduino. All array data inputs will be coming from a .csv file that will be read and transmitted by Python. Arduino will receive the data through Serial and process it into an array partnering up the first row of melody and duration.
Python Code
#csv file in python
import pandas as pd
import serial
import time
df = pd.read_csv(r"\\TEST\Super Mario Theme Melody and Duration.csv",skiprows=[0])
#print(df.to_string())
sendtoArduino = df.to_csv(index=False)
print(sendtoArduino)
ser = serial.Serial('COM9', 9600, timeout=1)
ser.write((sendtoArduino + '\n').encode())
ser.close()
Arduino Code
#include "pitches.h"
#define BUZZER_PIN 9
int melody[] = {
};
int durations[] = {
};
void setup() {
Serial.begin(9600);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop()
{
int size = sizeof(durations) / sizeof(int);
for (int note = 0; note < size; note++) {
//to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int duration = 1000 / durations[note];
tone(BUZZER_PIN, melody[note], duration);
//to distinguish the notes, set a minimum time between them.
//the note's duration + 30% seems to work well:
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);
//stop the tone playing:
noTone(BUZZER_PIN);
}
CSV File looks something like below, take note that the number of rows will be changing depending on the CSV File of a song, so data length varies:
| MELODY | DURATIONS |
|---|---|
| NOTE_E5 | 8 |
| NOTE_E5 | 8 |
| REST | 8 |
| NOTE_E5 | 8 |
| REST | 8 |
| NOTE_C5 | 8 |
| NOTE_E5 | 8 |
| NOTE_G5 | 4 |
So far, here are my concerns:
- I am not sure if I am transmitting the data from Python since I read that one cannot access the Serial Monitor in Arduino during Transmission of data from Python.
- If so the CSV data are received in Arduino, how can I input individual melody and duration inside the tone() function. Should this be stored in a variable inside Arduino before being called or can this be directly run in Arduino?