# Programming question, possible timer issue??

I am trying to write a program where if an input goes over a certain voltage then an output LED will blink. I wish to keep this LED blinking for 10s and if the input is still high after the 10 seconds then I would like turn off the blinking LED and make another 2 outputs high.
Any idea how I would go about it?

Cheers

Dave

Any idea how I would go about it?

Yes. The question is what parts of what you want to do are you having issues with? Detecting when an input transitions from below the threshold to above the threshold is simple.

Detecting when the transition from above the threshold to below occurs is easy.

Determining if now minus then exceeds a value is trivial.

Determining whether it is time to toggle the state of the LED is trivial. Doing so is even more trivial.

Turning two pins on is a rather simple matter.

So, where is the hard part?

state 0:
state = 1;

state 1:
startTime = millis();
state = 2;

state 2:
currentTime = millis();
if (currentTime-startTime > 10000) {
state = 3;
else
state = 0;
}

state 3:
turn on two outputs.

First get a reading of the pin at the required voltage, make a note of its value. "IF" incoming voltage is equal to the measured value, get the current millis() and store it in a variable. Now use another IF statement to check the difference of the new current "millis() - the stored millis " and see if it is greater than 10000UL (10 seconds), and still high. If it is greater than 10 seconds and still high, LED OFF and your 2 pins ON.

So you need to code in 3 IF statements. Check voltage, set timer, check timer and voltage. The rest after that is up to you.

Basically this code is for a burgular alarm. What I want it to do is monitor the input coming from a PIR and another input coming from a mic. All the electronics is sorted, I am just having a bit of trouble with the code. The aim is to get the code so that when eitherof inputs are high, an LED will flash. Then after a delay of 10s, if the input is still high, the Arduino will go low on the output to the blinking LED and go high on another 2 outputs. These outputs are going to another LED and a buzzer. My code does not seem to be working at all and I am getting high outputs even if none of the inputs are high. Any ideas peeps????

``````int MIC_IN_A1 = 1; //Declares an integer of Mic input
int PIR_IN_A2 = 2; //Declares an integer of PIR input
int RESET_A3 = 3; //Declares an integer of Reset input
int LED_PIR = 11; //Declares an integer of LED for PIR
int BUZZER_PIR = 12; // Declares and integer of buzzer for PIR
int LED_MIC = 9; //Declares an integer of LED for MIC
int BUZZER_MIC = 10; // Declares and integer of buzzer for MIC
int LED_ WARN_MIC = 2; //Declares an integer of LED MIC warning
int LED_WARN_PIR = 3; // Declares and integer of LED PIR warning

long aRead = 0;  //Assumes voltage range from input

void setup()
{
Serial.begin(9600);  //Serial console for debugging
pinMode(LED_PIR, OUTPUT);  //Sets led PIR as an output
pinMode(BUZZER_PIR, OUTPUT);    //Sets buzzer PIR as an output
pinMode(LED_MIC, OUTPUT);  //Sets led MIC as an output
pinMode(BUZZER_MIC, OUTPUT);    //Sets buzzer MIC as an output
pinMode(LED_ WARN_MIC, OUTPUT);  //Sets Mic LED Warning as an output
pinMode(LED_WARN_PIR, OUTPUT);    //Sets PIR LED Warning as an output
}

StartTime = millis (); // should this be declared elsewhere?
CurrentTime = millis (); // should this be declared elsewhere?

{
for(int i=0; i < 10; i++)   //Takes 10 readings
{
float voltage = (aRead * 5.0)/1024.0;  //Converts to digital
Serial.print(" Voltage on pin ");
Serial.print(pin);
Serial.print(" is ");
Serial.println(voltage);
return voltage*1000;       //converts to millivolts
}
}

