drakerdg
Published © GPL3+

Play musical notes using tone() using Arduino Nano

This is an example of using the tone () function on an Arduino Nano. In the same way you can apply other Arduinos and the like

IntermediateProtip3 hours4,594

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
LED (generic)
LED (generic)
×4
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×2
Resistor 221 ohm
Resistor 221 ohm
×4
Resistor 10k ohm
Resistor 10k ohm
×2
Buzzer
Buzzer
×1
Perma-Proto Breadboard Half Size
Perma-Proto Breadboard Half Size
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Play musical notes using tone() πŸ”ŠπŸŽΆπŸŽΆπŸŽΆ

Code

Play musical notes using tone() πŸ”ŠπŸŽΆπŸŽΆπŸŽΆ

C/C++
This is an example of using the tone () function on an Arduino Nano. In the same way you can apply other Arduinos and the like.
const int LED_GRN = 8;
const int LED_RED = 6;
const int LED_BLE = 4;
const int LED_YLW = 2;

const int BUZZER = 13;

const int BTN1 = 5;
const int BTN2 = 3;
const int POT = A0;

const int MeSderM[] [3] = {{60 ,6, LED_GRN}, {63 ,8, LED_RED}, {67 ,2, LED_BLE}, {66 ,6, LED_BLE}, {63 ,8, LED_RED}, {60 ,2, LED_GRN}, {60 ,6, LED_GRN}, {63 ,8, LED_RED}, {67 ,6, LED_BLE}, {68 ,8, LED_YLW}, {67 ,8, LED_YLW}, {66 ,6, LED_BLE}, {63 ,6, LED_RED}, {60 ,2, LED_GRN}, {65 ,6, LED_RED}, {68 ,8, LED_BLE}, {72 ,2, LED_YLW}, {71 ,6, LED_YLW}, {68 ,8, LED_BLE}, {65 ,2, LED_RED}, {60 ,6, LED_GRN}, {63 ,8, LED_BLE}, {67 ,2, LED_YLW}, {66 ,6, LED_BLE}, {63 ,8, LED_RED}, {60 ,2, LED_GRN}, {68 ,6, LED_YLW}, {67 ,1, LED_BLE}, {67 ,8, LED_BLE}, {65 ,8, LED_RED}, {63 ,8, LED_GRN}, {65 ,8, LED_RED}, {63 ,8, LED_GRN}, {60 ,1, LED_GRN}, {67 ,6, LED_RED}, {68 ,8, LED_BLE}, {70 ,2, LED_YLW}, {68 ,6, LED_YLW}, {67 ,2, LED_BLE}, {65 ,6, LED_RED}, {63 ,8, LED_GRN}, {65 ,2, LED_RED}, {63 ,6, LED_GRN}, {62 ,8, LED_RED}, {63 ,2, LED_GRN}, {67 ,6, LED_RED}, {68 ,8, LED_BLE}, {70 ,2, LED_YLW}, {68 ,6, LED_YLW}, {67 ,2, LED_BLE}, {65 ,6, LED_RED}, {63 ,8, LED_GRN}, {65 ,2, LED_RED}, {63 ,6, LED_GRN}, {65 ,8, LED_RED}, {67 ,1, LED_BLE}, {67 ,8, LED_BLE}, {65 ,8, LED_RED}, {63 ,8, LED_GRN}, {65 ,8, LED_RED}, {63 ,8, LED_GRN}, {60 ,1, LED_GRN}, {67 ,6, LED_BLE}, {65 ,6, LED_RED}, {63 ,8, LED_GRN}, {65 ,6, LED_RED}, {67 ,6, LED_BLE}, {74 ,1, LED_YLW}};

const int LEDS[] = {LED_GRN, LED_RED,  LED_BLE, LED_YLW};

int PotV = 0;
int AntO = 0;
int OctX = 0;

void Btn2Read(){
  if (digitalRead(BTN2)==HIGH){
    OctX=0;
  } 
}

void melodyY(int RepX, int iniX, int endX) {
	for (int RepI = 0; RepI < RepX; RepI++) {
        for (int thisNote = iniX; thisNote < endX; thisNote++) {
          PotV = map(analogRead(POT), 0, 1023, -3, 4);
          if (AntO != PotV){
            AntO = PotV;
            OctX = PotV;
          }
          Btn2Read();
          int noteDuration = 1000 / MeSderM[thisNote][1];
          digitalWrite(MeSderM[thisNote][2], HIGH);
          float NteX = MeSderM[thisNote][0]+OctX*12-69;
          NteX = pow(2, NteX/12)*440;
          String str1 = "Eighth: " + String(4+OctX) + "; Freq: " + String(NteX,3);
          Serial.println(str1);
          tone(BUZZER, NteX, noteDuration);
          int pauseBetweenNotes = noteDuration * 1.30;
          delay(pauseBetweenNotes*2/3);
          Btn2Read();
          digitalWrite(MeSderM[thisNote][2], LOW);
          delay(pauseBetweenNotes/3);
          Btn2Read();
          noTone(BUZZER);   
        }    
    }
}

void setup() {
    Serial.begin(9600);

    for(int i=0; i<4; i++){
      pinMode(LEDS[i], OUTPUT);
      digitalWrite(LEDS[i], LOW);
    }
  pinMode(BTN1, INPUT);
  pinMode(BTN2, INPUT);  
}

void loop() {

  if (digitalRead(BTN1)==HIGH)
  {
    melodyY(2,0,34);
    melodyY(1,34,56);
    melodyY(1,0,34);
    melodyY(1,56,68);
  }
  Btn2Read();
}

Credits

drakerdg

drakerdg

4 projects β€’ 21 followers

Comments