So I'm currently working on an automotive set up.
I'm using a 555 timer to emulate a turn-signal alternating voltage pulse (12V to 0V and repeat) for debugging purposes which is output to turnPin/A1.
Here's the goal: When voltage is greater than 1V arduino is to supply constant power to output pin (pin 13) until input voltage is returned to constant 0 (or end of turn signal input) for x-time or as I'll refer to as Timeout.
It seems I've gotten the constant power to apply when input is applied and then turn off constant output when voltage is back to 0V. However I'm having difficulty getting the timeout function to work properly as the LED still remains HIGH.
EDIT: I've reworked the code to use millis(); rather than purely using sensor inputs. I've also added comments to help walk you through what I believe is happened/what I want to happen.
// Constants won't change
const int ledPin = LED_BUILTIN; //using built in led + pin13
const int turnPin = A1; // the input pin (turn signal alternating 5V/0V/5V/0V & so-forth)
// Variables will change
int ledState = LOW; // the current state of the output pin
int prevLed = ledState;
int lastVoltage = 1; // the previous reading from voltage source
int state = 1; // the current "state" (see below)
// State = 1++: LED OFF -- State = 0: LED ON
// The following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long previousMillis = 0; //the last time the voltage reached 0
long waitTime = 100; // the wait time
void setup()
{
pinMode(ledState, OUTPUT); //set output pin
pinMode(turnPin, INPUT); //set input pin NOTE: SIGNAL ALTERNATES
5V/0V/5V/0V & so forth to emulate a turn signal for this application
Serial.begin(9600); //begin serial
}
void loop()
{
int sensorValue = analogRead(turnPin); // Read voltage at input pin
float voltage = sensorValue * (5.0/1023.0); // Convert
unsigned long currentMillis = millis(); // set currentMillis as current
time
if (voltage > 1) //if Voltage greater than 1V
{
ledState = HIGH; //turn led on
state = 0; //set state to 0
}
if (voltage < 1) //if voltage less than 1V
{
currentMillis; //set currentMillis as current time
if ((millis() - currentMillis) > waitTime) // if (current time minus time voltage was less than 1) is greater than wait time
{
ledState = LOW; //set LED to off
Serial.print("OFF - TIMED OUT - WAITING FOR INPUT: "); //output message for debug
Serial.println(state);//current state
state++; //increment state (time passed)
}
else if ((millis() - currentMillis) < waitTime) // if (current time minus time voltage was less than 1) is less than wait time
{
ledState = HIGH; //set LED to on
}
}
digitalWrite(ledPin, ledState); //send LED status to output
}
When voltage > 1.... larger than one what? potato? .... it is really unclear what you are asking, a few commas in some of your sentences would helpfloat voltage = sensorValue * (5.0 / 1023.0);, but you do not use the variablevoltageanywhere in your code