void loop()
{
bool ResetActive = false;  //sets a flag for reset which will be used later
bool MicActivate= false; // sets a flag for the MIC being activated
bool BlinkMicWarnLED = false; //sets a flag of Mic being triggered
bool PirActivate = false; // sets a flag of the PIR being active
bool BlinkPirWarnLED = false; // Sets a flag of the PIR being triggered
float voltage = readVoltage(MIC_IN_A1);   //reads the voltage on the MIC in pin
if(voltage >= 4000 )
{   //If voltage more than 4V
BlinkMicWarnLED = true;  //makes my flag true if the criteria is met
}

if(CurrentTime-StartTime > 10000); // Counts upto 10 seconds
{
if(voltage >= 4000)  //reads voltage again after delay
BlinkMicWarnLED = false;  //makes my flag falseif the criteria is met
MicActivate = true; //makes the Mic alarms flag true
}

voltage = readVoltage(PIR_IN_A2);   //reads the floating voltage on the PIR in pin
if( voltage >= 4000 )  //If voltage more than 4V
{
BlinkPirWarnLED = true;  //makes my flag true if the criteria is met
}
if( CurrentTime-StartTime > 10000); // Counts upto 10 seconds
{
if (voltage >= 4000 )  //reads voltage again after delay
BlinkPirWarnLED = false;  //makes my flag falseif the criteria is met
PirActivate = true; //makes the Mic alarms flag true
}

voltage = readVoltage(RESET_A3);   //reads the floating voltage on the Reset in pin
if(voltage >= 4000)  //If voltage more than 4V
ResetActive = true;  //makes my flag true if the criteria is met

if (BlinkMicWarnLED == true)  //If this is true depending on the above criteria being met
{
Serial.println("MIC Warning...");
digitalWrite(LED_ WARN_MIC, OUTPUT, HIGH); //Gives 5V output to LED
delay(100); // Keeps LED on for 100mS
digitalWrite(LED_WARN_MIC, OUTPUT, LOW); //Turns off LED warning output
}
else if(MicActivate == true) //Mic has been activated for longer than delay
{
Serial.println("Burglar picked up by MIC...");
digitalWrite(LED_MIC, HIGH); //Gives an output to MIC LED
digitalWrite(BUZZER_MIC, HIGH); //Gives an  output to MIC Buzzer
}
else if(BlinkPirWarnLED == true)  //If this is true depending on the above criteria being met
{
Serial.println("PIR Warning...");
digitalWrite(LED_ WARN_PIR, HIGH); //Gives 5V output to LED
delay(100); // Keeps LED on for 100mS
digitalWrite(LED_ WARN_PIR,  LOW); //Turns off LED warning output
}
else if(PirActivate == true) //PIR has been activated for longer than delay
{
Serial.println("Burglar picked up by PIR...");
digitalWrite(LED_PIR, HIGH); //Gives an output to PIR LED
digitalWrite(BUZZER_PIR, HIGH); //Gives an  output to PIR Buzzer
}

else if(ResetActive == true) //If the reset switch is activated
{
Serial.println("Reset Is Activated...");
digitalWrite(BUZZER_MIC, LOW); //No output Mic buzzer
digitalWrite(LED_MIC, LOW); //No output to Mic LED
digitalWrite(BUZZER_PIR, LOW); //No output PIR buzzer
digitalWrite(LED_PIR, LOW); //No output to PIR LED
}
else // If nothing at all is activated
{
Serial.println("All Is Clear...");
digitalWrite(BUZZER_MIC, LOW); //No output Mic buzzer
digitalWrite(LED_MIC, LOW); //No output to Mic LED
digitalWrite(BUZZER_PIR, LOW); //No output PIR buzzer
digitalWrite(LED_PIR, LOW); //No output to PIR LED
digitalWrite(LED_ WARN_PIR, LOW);  // No output  to PIR warning LED
digitalWrite(LED_ WARN_MIC, LOW);  // No output  to MIC warning LED
}
delay(100);  //100mS delay between measurements
}
``````
``````int LED_ WARN_MIC = 2; //Declares an integer of LED MIC warning
``````

No matter your desires, you can’t have spaces in variable names.

``````StartTime = millis (); // should this be declared elsewhere?
CurrentTime = millis (); // should this be declared elsewhere?
``````

You can’t have untyped variables, either.

``````  for(int i=0; i < 10; i++)   //Takes 10 readings
{
float voltage = (aRead * 5.0)/1024.0;  //Converts to digital
Serial.print(" Voltage on pin ");
Serial.print(pin);
Serial.print(" is ");
Serial.println(voltage);
return voltage*1000;       //converts to millivolts
}
``````

Why is there a return in the for loop?

There is no reason to convert the reading to millivolts. Just use the raw (integer) reading.

``````     digitalWrite(LED_ WARN_MIC, OUTPUT, HIGH); //Gives 5V output to LED
delay(100); // Keeps LED on for 100mS
digitalWrite(LED_WARN_MIC, OUTPUT, LOW); //Turns off LED warning output
``````

What is this supposed to do? Back to the reference docs for you.

Using Tools + Auto Format
before posting
code is appreciated.