arduino-volume1
arduino-volume1 copied to clipboard
Beeping when playing sweeped tones IE. siren
HI , I am having a problem with Volume version 1.1.2. The library seems to function correctly, however I get a random Beeping sound while playing a swept tone ( Siren sound). The problem sound is a short beep sound that plays randomly. It seems to occur more when the volume is low. It occurs much less when the volume is at its highest, but it still happens. I can't locate the cause and I have included the first version of my code below. Your comments would be most appreciated. Thanks for the great work you have done!! I am running this on an arduino nano with the speaker output on pin 5. This code reads the PPM signal from channels 5 and 6 of an RC receiver. One channel controls the volume using Volume V1.1.2. , the other selects different options: 1) no lights no siren 2) LightS only, 3) Lights, swept up and down siren, 4) Lights and swept up siren.
Code to follow: #include "Volume.h" // Include the Volume library #define rcPin1 2 //8 // Pin 8 Connected to CH1 of Transmitter #define rcPin2 3 //9 // Pin 9 Connected to CH2 #define RedLights 4 //Red lights pin // Speaker 5 //Siren speaker #define BlueLights 6 //BlueLights pin
int ch1 = 0; // Receiver Channel 1 PPM value int ch2 = 0; // Receiver Channel 2 PPM value Volume vol; // Plug your speaker into the default pin for your board type: // https://github.com/connornishijima/arduino-volume1#supported-pins int lowFreq = 300; int highFreq = 700; int durFreq = 2; long Mynow=0; void setup() { pinMode(RedLights, OUTPUT); pinMode (BlueLights, OUTPUT); pinMode(rcPin1, INPUT); pinMode(rcPin2, INPUT); Serial.begin(9600); }
void loop() { // Read in the length of the signal in microseconds ch1 = pulseIn(rcPin1, HIGH); // (Pin, State, Timeout) rc reciever ch 5 and 6 ch2 = pulseIn(rcPin2, HIGH); Serial.print("Ch1: "); Serial.print(ch1); Serial.print(" Ch2: "); Serial.print(ch2); //delay(500); int sound = 0 ; //Serial.println("here"); if (ch1 <= 1150){ sound = 1; } if (ch1 > 1150 && ch1 <=1400){ sound = 2; } if (ch1 > 1400 && ch1 <=1650){ sound = 3; } if (sound == 0 ) { sound = 4; }
int volume = map(ch2,970,1950,20,255); if (volume <60 ){volume=60;} if (volume >255){volume=255;} byte V=char(volume); //cast int to a byte value for use in Volume library
Serial.print(" sound: "); Serial.print(sound); Serial.print(" volume: "); Serial.print(volume);
vol.begin(); // After calling this, delay() and delayMicroseconds will no longer work // correctly! Instead, use vol.delay() and vol.delayMicroseconds() for // the correct timing
vol.setMasterVolume(1.0); // Self-explanatory enough, right? Try lowering this value if the speaker is too loud! (0.00 - 1.00) //vol.delay(500); switch (sound) { case 1: //no Sound No Flashing Serial.println(" - Using Case 1"); digitalWrite(RedLights, LOW); vol.tone(0,0); break;
case 2: // flashing lights no sound V=char(0); flashLights(); Serial.println(" - Using Case 2"); vol.tone(0,0); break;
case 3: // Lights and random siren sounds Serial.println(" - Using Case 3"); for (int i=1; i<random(2,3); i++){ SirenUp (V); } // for (int i=1; i<random(3,5); i++){ // SirenWhoop (V);} break;
case 4: //Lights and Whooping Siren
Serial.println(" - Using Case 4");
SirenWhoop (V);
break;
default: Serial.print(" - Using none"); } vol.noTone(); vol.end(); } void SirenWhoop(byte volume) { durFreq = 2; int freq=1; for (freq=lowFreq; freq <highFreq; freq+=6
) { delay(100); digitalWrite(RedLights,HIGH); vol.tone(freq,volume); vol.delay(durFreq); vol.noTone(); digitalWrite(RedLights, LOW); }
}
void SirenUp(byte volume) { durFreq = 2; int freq=1; for (freq=lowFreq; freq <highFreq; freq+=2) {
digitalWrite(RedLights,HIGH);
vol.tone(freq,volume);
vol.delay(durFreq);
vol.noTone();
digitalWrite(RedLights, LOW);
}
for (freq=highFreq; freq >lowFreq; freq-=2 )
{
digitalWrite(RedLights, LOW);
vol.tone(freq,volume);
vol.delay(durFreq);
vol.noTone();
digitalWrite(RedLights, HIGH);
}
}
void flashLights() {
digitalWrite(RedLights, HIGH);
delay (20000);
digitalWrite(RedLights, LOW);
delay (20000);
digitalWrite(RedLights, HIGH);
delay (2000);
digitalWrite(RedLights, LOW);
delay (2000);
digitalWrite(RedLights, HIGH);
delay (2000);
digitalWrite(RedLights, LOW);
delay (2000);
digitalWrite(RedLights, HIGH);
delay (2000);
digitalWrite(RedLights, LOW);
delay (2000);
digitalWrite(RedLights, HIGH);
delay (20000);
digitalWrite(RedLights, LOW);
delay (20000);
}