hi everybody
i'm trying to automate my audio system with arduino: if audio is detected on one input ,the amp is powered up and this input is routed to the amp.
aditionnaly iuse a relay based attenuator to manage volume. if there is no audio at all, everything is turned off after 10seconds.
everything works fine exept that if the temporisation is running i cant stop it with incoming audio.
could somebody explain to me how can i do that.
thank you
here is the code:
// define inputs
int inputPin0=A0; // volume pot input
int inputPin1=A1; // input 1 signal detector
int inputPin2=A2; // input 2 signal detector
int inputPin3=A3; // input 2 signal detector
// define outputs
int latchPin = 8; // Pin connected to ST_CP of 74HC595
int clockPin = 12; // Pin connected to SH_CP of 74HC595
int dataPin = 11; // Pin connected to DS of 74HC595
int powerPin =4; // pin controling power relay
//define variables
int volume; // value sent to 74HC595, bit 0 to 5 controling relay based attenuator, bit 6&7 controling input selection relays
int in1signal; // value of signal 1
int in2signal; // value of signal 2
int in3signal; // value of signal 3
int tempo; // temporisation value
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(powerPin, OUTPUT);
pinMode(inputPin0, INPUT);
pinMode(inputPin1, INPUT);
pinMode(inputPin2, INPUT);
pinMode(inputPin3, INPUT);
volume=0;
Serial.begin (9600);
}
void loop()
{
in1signal= analogRead (inputPin1);
in2signal= analogRead (inputPin2);
in3signal= analogRead (inputPin3);
if ((in1signal!=0) && (in2signal==0) && (in3signal==0)) // if there is a signal on input1
{powerPin=HIGH; // turn power on
volume=31; // set fixed volume of tv box input
tempo=0; // reset temporisation
Serial.print ("tv box selected" ); // send to serial monitor for debug
Serial.println (volume ); // send to serial monitor for debug
}
if ((in1signal==0) && (in2signal!=0) && (in3signal==0)) // if there is a signal on input2
{powerPin=HIGH; // turn power on
volume=analogRead (inputPin0); // read volume pot
volume=map (volume, 0, 1023, 65, 127); // set volume with bit 6 high to select input2 relay
tempo=0; // reset temporisation
Serial.print ("cd selected" ); // send to serial monitor for debug
Serial.println (volume ); // send to serial monitor for debug
}
if ((in1signal==0) && (in2signal==0) && (in3signal!=0)) // if there is a signal on input3
{powerPin=HIGH; // turn power on
volume=analogRead (inputPin0); // read volume pot
volume=map (volume, 0, 1023, 192, 255); // set volume with bit 6&7 high to select input3 relay
tempo=0; // reset temporisation
Serial.print ("tuner selected" ); // send to serial monitor for debug
Serial.println (volume ); // send to serial monitor for debug
}
if (in1signal+in2signal+in3signal==0)
{
while (tempo<10)
{ delay (1000); // wait for a second
tempo++; // increase "tempo"
Serial.print ("temporisation =" ); // send to serial monitor for debug
Serial.println (tempo); // send to serial monitor for debug
}
if (tempo=30) // if tempo is 30
{volume=0; // turn off sound
Serial.println ("mute" ); // send to serial monitor for debug
delay (1000);
powerPin=LOW; // turn off power
Serial.println ("power off" ); // send to serial monitor for debug
tempo=0; // reset temporisation
}
}
// send data to hc74595
digitalWrite(latchPin, LOW); // take the latchPin low so the relays don't change while you're sending in bits:
shiftOut(dataPin, clockPin, MSBFIRST, volume); // shift out the bits:
digitalWrite(latchPin, HIGH); // take the latch pin high so the relays will be activated:
}