set LED blink time without delay and a potentiometer (problem)

Hello!

I'm new with Arduino, and I have a problem with my code... I assume the problem in the code is simple, but I'm not able to find it, so I will appreciate if you could give me a hand.

My code is a modification of the Blink Without Delay tutorial and I'm using and Arduino UNO R3. I want to turn on a LED after X milliseconds the pushbutton has been pushed, where X is can be varied with a potentiometer. I don't want to use the delay() function, as in a next step I need to read other sensors while the LED is "waiting" (not included in this code).

I used the print function to check the time reading and the problem seems to be that Arduino is not capable to do the operation (finishTime - startTime), and instead it is only reading (finishTime). This is the printed info:

Potmeter value: 1023 startTime: 834 finishTime: 1024 Difference: 1024 (finish-start)

Thanks in advance for you help! Here is the code:

// constants won't change.

const int buttonPin = A0;    // the number of the pushbutton pin
const int potPin = A1;       // pin number potmeter
const int ledPin = 13;       // LED 


// variables will change:

int buttonState = 0;       // variable for reading the pushbutton status
int potValue = 0;          // variable for the potentiometer
int act =0;              // LED switch variable (pushbutton to on/off button)

long startTime = 0;


void setup() {
  
  pinMode(ledPin, OUTPUT);       // LED pin as an output
  pinMode(buttonPin, INPUT);     // pushbutton pin as an input
  pinMode(potPin, INPUT);        // potentiometer pin as an input
  Serial.begin(9600);
}

void loop(){

  // Read input
  buttonState = digitalRead(buttonPin);         // read the state of the pushbutton value
  unsigned long potValue = analogRead(potPin);  // read the value from the sensor: 

  if (buttonState == HIGH) {
    act = 1;   // Turns LED switch to ON     
    unsigned long startTime = millis();         // time when button is pushed 
  
  // print
    Serial.println("Potmeter value");
    Serial.println(potValue);
    Serial.println("start");
    Serial.println(startTime);
    }
    
    if (act == 1) {
      unsigned long finishTime = millis();   // actual time
    
      if (finishTime - startTime > potValue) {
      // Print
      Serial.println("finish");
      Serial.println(finishTime);
      Serial.println("Difference");  // should be > than potValue
      Serial.println(finishTime - startTime);
      
      // Activate LED
      digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      act = 0;
      }
      }
    }

My code is a modification of the Blink Without Delay tutorial

The modification being, you've put the delays back in, it seems.

Yes, there is an delay(1000) to determine how much time the LED is ON. It is just temporal, to make the code simple. The main issue is to control with a potentiometer (using the millis() function) the waiting time before the LED turns ON. In a next step, I have to change the LED for a servo, so the delay(1000) will be removed... but I prefer to go slowly, step by step.

AWOL:

My code is a modification of the Blink Without Delay tutorial

The modification being, you've put the delays back in, it seems.

but I prefer to slowly

Well, yes, those delays will certainly dull the responsiveness of your sketch.

I mean in programing the code, first something simple, and then include other functions, like servos, sensors, etc... don't worry about the delay(1000) I will remove it.

Any clue why the if (finishTime - startTime > potValue) is not working? It seems to compare (finishTime > potValue) instead.

AWOL:

but I prefer to slowly

Well, yes, those delays will certainly dull the responsiveness of your sketch.

Any clue why the if (finishTime - startTime > potValue) is not working?

Could it be something to do with the fact that "startTime" is always zero?

I don't think so, because startTime gets a non-zero value when you push the button. As example,

homusubi: This is the printed info:

Potmeter value: 1023 startTime: 834 finishTime: 1024 Difference: 1024 (finish-start)

It seems that "- startTime" is being ignored for some reason.

AWOL:

Any clue why the if (finishTime - startTime > potValue) is not working?

Could it be something to do with the fact that "startTime" is always zero?

startTime gets a non-zero value when you push the button.

Are you sure about that? I'm certain it isn't.

I'm using the Arduino right now, and this is what I get printed:

Potmeter value 1023 start 2195533 finish 2195534 Difference 2195534

The startTime changes each tmie I press the button, look at the code:

  if (buttonState == HIGH) {
    act = 1;   // Turns LED switch to ON     
    unsigned long startTime = millis();         // time when button is pushed 
  
  // print
    Serial.println("Potmeter value");
    Serial.println(potValue);
    Serial.println("start");
    Serial.println(startTime);

AWOL:

startTime gets a non-zero value when you push the button.

Are you sure about that? I'm certain it isn't.

I already looked at the code.

The startTime changes each tmie I press the button,

sp. “A “startTime” changes each time I press the button”.

Just not the correct “startTime”.
Check the scope.

humm... I think that I understand what you mean. But I don't understand why or how to solve it, can you help me?

If I remove //long startTime =0; from the beggining I get the error: error: 'startTime' was not declared in this scope

So I assume that in some way there are two "startTime". If this is the case Why is the startTime from the if (finishTime - startTime > potValue) not the same as the one I define in: unsigned long startTime = millis(); ?

AWOL: I already looked at the code.

The startTime changes each tmie I press the button,

sp. "A "startTime" changes each time I press the button".

Just not the correct "startTime". Check the scope.

You're declaring two variables (you give both a type), when you should only be assigning to the global variable (which is zero by default).

unsigned long startTime = millis();

should be just startTime = millis();

Hey AWOL, thanks a lot. Now it is working! :grin:
I still don’t totally understand how variables are assigned and why there were two “startTime”, but i will give it a read. I assume you can find the info in the support; however a recommended lecture will be appreciated.

Thanks again!

AWOL:
You’re declaring two variables (you give both a type), when you should only be assigning to the global variable (which is zero by default).

unsigned long startTime = millis();

should be just startTime = millis();

Just read up on C scope rules and code blocks.