Go Down

Topic: Playing a melody simultaneously on multiple buzzers (Read 198 times) previous topic - next topic


Hi there !

I am currently working on a project where 4 Hall Effect Sensors drive 4 different buzzers. Basically if for example hall effect sensor #1 is activated (via a magnet) then buzzer 1 plays a note (ultimately I want it to play a melody). Now if hall sensor 1 AND hall sensor 2 are simultanesously activated then the two associated buzzers, buzzer 1 and buzzer 2, play the note simultaneously.

I spend two days trying to make it work. I am aware the arduino (I use the MEGA 2560) have timer limitations this is why I use the ToneLibrary : https://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation. Basicaly it allows to play a note without blocking the code.

I tried to code it, however the piezzo buzzer do not act as intended: even if two or three hall sensors are activated, only one buzzer play a sound. Moreover after few tries, the whole circuit seems to block and my on/off do not have effect anymore.

For you to understand my code, be aware that in parallel the hall effect sensors also drive the rotating speed of two fans: the more hall effect sensors are activated the faster the fans turns. I got this part working using an additional motorshield.

I need help on this one, I do not see where my code is wrong and my understandment of the Tone Library is limited.

My current code:
Code: [Select]
//Made by PAD and VM on the 08/07/2014. Ver 3.1

#include <Tone.h>

// Definition of Motor Variables
int MotorSpin[2] = {12,13}; 
int MotorPWM[2] = {3,11}; 

const int hallPin[4] = {6,7,8,9};        // Pins attributed to hall effect sensors
const int buzzPin[4] = {50,46,42,38};    // Pins attributed to buzzers

//switches initialisation
const int switchPin = 30;                // Pin attributed to the global on/off switch
const int switchbuzzPin = 32;            // Pin attributed to the buzzers on/off switch

int switchState= 0;                      // Initialisation of the global on/off switch
int switchbuzzState =0;                  // Initialisation of the buzzer on/off switch

// Definition of the tones
Tone jingle1;
Tone jingle2;
Tone jingle3;
Tone jingle4;

void setup()
  pinMode(switchPin, INPUT);   
  pinMode(switchbuzzPin, INPUT);
  for (int i = 0; i<4;i++){
    pinMode(hallPin[i], INPUT);   
    pinMode(buzzPin[i], OUTPUT);

void loop()
   switchState=digitalRead(switchPin);            // Reading switchPin state
   switchbuzzState=digitalRead(switchbuzzPin);    // Reading switchbuzzPin state

  if (switchState == HIGH)                         // Checking if global switch is ON
    int Counter = 0;
    for (int i=0;i<4;i++)
      if (digitalRead(hallPin[i]) == LOW )         // Checking which hall effect sensor is activated
        Counter++ ;                                // Incrementing the counter
        Serial.print(i);                           // Printing the number of the activated hall sensors

int FanSpeed ;                                    // Definition of FanSpeed
if (Counter==0){
  FanSpeed=70;                                     // Set the fanSpeed to 70 if no hall effect activated, minimum fanspeed to break inertia
else {
  FanSpeed = map(Counter,1,4,95,255);              // Rises FansSpeed with each activated hall sensor

    Serial.print("FanSpeed: ");

    MotorSpeed(0,FanSpeed);                      // Orders motor 0 to turn at FanSpeed
    MotorSpeed(1,FanSpeed);                      // Orders motor 1 to turn at FanSpeed
  if(switchbuzzState == LOW){                    // Checks if the buzzers on/off switch is ON
      if(digitalRead(hallPin[1]) == LOW){        // Checks if hall sensor 1 is activated
        jingle1.begin(buzzPin[1]);               // Prepares buzzer 1 to play
        jingle1.play(NOTE_C4, 4000);             // Orders buzzer 1 to play C4 during 4 sec
      if(digitalRead(hallPin[2]) == LOW){        // Checks if hall sensor 2 is activated
        jingle2.begin(buzzPin[2]);               // Prepares buzzer 2 to play
        jingle2.play(NOTE_D4, 4000);             // Orders buzzer 2 to play D4 during 4 sec
      if(digitalRead(hallPin[3]) == LOW){
        jingle3.play(NOTE_F4, 4000);
      if(digitalRead(hallPin[4]) == LOW){
        jingle4.play(NOTE_G4, 4000);

else {                                         // Orders fans to not rotate if global switch is OFF

void MotorSpeed(int MotorID , int value) {     // Definition of MotorSpeed
  digitalWrite(MotorSpin[MotorID], HIGH);
  analogWrite(MotorPWM[MotorID], value);   

Thank you in advance.



Ok so actually, to get the Tone library to work on the Mega 2560, I had to replace all instances of this: #if defined(AVR_ATmega1280) And replaced it with this: #if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)  in library file Tone.cpp.
Now the sound are correctly distributed simultaneously to the buzzers.

However I have another issue : as soon as the buzzers switch is ON, so as soon as the sound is distributed to the buzzers, the fan stop rotating ! Any idea on this ?

Thank you.


Go Up