0

I'm trying somehow to power OFF the car when the song plays using Arduino using Infrared (IR) Remote. I decided to play a song (SuperMario) using a Buzzer and when I press the Power ON button works fine and the song plays.

The problem is when I press Power OFF, I have to wait until the song ends to Power off the car.

I was thinking that maybe I need threads or something, but I'm not sure, or maybe there is a better way to fix this.

Here is a Demo program:

#include "IRremote.h"

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

#define powerLedRed    2
#define powerLedGreen  3
#define receiver       5
#define buzzer         7

void translateIR( void );
void powerON( void );;
void powerOFF( void );
void swap( int *x, int *y );
void playSuperMario( void );
void buzz( int targetPin, long frequency, long length );

int power           = 2;
int switchOFF       = 0;
int switchON        = 1;

int melody[] = {
  NOTE_E7, NOTE_E7, 0, NOTE_E7,  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,  NOTE_G6, 0, 0, 0,
  NOTE_C7, 0, 0, NOTE_G6,  0, 0, NOTE_E6, 0, 0,
  NOTE_A6, 0, NOTE_B6,  0, NOTE_AS6, NOTE_A6, 0,
  NOTE_G6, NOTE_E7, NOTE_G7,  NOTE_A7, 0, NOTE_F7, NOTE_G7, 0,
  NOTE_E7, 0, NOTE_C7,  NOTE_D7, NOTE_B6, 0, 0,
  NOTE_C7, 0, 0, NOTE_G6,  0, 0, NOTE_E6, 0, 0,
  NOTE_A6, 0, NOTE_B6,  0, NOTE_AS6, NOTE_A6, 0,
  NOTE_G6, NOTE_E7, NOTE_G7,  NOTE_A7, 0, NOTE_F7, NOTE_G7, 0,
  NOTE_E7, 0, NOTE_C7,  NOTE_D7, NOTE_B6, 0, 0
};

int tempo[] = {
  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,
  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,
  9, 9, 9,  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,
  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,
  9, 9, 9,  12, 12, 12, 12,  12, 12, 12, 12,  12, 12, 12, 12,
};

int underworld_melody[] = {
  NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,  NOTE_AS3, NOTE_AS4, 0,  0,
  NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,  NOTE_AS3, NOTE_AS4, 0,  0,
  NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,  NOTE_DS3, NOTE_DS4, 0,  0,
  NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,  NOTE_DS3, NOTE_DS4, 0,  0,
  NOTE_DS4, NOTE_CS4, NOTE_D4, NOTE_CS4, NOTE_DS4, NOTE_DS4, NOTE_GS3,
  NOTE_G3, NOTE_CS4, NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  NOTE_GS4, NOTE_DS4, NOTE_B3, NOTE_AS3, NOTE_A3, NOTE_GS3, 0, 0, 0
};

int underworld_tempo[] = {
  12, 12, 12, 12,  12, 12, 6,  3,
  12, 12, 12, 12,  12, 12, 6,  3,
  12, 12, 12, 12,  12, 12, 6,  3,
  12, 12, 12, 12,  12, 12, 6,
  6, 18, 18, 18,  6, 6,  6, 6,  6, 6,
  18, 18, 18, 18, 18, 18,  10, 10, 10,
  10, 10, 10, 3, 3, 3
};

IRrecv irrecv(receiver);
decode_results results;

void setup( void )
{
  pinMode(buzzer, OUTPUT);
  pinMode(powerLedGreen, OUTPUT);
  pinMode(powerLedRed,   OUTPUT);
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
}

void loop( void )
{
  if ( power == 2 ) {
    powerOFF();
  }

  if (irrecv.decode(&results)) {
    translateIR();
    irrecv.resume();
  }
}

void playSuperMario( void ) {
  Serial.println(" 'Mario Theme'");
  int size = sizeof(melody) / sizeof(int);
  for (int thisNote = 0; thisNote < size; thisNote++) {
    int noteDuration = 1000 / tempo[thisNote];
    buzz(buzzer, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    buzz(buzzer, 0, noteDuration);
  }
}

void buzz( int targetPin, long frequency, long length ) {
  digitalWrite(13, HIGH);
  long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing

  for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  }

  digitalWrite(13, LOW);
}

