Hey guys, another question for the masses. I am doing a setup with a Potentiometer, where as I turn it up, it will light up a series of LED’s, one at a time, turning off all of them, and leaving on only the “current” active one. Im getting a range of ±5 on my ACD readings, and Im not sure how to tell my trigger to listen for a range of numbers, instead of just a single value. Im still pretty new to this part, so I included my code so far. I only have 2 LEDS in the code, but it should get the idea across.

``````int loc0 = LOW;
int loc1 = LOW;
int loc2 = LOW;

int locPin = 0;
int locPot = 0;

void setup()
{
Serial.begin(9600);

pinMode(locPin, INPUT);
pinMode(loc0, OUTPUT);
pinMode(loc1, OUTPUT);
pinMode(loc2, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);

}

void loop()
{
Serial.begin(9600);
Serial.println (locPot, DEC);
delay (100);

if (locPot == 0 )                 //pot detent 1
{digitalWrite (12, HIGH);}
else
{digitalWrite (12, LOW);}

if (locPot == 196)                //pot detent 2
{digitalWrite (11, HIGH);}
else
{digitalWrite (11, LOW);}

}
``````

http://arduino.cc/en/Reference/If

look at the comparison operators, then just pick what values you want, like if (value < 100)

well that only takes care of half of what Im trying to do. I need something like ( 150 < val < 220) if that makes any sense. If its less then 220 or more then 150 it will trigger HIGH.

these are my approximate ADC values at each detent

D ADC 1 0 2 200 3 400 4 600 5 800 6 1000

When the next detent is selected, I need the previous LED to turn off, so only one LED is lit at a time.

i forgot about the & command, i kept typing AND, like in BASIC. new code works like a charm. Anyone care to criticize??

``````int loc0 = LOW;
int loc1 = LOW;
int loc2 = LOW;

int locPin = 0;
int locPot = 0;

void setup()
{
Serial.begin(9600);

pinMode(locPin, INPUT);
pinMode(loc0, OUTPUT);
pinMode(loc1, OUTPUT);
pinMode(loc2, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);

}

void loop()
{
Serial.begin(9600);
Serial.println (locPot, DEC);
delay (100);

if (locPot < 10 )                 //pot detent 1
{digitalWrite (12, HIGH);}
else
{digitalWrite (12, LOW);}

if (locPot > 150 && locPot < 220 )              //pot detent 2
{digitalWrite (11, HIGH);}
else
{digitalWrite (11, LOW);}

if (locPot > 350 && locPot < 450 )                //pot detent 2
{digitalWrite (10, HIGH);}
else
{digitalWrite (10, LOW);}

}
``````

I need something like ( 150 < val < 220)

Use the and function in the if statement:-
if( (val > 150) && ( val < 220) ) // sorted
{ }

ya took me a while, but I got it sorted, exactly what you said Mike. Any chance you know how to make a 2-way decade counter ?

Anyone care to criticize

Be careful of your scoping - you've got two"locPot"s declared, one (correctly) with local scope in "loop", and another with global scope.

For ease of maintenence, I'd be inclined to put all your values (min, max and output pin) into a table, and drive the main part of "loop" with a "for" loop.

not exactly sure what/how to accomplish that. Im still trying to get the basics down while trying to complete a fairly complex project. Any way you can show me what you mean? Im more of a visual person.

my code grouping and organization is something i need to work on for sure, and does indenting do anything as far as code "seniority" (i guess thats the best word for it).

WHat about something like this? (coding on the fly, not tested…)

int whatLED(int analog_reading) //a new function
{
int p,calculate;

for(p=0;p<=5;p++) // 6 times
{
if(calculate<=10 && calculate>=-10)return p; //some error margin in ADC
} //end for
} //end function

this will return a number between 0 and 5, that will do the number of your led.

ill have to see how long it takes to wrap my head around that one. Ill throw it on the board tomorrow and see how it goes. I certainly have a lot to learn. Thanks!!

edited. some were wrong :P

Any chance you know how to make a 2-way decade counter ?

Do you mean an up / down counter, I am not sure what you mean by decade counter. Are you talking about a BCD counter?

ya i guess it could be called an up down counter. Im trying to take a rotary knob with 18 contact points, and 1 ground, and make it control through an up/down button. Im going to be using 8x1 multiplexers to act like isolation relays, and each one only uses 3 triggers from the arduino. it needs to count from 0-18, and stop at 18 counting up, and stop at 0 counting down.

After I get the counter figured out, i guess Ill do something like:

if count == 2
{pin1-HIGH
pin2-LOW
pin3-LOW}

you get the idea. hopefully.

westfw posted this in the actual thread i have for this issue:

``````buttonUpTransition = read_and_debounce(inpinup);

if (buttonUpTransition == BUTTONPRESSED && count < 18) {
count++;
} else if (buttonDownTransition == BUTTONPRESSED && count > 0) {
count--;
}
``````

ok i messed with it a little bit more and got it working off WestFW’s code, i changed it around, and Ill do an external debouncer, and so far it works well.

heres what it looks like:

``````byte count;

void setup()
{
pinMode (39, INPUT);
pinMode (38, INPUT);
Serial.begin (9600);
}

void loop()
{
if (digitalRead(39) == HIGH && count < 18) {
count++;
delay (200);
} else if (digitalRead(38) == HIGH && count > 0) {
count--;
delay (200);
}
{
Serial.print (count, DEC);
}
}
``````

