How can I program this? I'm a beginner.

I wanna program an arduino program, that while the potentiometer’s value is under 500, the delay between the blinks is 500 ms, while potentiometer’s value is 500 the delay is 1000 ms and while the potentiometer’s value is bigger than 500 the delay is 2000 ms.
My actual code is:

void setup() {
  pinMode(2, OUTPUT);
}

void loop() {
  int potenziometro = analogRead(5);
  int velocita;
  
  while (potenziometro < 500)
  {
    velocita = 500;  
  }
  while (potenziometro = 500) 
  {
    velocita = 1000;
  }
  while (potenziometro > 500)
  {
    velocita = 2000;
  }

  digitalWrite(2, HIGH);
  delay(velocita);
  digitalWrite(2, LOW);
  delay(velocita);
}

but it don’t work. Can someone please explain how to code it?
Thanks.

If you read from analog pin 5, you should use: "analogRead(A5)". What does the code do?

EDIT: And you should probably add "pinMode(A5, INPUT)" to setup :slight_smile:

If you read from analog pin 5, you should use: "analogRead(A5)".

analogRead(5);will work fine as long as the pot is connected to pin A5 and not digital pin 5. The analogRead() function understands that 5 refers to A5

you should probably add "pinMode(A5, INPUT)" to setup

No real need as pins default to being inputs.

Having said all of that I personally would use the pinMode() function and refer to the pin as A5 rather than 5 to make it clear what was going on and exactly which pin was being used.

Oh… And you should probably use “if” instead of “while” LOL! :wink:

Wrong:

  while (potenziometro < 500)
  {
    velocita = 500; 
  }
  while (potenziometro = 500)
  {
    velocita = 1000;
  }
  while (potenziometro > 500)
  {
    velocita = 2000;
  }

Right:

if (potenziometro < 500) velocita = 500;
else if (potenziometro == 500) velocita = 1000;
else /*if (potenziometro > 500)*/ velocita = 2000;

The readings from a pot-metre are usually fluctuating - so you should probably have “velocita = 1000” set in the range 450-550 (and adjust other ranges accordingly) in order to make it work.

EDIT: Changed “=” to “==” :slight_smile:

while potentiometer’s value is 500 the delay is 1000 ms and while the potentiometer’s value is bigger than 500 the delay is 2000 ms.

  while (potenziometro < 500)
  {
    velocita = 500;
  }

You are not going to get any blinks because the code will not leave this while loop when the reading is below 500. Similarly with the while loop for greater than 500.

 while (potenziometro = 500)
  {
    velocita = 1000;
  }

Nor will this work because you are setting potenziometro to 500 not testing its value. In any case, the chances of it being exactly 500 are very small unless you have a precision pot.

As has been pointed out you probably should use if rather than while or more clumsily put the flashing code inside the while loops (don’t do it !)

My first try was with if, but it don't worked.
But I'll retry defining pinmode of the potentiometer, but when I tried to set the delay the same value of the potentiometer, it worked fine without defining pinmode.

UKHeliBob:
or more clumsily put the flashing code inside the while loops (don't do it !)

Why? Is it just messy or it can harm the arduino?

I retried with the if method and it works! Thank guys!

Why? Is it just messy or it can harm the arduino?

No harm will come to the Arduino but it would be a clumsy way to do it because of either the repeated code or repeated calls to a function to do the flashing.

I retried with the if method and it works!

That is the important thing. Are you likely to want to do anything else in the code such as reading an input ? Warning ! This is a loaded question !