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:
//===============================
//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()
{
Serial.begin(9600);
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
Serial.print("switchbuzzState:");
Serial.print(switchbuzzState);
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("id:");
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("Counter:");
Serial.print(Counter);
Serial.print("FanSpeed: ");
Serial.print(FanSpeed);
Serial.println();
MotorSpeed(0,FanSpeed); // Orders motor 0 to turn at FanSpeed
MotorSpeed(1,FanSpeed); // Orders motor 1 to turn at FanSpeed
delay(100);
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.begin(buzzPin[3]);
jingle3.play(NOTE_F4, 4000);
}
if(digitalRead(hallPin[4]) == LOW){
jingle4.begin(buzzPin[4]);
jingle4.play(NOTE_G4, 4000);
}
}
else{
}
}
else { // Orders fans to not rotate if global switch is OFF
MotorSpeed(0,0);
MotorSpeed(1,0);
}
}
void MotorSpeed(int MotorID , int value) { // Definition of MotorSpeed
digitalWrite(MotorSpin[MotorID], HIGH);
analogWrite(MotorPWM[MotorID], value);
}
Thank you in advance.