CSV File containing data of Melody and Duration from Python, sending to Arduino for playback

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:

  1. 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.
  2. 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?

Satisfy your concern by using an Arduino with multiple hardware serial ports.
For the second concern, what are the possible parameter for the "tone" function? You cannot input melody, what ever that might be, and send it to "tone". You can send a single frequency to "tone" as you can see from the documentation.

Hi thanks for replying!

  1. If ever i will be using an Arduino Mega, can I somehow echo the serial outputs to a second serial port?
  2. I used the pitches.h library here in Arduino code so tone() have 3 parameters, namely:
    tone(pin output, melody[note], duration) as far as i've read in the documentation.

Been checking out this YouTube link by Paul McWhorter but with a different application; he used 3 integer values that will change the color of a tricolor LED.

The documentation I found says the board has four UART serial ports, so, yes, you can serial.Print(data) to any of the four.
If pitches.h allows what you want, go for it.

But you'll find that things like "NOTE_E5" are compile-time definitions that can not be used at runtime. Somewhere (probably in the python program) you'll need to convert those symbols into integers that reflect the frequency (or period) of the tone.

I believe we can transmit the "NOTE_E5, 8" as a string and have it segmented in Arduino once received in serial using readSerial until the comma. Reference project for tone() is this.

I haven't seen any documentation on how to apply this. Any idea mate?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.