void translateIR( void ) {
  switch (results.value) {
    case 0xFF02FD:
      Serial.println(" -OK-");

      swap(&switchON, &switchOFF);

      if (switchON == 0 )
      {
        powerON();
        playSuperMario();
      }
      else
      {
        powerOFF();
      }
      break;
    default:
      Serial.println(" other button   ");
      power = 0;
  }
  //delay(500);
}

void powerOFF( void ) {
  digitalWrite(powerLedRed,   HIGH);
  digitalWrite(powerLedGreen, LOW);
  power = 0;
}

void powerON( void ) {
  digitalWrite(powerLedRed,   LOW);
  digitalWrite(powerLedGreen, HIGH);
}

void swap( int *x, int *y ) {
  if (*x != *y) {
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
  }
}

inside the translateIR function I have this:

void translateIR( void ) {
  switch (results.value) {
    case 0xFF02FD:
      Serial.println(" -OK-");

      swap(&switchON, &switchOFF);

      if (switchON == 0 )
      {
        powerON();
        playSuperMario();
      }
      else
      {
        powerOFF();
      }
      break;
    default:
      Serial.println(" other button   ");
      power = 0;
  }
  //delay(500);
}

Here is a VIDEO Demo. How to fix this?

2 Answers 2

2

There are two ways you can accomplish this. FYI, my buzzer is connected to pin 11 which is a PWM pin on the Arduino UNO.

  1. Get a DFPlayer mini which can play the song and all you do is press the stop function to stop the music just like in your code. (Recommended)
  2. Use MelodyWithoutDelay(Okay, but it works just as well and is less expensive)

For this answer, I will explain the MelodyWithoutDelay method.

As you have heard before, the delay function is a terrible way to stall the Arduino. This is because the Arduino can not be aware, or multitask, while it is staring at its own clock. I dealt with the same issue when I tried to "multitask" my Arduino to play music while receiving commands via Bluetooth.

For this reason, I created code a bit similar to the ToneMelody example given by Arduino. I called it MelodyWithoutDelay. It's a pretty swag name :)

Anyway, all you have to do replace my melody[] and noteDuration[] code with your melody[] and tempo[] code. And you have to change lengthOfSong to the number of notes in your melody from start to finish. You might have to include the pitches.h file from the toneMelody example code given by Arduino.

But the bare meat you should be looking at is the void melodyWithoutDelay().

Instead of creating the code applicable to you, I will create the most basic form, which is the one I show below. I need to show the MelodyWithoutDelay example to some viewers out there for their own projects too!.

Just marinate your head in the code and it should give you a good understanding on how to attack your mission.

If you want to test my code, Upload it and all you have to do is type 'g' in the Serial monitor and you should be able to pause and play the song.

If you're wondering where I used the code check out my Instructable.

Good Luck!

Here is the code:

    //Invented by Nathan Ramanathan
#include "pitches.h"  //File with all the music notes in the known universe
#define buzzer 11

unsigned long songMillis = 0;
bool check = false;
int thisNote = -1;
// notes in the melody:
//int melody[] = {
//  NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
//};

int melody[] = {//The FINAL COUNTDOWN song
  NOTE_B4, NOTE_A4, NOTE_B4, NOTE_E4, 0, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_A4, 0, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_E4, NOTE_FS4, 0, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_A4,   NOTE_G4,0,NOTE_B4,NOTE_A4,NOTE_B4,NOTE_E4,0,NOTE_C5,NOTE_B4,NOTE_C5,NOTE_B4,NOTE_A4,0,NOTE_B4,NOTE_A4,NOTE_B4,NOTE_E4,NOTE_FS4,0,NOTE_A4,NOTE_G4,NOTE_A4,NOTE_G4,NOTE_FS4,NOTE_A4,   NOTE_G4,NOTE_FS4,NOTE_G4,NOTE_A4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_A4,NOTE_G4,NOTE_FS4,NOTE_E4,NOTE_B4,NOTE_C5,NOTE_G4,0,NOTE_B4,NOTE_A4,NOTE_B4,NOTE_E4,0,NOTE_C5,NOTE_B4,NOTE_C5,NOTE_B4,NOTE_A4,    0,NOTE_C5,NOTE_B4,NOTE_C5,NOTE_E4,NOTE_FS4,0,NOTE_A4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_A4,NOTE_G4,0,NOTE_B4,NOTE_A4,NOTE_B4,NOTE_E4,0,NOTE_C5, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_A4,0, NOTE_C5,NOTE_B4,NOTE_C5,NOTE_E4,   NOTE_FS4,0,NOTE_A4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_A4,NOTE_G4        
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  16, 16, 4, 4, 2, 16, 16, 8, 8, 4, 2, 16, 16, 4, 4, 4, 4, 16, 16, 8, 8, 8, 8,  4,8,16,16,4,4,2,16,16,8,8,4,2,16,16,4,4,4,6,16,16,8,8,8,8,  2,16,16,3,16,16,8,8,8,8,4,8,1,4,8,16,16,4,4,2,16,16,8,8,8,  2,16,16,4,4,4,8,16,16,8,8,8,8,4,6,16,16,4,4,2,16,16,8,8,4,2,16,16,4,4,  4,6,16,16,8,8,8,8,4
};

