While or if analogRead(A0) not in a range

Hello, in college and having some difficulty with one problem. I have a SparkFun kit and im trying to do different tasks based on what value my potentiometer reads. How do I code, "If im not in the range of 257-512, then a light shouldnt go on."? Right now I am having the issue of the light turning on, but not turning off once I turn the knob outside of that range. Heres the snippet of my code I am having problems with.

  while( 257 <= analogRead(A0) <= 512)
  {
    digitalWrite(11, HIGH);
  }
  if()// in the brackets, if analogRead(A0) is not in the range, then then digitalWrite(11,LOW)

Hi, @brmxcy
Welcome to the forum.
Can you please post your complete code?

Do you have a statement that turns the light OFF.

Will not turn it off.

TIP; Use if... else...

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

1 Like

While that, sadly, is syntactically valid, it does not do what you think it does.

Did some modifying. Once the dial reaches the second range, the light becomes red, but doesn't turn off if the dial is moved to another range. If i press the button attached to pin 7 to turn off the light, the light will remain off even if i move dial to another range. Also if you know any way I can use a for loop to replace the first if loop that's be greatly appreciated.

int hertz = 1000;
int bomb = 200;

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(7, INPUT_PULLUP);
}

void loop()
{
  if(analogRead(A0) >=0 && analogRead(A0) <= 256)
  {
    digitalWrite(9, HIGH);
    delay(200);
    tone(8,hertz,bomb);
    digitalWrite(9, LOW);
    delay(200);
    hertz = hertz +250;
  }
  if(analogRead(A0) >=257 && analogRead(A0) <= 512)
  {
    digitalWrite(11,HIGH);
    if(digitalRead(7)==LOW)
    {
      while(true)
      {
      digitalWrite(11,LOW);
      }
    }
  }
}

once you are in the while(true) loop you will stay there forever
did you intend something like

while(digitalRead(7)==LOW)
      {
      digitalWrite(11,LOW);
      }

the loop exits when you release the button
or possibly

 if(analogRead(A0) >=257 && analogRead(A0) <= 512 && digitalRead(7)==HIGH)
     digitalWrite(11,HIGH);
else
    digitalWrite(11,LOW);

think you need to write out a specification of what the code is required to do

analogRead returns an integer number between 0 and 1023. https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

This means analogRead(A0) >= 0 is always true

The above of condition can then be simplified as
if(analogRead(A0) <= 256)

HI,
Try this, I have taken tone out to JUST work on the flashing.

int hertz = 1000;
int bomb = 200;

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(7, INPUT_PULLUP);
}

void loop()
{
  if (analogRead(A0) <= 256)
  {
    digitalWrite(9, HIGH);
    delay(200);
    digitalWrite(9, LOW);
    delay(200);
  }
  else
  {
    digitalWrite(9, LOW);
  }
}

Note you will have to wait up to 400ms for any change in the pot value to take effect.

delay is a blocking function.

Better to use "Blink without delay" example in Examples tab of IDE.

Tom.. :grinning: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.