I want to use the Arduino to detect a voltage on analogue pin A1, I want the Arduino to turn an LED on for 2 seconds then turn it permanently off if it detects a voltage (say a threshold of at least 100 based on the Arduino 1024 scale). It only does this, once and does not keep looping through the code and turning the LED on and off even if the voltage is detected after the LED has been turned off.
The only exception is where it detects a voltage less than the value of 100 (based on the Arduino 1024 scale), then this will have the effect of going back to the start of the script, and the LED will come on and go off the next time the threshold voltage is reached.
Here is a practical example (I know you can't measure 12v directly with the arduino, but it's just an example):
A) someone turns a car ignition on, and an ignition live is monitored by pin A1 on the Arduino
B) the arduino detects voltage is present when the ignition turned on
C) The arduion turns on an LED for 2 seconds then turns it permanently off. The LED is turned off permanently even if the ignition is left on and voltage continues to be detected.
D) The LED will only come on and off again if the ignition is turned off and then back on again. Simply leaving the ignition on will not cause the LED to do anything
My coding is at a very basic level, but I have had a go at coding something. But I don't know how to stop the LED turning off and on once it has done it once. Remember I only want to do it once, even if a voltage threshold continues to be detected. The only exception is when voltage is no longer detected at all - then I want it to loop back to the start of the script (like turning the ignition off then on again). Here is what I have done so far, but have no idea how to progress any further. I can't help but think what I want to do is quite simple, but I have wasted hours looking for a solution. So any help would be much appreciated.
CODE:
int sensorPin=A1; // Voltage Sensor connected to A1
int sensorValue; // variable to store the value coming from the voltage sensor
int TestLED=9; // LED connected to digital pin 9
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // initialize the serial port
pinMode(sensorPin, INPUT); // sets analog pin A1 to be an input
pinMode(TestLED, OUTPUT); // sets digital pin 9 to be an output
}
void loop() //
{
sensorValue = analogRead(sensorPin); // read the value from the voltage sensor
Serial.println(sensorValue); // send that value to the computer
if (sensorValue >= 100) // voltage detected.
{
digitalWrite(TestLED, HIGH); //turns on led connected to digital pin 9
delay(2000);
digitalWrite(TestLED, LOW); //turns off led
UKHeliBob:
You need to detect when the level becomes more than 100 rather than when it is more than 100 and the same for less than 100.
See the StateChangeDetection example in the IDE
Many thanks for your reply.
I have looked at the statechangedetection example, but that is under the digital section and I am concerned with analogue pin A1. Plus all the literature on satechange seems to relate to using push buttons, whereas I am concerned with a voltage reading. Another thing, even if statechangedetection somehow helps with turning the LED on and off just once, I need a way to return to the start of the script if a voltage less than a 100 is detected (based on the Arduino 1024 scale). Please could you show me an example based on my code, as I am not getting this.
The key to state change detection is comparing previous values to current values. Here's and analogRead() example which tests for the value rising through a threshold of 100. You then manage the led timing with a millis() timer and a boolean timing control variable.
int sensorPin = A1; // Voltage Sensor connected to A1
int sensorValue; // variable to store the value coming from the voltage sensor
int previousSensorValue;
int testLED = 9;
boolean timing = false;
unsigned long startTime;
unsigned long onTime = 2000;
void setup()
{
Serial.begin(9600); // initialize the serial port
pinMode(sensorPin, INPUT); // sets analog pin A1 to be an input
pinMode(testLED, OUTPUT); // sets digital pin 9 to be an output
}
void loop()
{
previousSensorValue = sensorValue;
sensorValue = analogRead(sensorPin); // read the value from the voltage sensor
Serial.println(sensorValue);
if (sensorValue >= 100 && previousSensorValue < 100)
{
timing = true;
startTime = millis();
digitalWrite(testLED, HIGH); //turns on led connected to digital pin 9
}
if (timing && millis() - startTime >= onTime)
{
digitalWrite(testLED, LOW);
timing = false;
}
}
I need a way to return to the start of the script
Please explain what you mean by this. The structure of the script is a repeating loop, and it returns to the start each time.
cattledog:
The key to state change detection is comparing previous values to current values. Here's and analogRead() example which tests for the value rising through a threshold of 100. You then manage the led timing with a millis() timer and a boolean timing control variable.
int sensorPin = A1; // Voltage Sensor connected to A1
int sensorValue; // variable to store the value coming from the voltage sensor
int previousSensorValue;
int testLED = 9;
boolean timing = false;
unsigned long startTime;
unsigned long onTime = 2000;
void setup()
{
Serial.begin(9600); // initialize the serial port
pinMode(sensorPin, INPUT); // sets analog pin A1 to be an input
pinMode(testLED, OUTPUT); // sets digital pin 9 to be an output
}
void loop()
{
previousSensorValue = sensorValue;
sensorValue = analogRead(sensorPin); // read the value from the voltage sensor
Serial.println(sensorValue);
if (sensorValue >= 100 && previousSensorValue < 100)
{
timing = true;
startTime = millis();
digitalWrite(testLED, HIGH); //turns on led connected to digital pin 9
}
Please explain what you mean by this. The structure of the script is a repeating loop, and it returns to the start each time.
Thank you very much for providing the example - you have cleared up the things I misunderstood. I now have a much better understanding. I am currently away from home for a few days, so will test when I get back. Thank you very much