ok I started doing the IF statements for triggering the pins on the multiplexers, and theres got to be an easier way.

anyone care to condense??

``````byte count;

void setup()
{
pinMode (52, INPUT);         //external debounced switch pins 52 is temp up
pinMode (50, INPUT);        // 50 is temp down
pinMode (37, OUTPUT);       //pinS ODD 37-53 are outputs for multiplexers
pinMode (39, OUTPUT);
pinMode (41, OUTPUT);
pinMode (43, OUTPUT);
pinMode (45, OUTPUT);
pinMode (47, OUTPUT);
pinMode (49, OUTPUT);
pinMode (51, OUTPUT);
pinMode (53, OUTPUT);
Serial.begin (9600);

}

void loop()
{
if (digitalRead(52) == HIGH && count < 18) {
count++;
delay (200);
} else if (digitalRead(50) == HIGH && count > 0) {
count--;
delay (200);
}

if (count = 0)
{digitalWrite (37, LOW);
digitalWrite (39, LOW);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW);}
else
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, HIGH);}

if (count = 1)
{digitalWrite (37, HIGH);
digitalWrite (39, LOW);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW);}
else
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, HIGH);}

delay (10);

if(count = 2)
{digitalWrite (37, LOW);
digitalWrite (39, HIGH);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW);}
else
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, HIGH);}

if (count = 3)
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, HIGH);}

if(count == 4)
{digitalWrite (37, LOW);
digitalWrite (39, LOW);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, HIGH);}

if(count == 5)
{digitalWrite (37, HIGH);
digitalWrite (39, LOW);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, HIGH);}

if(count == 6)
{ digitalWrite (37, LOW);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count == 7)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count == 8)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, HIGH);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count == 9)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count == 10)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count ==11)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, LOW);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count == 12)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count == 13)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }

if(count == 14)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, HIGH);
digitalWrite (53, LOW); }

if(count == 15)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, LOW); }

if(count == 16)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, HIGH); }

if(count == 17)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, LOW);
digitalWrite (53, HIGH); }

Serial.print (count, DEC);
}
``````

You seem to be a bit messed up here. First off:- if (count = 0) should be if (count == 0)

Next ever if has an else so that the outputs are rattling up and down all the time destroying any state set when the if is true by all the cases when the if is false and the else gets executed.

Finally yes all that repetition is wrong. As I am not sure what you are trying to do it is hard to correct it. You seem to be defining pins as inputs and then writing to them. At first I thought you were outputting a count in binary but with so many bits you can't possibly be doing that. Can you have another go at explaining what it it is all about.

ya i caught my == issue. I did a little more work, and got it all working the way I want. Might be a little crude or in-efficient, but for what Im doing its not a big deal. Heres my revised code. Ill take a short vid of it later and post it so you guys can see what I am trying to do.

``````byte count;

void setup()
{
pinMode (52, INPUT);         //external debounced switch pins 52 is temp up
pinMode (50, INPUT);        // 50 is temp down
pinMode (37, OUTPUT);       //pinS ODD 37-53 are outputs for multiplexers
pinMode (39, OUTPUT);
pinMode (41, OUTPUT);
pinMode (43, OUTPUT);
pinMode (45, OUTPUT);
pinMode (47, OUTPUT);
pinMode (49, OUTPUT);
pinMode (51, OUTPUT);
pinMode (53, OUTPUT);
Serial.begin (9600);

}

void loop()
{
if (digitalRead(52) == HIGH && count < 18) {
count++;
delay (200);
} else if (digitalRead(50) == HIGH && count > 0) {
count--;
delay (200);
}

if (count == 0)
{digitalWrite (37, LOW);
digitalWrite (39, LOW);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW);}
else
{}

if (count == 1)
{digitalWrite (37, HIGH);
digitalWrite (39, LOW);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW);}
else
{}

if(count == 2)
{digitalWrite (37, LOW);
digitalWrite (39, HIGH);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW);}
else
{}

if (count == 3)
{digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, LOW);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 4)
{digitalWrite (37, LOW);
digitalWrite (39, LOW);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 5)
{digitalWrite (37, HIGH);
digitalWrite (39, LOW);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 6)
{ digitalWrite (37, LOW);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 7)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, LOW);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 8)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, HIGH);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 9)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, LOW);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 10)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, LOW);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count ==11)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, LOW);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 12)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, LOW);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 13)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, LOW);
digitalWrite (53, LOW); }
else
{}

if(count == 14)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, HIGH);
digitalWrite (53, LOW); }
else
{}

if(count == 15)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, HIGH);
digitalWrite (53, LOW); }
else
{}

if(count == 16)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, LOW);
digitalWrite (51, LOW);
digitalWrite (53, HIGH); }
else
{}

if(count == 17)
{ digitalWrite (37, HIGH);
digitalWrite (39, HIGH);
digitalWrite (41, HIGH);
digitalWrite (43, HIGH);
digitalWrite (45, HIGH);
digitalWrite (47, HIGH);
digitalWrite (49, HIGH);
digitalWrite (51, LOW);
digitalWrite (53, HIGH); }
else
{}

Serial.print (count, DEC);
}
``````

o and just for a little more info, im using the outputs to trigger s0, s1, and s2 inputs on a 1x8 multiplexer chip, which is acting like a relay/isolator for the piece that I am interfacing to. The chips are HCT4051's, and they are pretty cool. Ive build video switchers and all sorts of stuff with them. 3 input pins can define 8 outputs. Plus is saving me on my output pins on the Duino.