IF an analogue value change from 0 to 255 within a certain time, flash LED

Hi, I'm looking to make a project whereby if a potentiometers value changes from 0 to its max within 1 second - flash LEDS.

Then else, run the rest of the code as normal. (that side of my code is sorted)

I need the timer to be constantly running and looking for that analogue value change.

Any help would be massively appreciated.

Thanks,
Rhys.

Help us help you.

if it's constantly running and you are on a single core arduino, then your arduino can't do anything else...

if your loop is non blocking and running fast, may be it's good enough for you to check the status once per loop?

Here is what I tried doing. I'm using an orange pip, unsure if that's single core?

This code doesn't work correctly, I dont know where to go from here :confused:

int analogInput = 0; // variable to store the analog input reading
unsigned long currentTime = 0; // variable to store the current time
unsigned long previousTime = 0; // variable to store the previous time

void setup() {
pinMode(13, OUTPUT); // set digital pin 13 as an output
Serial.begin(9600); // initialize serial communication at 9600 baud rate
}

void loop() {
previousTime = currentTime; // store the previous time
currentTime = millis(); // update the current time
analogInput = analogRead(A0); // read the analog input

// check if the analog input value increases from 0 to 255 within less than one second
if (analogInput > 0 && analogInput < 255 && (currentTime - previousTime) < 1000) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
else
digitalWrite(13, LOW);
}


Use code tags (the </> icon above the compose window) to make it easier to read and copy code for examination

Look at File|Examples|02.Digital|BlinkWithoutDelay to use millis() for timing instead of delay().

you have a UNO like board

note that analogRead() is not always super stable, so you could read 0, 1, 2 for example at the low end and 1022 or 1023 at the high end

I think you should also check your wirings. Some things doesn´t appear to be correct.

You want more like:

  currentTime = millis(); // update the current time
  analogInput = analogRead(A0); // read the analog input

  // check if the analog input value increases from
  // 0 to 255 within less than one second
  if (analogInput == 0)
    previousTime = currentTime;

  if (analogInput == 1023 && (currentTime - previousTime) < 1000) {

Not easy to set a pot to exactly 255 :slight_smile:

Try
if (analogInput == 1023
Leo..

John, you are a legend!! it works now.

Thank you so much!

Thank you to everyone else who gave input too

and just in case your pot or arduino ADC is not perfect, you can give yourself a bit of wiggle room

  currentTime = millis();             // update the current time
  analogInput = analogRead(A0);      // read the analog input

  // check if the analog input value increases from
  // less than 3  to  more than 1020 within less than one second
  if (analogInput <= 3)
    previousTime = currentTime;

  if (analogInput >= 1020 && (currentTime - previousTime) < 1000) {
    ...
  }