const int lengthOfSong = 23+25+25+30+9; //For some reason the millis() doesn't like calculating array size.
//////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200);
  Serial.println("Type 'g' to hear a melody.");
}

void loop() {
  // no need to repeat the melody.
  unsigned long currentMillis = millis();
  if (Serial.available()) {
    int c = Serial.read();
    if (c == 'g') {               //type 'g' to start song
      check = !(check);
    }
    if (c == 'h') {               //type 'h' in the serial monitor to confirm that the arduino listens to you while it plays music!
       Serial.println("Hello");
    }
  }
  else {
    if (check) {
      MelodyWithoutDelay(currentMillis);
    }
  }
}

void MelodyWithoutDelay(long cMillis) {
  int noteDuration = 1700 / noteDurations[thisNote];
  int pauseBetweenNotes = noteDuration * 1.2;

  if ((cMillis - songMillis <= pauseBetweenNotes / 4)) {
  }
  else if ((cMillis - songMillis <= pauseBetweenNotes)) {
    tone(buzzer, melody[thisNote], noteDuration);
  }
  else {
    if (thisNote >= lengthOfSong) {
      thisNote = -2;
      check = false;
    }
    thisNote++;
    noTone(buzzer);
    songMillis = cMillis;
  }
}
1
  • Did you figure out your issue and is it working now? Jun 17, 2017 at 15:56
0

What you have to is in the for loop within function playSuperMario to check for the Off button. If so, stop the loop.

This is assuming the delay for waiting until the note is finished is ok.\

Maybe a better 'design' is not to use playSuperMario as one function, but within the loop function, to keep a boolean that defines if the song is playing or not. If playing, call a function: playNextNoteOfSuperMario. You also need to keep the index of the next to play note.

Something like:

bool CurrentlyPlaying = false; 

Maybe you can skip this variable CurrentlyPlaying since it is probably the same as (power == 2) ??. 2 means something, so use an enumeration for that.

void powerOFF( void ) {
  digitalWrite(powerLedRed,   HIGH);
  digitalWrite(powerLedGreen, LOW);
  power = 0;
  CurrentlyPlaying = false;
}

void powerON( void ) {
  digitalWrite(powerLedRed,   LOW);
  digitalWrite(powerLedGreen, HIGH);
  I would expect power also to be assigned here.
  CurrentlyPlaying = true;
}

int CurrentNote = 0; 

Set this variable again to 0 when Power On button pressed

void loop( void )
{
  if (songPlaying)
  {
      playNextNoteOfSuperMario();
  }

  if ( power == 2 ) {
    powerOFF();
  }

  if (irrecv.decode(&results)) {
    translateIR();
    irrecv.resume();
  }
}



void playNextNoteOfSuperMario( void ) {
  Serial.println(" 'Mario Theme'");
  int size = sizeof(melody) / sizeof(int);

  int noteDuration = 1000 / tempo[CurrentNote];
  buzz(buzzer, melody[CurrentNote], noteDuration);

  int pauseBetweenNotes = noteDuration * 1.30;
  delay(pauseBetweenNotes);

  buzz(buzzer, 0, noteDuration);
}
1
  • I already tried something similar (if (power == 0 ){} ) inside that loop, didn't work, or maybe I not understand you. Could you provide an Example?
    – Michi
    Jun 13, 2017 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.