Ai random melody composer arduino uno r3

This is an AI random melody composer for arduino uno r3, writing the command componi in the serial monitor and pressing return key it will make a new melody everytime. Use the buzzer in pin 5 and gnd.

#define BUZZER_PIN 5 // Usa il numero del pin diretto per il buzzer

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    command.trim(); // Rimuovi eventuali spazi bianchi extra
    if (command.equalsIgnoreCase("componi")) {
      composeRandomMelody();
    }
  }
}

void composeRandomMelody() {
  for (int i = 0; i < 8; i++) {
    int randomNote = random(1, 8);
    playNote(getNoteFrequency(randomNote), 500);
    delay(100);
  }
}

void playNote(int frequency, int duration) {
  tone(BUZZER_PIN, frequency, duration);
  delay(duration + 50);
  noTone(BUZZER_PIN);
}

int getNoteFrequency(int noteIndex) {
  switch (noteIndex) {
    case 1:
      return 262; // Do
    case 2:
      return 294; // Re
    case 3:
      return 330; // Mi
    case 4:
      return 349; // Fa
    case 5:
      return 392; // Sol
    case 6:
      return 440; // La
    case 7:
      return 494; // Si
    default:
      return 0; // Nota non valida
  }
}

Maybe ChatGPT "wrote" the code, hence the reference to AI.

This is pretty astounding, though, for a number-based music generator:

Try it in Wokwi:

Sorry @jremington but Wokwi can not make sound from high frequency PWM signals :sob:
Wokwi has extra things to make the Arduino tone() function sound smooth in the simulation in the browser.

i call it AI becasue it compose by itself the music. something like an human being composer.

The notes are chosen by a pseudorandom number generator, which follows a very definite sequence defined by the particular algorithm chosen.

yes i made it with the help of chat gpt but was really hard to instruct it!

Here the code for a melody random composer for arduino uno r3 and buzzer in pin 5 and gnd:

#define BUZZER_PIN 5 // Usa il numero del pin diretto per il buzzer

void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {
  composeRandomMelody();
  delay(2000); // Pausa di 2 secondi tra le composizioni
}

void composeRandomMelody() {
  for (int i = 0; i < 8; i++) {
    int randomNote = random(1, 8);
    playNote(getNoteFrequency(randomNote), 500);
    delay(100);
  }
}

void playNote(int frequency, int duration) {
  tone(BUZZER_PIN, frequency, duration);
  delay(duration + 50);
  noTone(BUZZER_PIN);
}

int getNoteFrequency(int noteIndex) {
  switch (noteIndex) {
    case 1:
      return 262; // Do
    case 2:
      return 294; // Re
    case 3:
      return 330; // Mi
    case 4:
      return 349; // Fa
    case 5:
      return 392; // Sol
    case 6:
      return 440; // La
    case 7:
      return 494; // Si
    default:
      return 0; // Nota non valida
  }
}

And what does it produce?

random of the 8 piano notes.

No issue, no question then.
I was flying drones yesterday....

1 Like

I changed toothpaste flavor... and this...

1 Like

@toolkitman, please do not cross-post. Threads merged.

Well done @xfpd , do you think the OP will get the point?

Yes. I believe OP will see the benefit of simulation for projects (and stimulation for teeth).

If you want more complex melodies, you should use a midi index, Ive made one with 10 octaves.

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