Im doing a radio project and while in the loop the delay makes an audible clicking noise from the speaker the less the delay the faster the clicking.Is there a way to fix this. The delay that makes the noise is just before the switch case .criticize welcome .I've just started with trying to code
#include <Wire.h>
#define signslInputButton 2
#define stationPotIn A0
#define VoumePotIn A2
int inputState=0;
int oldInputState=0;
int inputButtonPoll=0;
int volumeState=0;
int frequencyPLL;
int volume;
int oldVolume=0;
int volumeConverted=0;
int oldPotState;
int potState = 0;
int potStateConverted;
float station;
const int numReadings =2;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0;
const int numReadings2 =2;
int readings2[numReadings2]; // the readings from the analog input
int readIndex2 = 0; // the index of the current reading
int total2 = 0;
float stationArray[100]={88.1,88.3,88.5,88.7,88.9,89.1,89.3,89.5,89.7,89.9,
90.1,90.3,90.5,90.7,90.9,91.1,91.3,91.5,91.7,91.9,
92.1,92.3,92.5,92.7,92.9,93.1,93.3,93.5,93.7,93.9,
94.1,94.3,94.5,94.7,94.9,95.1,95.3,95.5,95.7,95.9,
96.1,96.3,96.5,96.7,96.9,97.1,97.3,97.5,97.7,97.9,
98.1,98.3,98.5,98.7,98.9,99.1,99.3,99.5,99.7,99.9,
100.1,100.3,100.5,100.7,100.9,101.1,101.3,101.5,101.7,101.9,
102.1,102.3,102.5,102.7,102.9,103.1,103.3,103.5,103.7,103.9,
104.1,104.3,104.5,104.7,104.9,105.1,105.3,105.5,105.7,105.9,
106.1,106.3,106.5,106.7,106.9,107.1,107.3,107.5,107.7,107.9};
int volumeArray[40]={0x60,/*0x5f,*/0x5e,/*0x5d,*/0x5c,/*0x5b,*/0x5a,/*0x59,*/0x58,/*0x57,*/0x56,/*0x55,*/0x54,/*0x53,*/0x52,/*0x51,*/0x50,
/*0x4f,*/0x4e,/*0x4d,*/0x4c,/*0x4b,*/0x4a,/*0x49,*/0x48,/*0x47,*/0x46,/*0x45,*/0x44,/*0x43,*/0x42,/*0x41,*/0x40,
/*0x3f,*/0x3e,/*0x3d,*/0x3c,/*0x3b,*/0x3a,/*0x39,*/0x38,/*0x37,*/0x36,/*0x35,*/0x34,/*0x33,*/0x32,/*0x31,*/0x30,
/*0x2f,*/0x2e,/*0x2d,*/0x2c,/*0x2b,*/0x2a,/*0x29,*/0x28,/*0x27,*/0x26,/*0x25,*/0x24,/*0x23,*/0x22,/*0x21,*/0x20,
/*0x1f,*/0x1e,/*0x1d,*/0x1c,/*0x1b,*/0x1a,/*0x19,*/0x18,/*0x17,*/0x16,/*,0x15,*/0x14,/*,0x13,*/0x12/*,0x11,0x10,
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f*/};
void setup() {
pinMode(VoumePotIn,INPUT);
pinMode(stationPotIn,INPUT);
pinMode(signslInputButton,INPUT_PULLUP);
Wire.begin();
Serial.begin (9600);
Wire.beginTransmission( 0x44 );
Wire.write( 0x20 ); // deault settings to get audio ic started
Wire.write( 0x78 );
Wire.write( 0x00 );
Wire.write( 0x81 );
Wire.write( 0x60 );
Wire.write( 0xff );
Wire.write( 0x00 );
Wire.write( 0x1f );
Wire.write( 0x10 );
Wire.write( 0x00 );
Wire.write( 0x07 );
Wire.write( 0x05 );
Wire.write( 0x05 );
Wire.write( 0x05 );
Wire.write( 0x05 );
Wire.write( 0x8f );
Wire.write( 0x05 );
Wire.write( 0xdf );
Wire.write( 0x00 );
Wire.endTransmission();
// used for smoothing volume pot input
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
// used for smoothing station pot input
for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++) {
readings2[thisReading2] = 0;
}
}
void loop(){
// used for smoothing volume pot input
total = total - readings[readIndex]; // subtract the last reading:
readings[readIndex] = analogRead(VoumePotIn); // read from the pot:
total = total + readings[readIndex]; // add the reading to the total:
readIndex = readIndex + 1; // advance to the next position in the array:
if (readIndex >= numReadings) { // ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
volumeState = total / numReadings;
// delay in between reads for stability
delay(1);
// dont write unless pot has rotated
if(abs(volumeState-oldVolume)>9){
oldVolume=volumeState;
volumeConverted=map(volumeState,0,1023,0,39);
volume=volumeArray[volumeConverted];
Wire.beginTransmission( 0x44 );
Wire.write( 0x03 );
Wire.write( volume );
Wire.endTransmission();
Serial.println(volume);
}
//////////////////////////////////////////////////////////////////
// used for smoothing station pot input
total2 = total2 - readings2[readIndex2]; // subtract the last reading:
readings2[readIndex2] = analogRead(stationPotIn); // read from the pot:
total2 = total2 + readings2[readIndex2]; // add the reading to the total:
readIndex2 = readIndex2 + 1; // advance to the next position in the array:
if (readIndex2 >= numReadings2) { // if we're at the end of the array...
readIndex2 = 0; // ...wrap around to the beginning:
}
// calculate the average:
potState = total2 / numReadings2;
// delay in between reads for stability
delay(1);
// dont write unless pot has rotated
if(abs(potState-oldPotState)>5){
oldPotState=potState;
potStateConverted=map(potState,0,1023,0,100);
station=stationArray[potStateConverted];
frequencyPLL = 4 * ( station * 1000000 + 225000 ) / 32768 ;
//data sheet for FM ic said add this delay
delay(100);
//write radio station to fm radio ic
Wire.beginTransmission( 0x60 );
Wire.write( highByte(frequencyPLL) );
Wire.write( lowByte(frequencyPLL) );
Wire.write( 0xB0 );
Wire.write( 0x17 );
Wire.write( 0x00 );
Wire.endTransmission();
Serial.println(station);
}
//////////////////////////////////////////////////////////////////
//select FM/AUX/BLUETOOTH and remember selection
// and debounce button
inputButtonPoll=digitalRead(signslInputButton);
if(inputButtonPoll == 0){
delay(50);
inputButtonPoll=digitalRead(signslInputButton);
if(inputButtonPoll == 1){
inputState = oldInputState +1 ;
Serial.println(inputState);
}
}
//if button has not been pressed wait 100ms
else{
delay(50);
}
switch (inputState) {
case 1:
// program audio ic for AUX input
Wire.beginTransmission( 0x44 );
Wire.write( 0x00);
Wire.write( 0x78 );
Wire.endTransmission();
// remember selection
oldInputState = inputState;
break;
case 2:
// program audio ic for FM input
Wire.beginTransmission( 0x44 );
Wire.write( 0x00 );
Wire.write( 0x79 );
Wire.endTransmission();
// remember selection
oldInputState = inputState;
break;
case 3:
//program audio ic for Bluetooth input
Wire.beginTransmission( 0x44 );
Wire.write( 0x00 );
Wire.write( 0x7b );
Wire.endTransmission();
oldInputState = 0;
break;
}
}