LED Blink Within a Range

Hi,

New to Arduino. Working on a sketch that is supposed to trigger an LED with a knob. When the knob is at zero, the LED is off. when the led is up to a half of the way up, the LED blinks. When the knob is past the halfway point, the LED stays on.

I can keep the LED off at zero, and keep the LED on at max, with blinks in between using IF loops. However, when I adjust limits to the middle (using 450 for the test) the loop bypasses the blinking part.

Example:

void loop()
{
timex = analogRead (timePot);
maptime = map(timex, 0, 1023, 450, 24);

if (timex == 0) //1st LED off. This works fine
{
digitalWrite(LEDpin, LOW);
Serial.println (“Off”);
delay (200);
}

if (timex != 0 && timex << 450) //This gets bypassed (“Active”) never prints. LED don’t blink
{
digitalWrite (LEDpin, HIGH);
delay (maptime/2);
digitalWrite (LEDpin, LOW);
delay (maptime);
Serial.println (“Active”);
}

if (timex >> 450) //1st LED on all the time if not zero
{
digitalWrite(LEDpin, HIGH);
Serial.println (“Max”);
delay (10);
}

}

Any clues?

Try:
if (timex != 0 && timex < 450)

and

if (timex > 450)

<< and >> is for bitshift operation… I don’t think this is what you meant.

Yep, figured it out right after posting....

Thank you for your reply! Cheers

Here is the resulting sketch. Very simple, but maybe someone will find it useful.

/* Modified version of the BLINK sketch to progressively illuminate

  • a green, yellow and red LED as the values from a potentiometer increase.
  • The idea is to add this code to a larger project that will control
  • mozzi parameters with pots. The LEDs will indicate when the Cutoff
  • value is reaching its limits.
    */

int LEDpinG = 2;
int LEDpinY = 3;
int LEDpinR = 4;

int timePot = A0;

int timex = 0;
int maptime;

void setup()
{
pinMode (LEDpinG, OUTPUT);
pinMode (LEDpinY, OUTPUT);
pinMode (LEDpinR, OUTPUT);
}

void loop()
{
timex = analogRead (timePot);
maptime = map(timex, 0, 1023, 450, 24);

/////////////GREEN/////////////////

if (timex == 0) //Green LED off
{
digitalWrite(LEDpinG, LOW);
delay (2);
}

if (timex != 0 && timex < 400) //Green LED Blinks
{
digitalWrite (LEDpinG, HIGH);
delay (maptime/2);
digitalWrite (LEDpinG, LOW);
delay (maptime);
}

if (timex > 400) //Gren LED stays on
{
digitalWrite (LEDpinG, HIGH);
delay (2);
}

/////////////YELLOW/////////////////

if (timex < 400)
{
digitalWrite(LEDpinY, LOW);
delay (2);
}

if (timex > 400 && timex < 800)
{
digitalWrite (LEDpinY, HIGH);
delay (maptime/2);
digitalWrite (LEDpinY, LOW);
delay (maptime);
}

if (timex > 800)
{
digitalWrite (LEDpinY, HIGH);
delay (2);
}

/////////////RED/////////////////

if (timex < 800)
{
digitalWrite(LEDpinR, LOW);
delay (2);
}

if (timex > 800 && timex < 950)
{
digitalWrite (LEDpinR, HIGH);
delay (maptime/2);
digitalWrite (LEDpinR, LOW);
delay (maptime);
}

if (timex > 950)
{
digitalWrite (LEDpinR, HIGH);
delay (2);
}

}

rmontes01:
if (timex == 0) //Green LED off

Make that

  if (timex < 10)                                  //Green LED off

Most pots do not reliably go to zero, the tiniest bit of dirt will make them stick at a reading of 1 or even 2, and your LED never switches off. It also prevents users from forcefully pushing the pot to its limits to just get that darn LED to switch off completely.

if (timex > 950)
  {
    digitalWrite (LEDpinR, HIGH);

I think you mean

if (timex > 950)
  {
    // BOOM!! 
    digitalWrite (LEDpinR, HIGH);

Lol.

wvmarle, good point. Thank you. bigred1212, that's when it drops! :wink: Now, the next riddle to solve is the ADSR and cutoff breakout in mozzi. All I need are the 5 pots. I got the A and the R to work. The cutoff is wonky, but it's there. We'll see what happens. The last piece is the MIDI in. But I think that one is pretty simple with the MIDI.h library. Thanks again, all.

The idea is to add this code to a larger project

In that case you would be well advised to rewrite the code using millis() for timing as using delay() will block the operation of other code.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.