 # Assigning binary values to ranges?

Is there a way to replace “test” with a binary number, so that if the potentiometer is in a specific range that would equal a binary value? e.g. between 0-100 would be 000, 101-200 would be 001 and so on. It would help if you gave more examples of the ranges involved. For instance, you say “and so on”.
The next binary number after 000 and 001 is 010 then 011 and so on is that what you mean ?

I’d be following the standard 3 bit Gray code pattern, so 000, 001, 011, 010, 110, 111, 101, 100. This way, as the potentiometer cycles through ranges it’s only changing 1 value.

Hello
take a look here.

So to summarise, you have a pot for input and want to output a Gray code depending on its position

How about using the map() function to convert the pot reading into a value from 0 to the number of Gray codes required then using that value as the index to an array of the corresponding Gray codes ?

I’ll give it a try, cheers mate

Hello
or try this ``````#define ProjectName "Assigning binary values to ranges?"
// https://forum.arduino.cc/t/assigning-binary-values-to-ranges/859159/5
// https://www.arduino.cc/reference/en/
// https://www.arduino.cc/en/Guide/Introduction
//----------
//---------- Declarations
const int heartBeat {LED_BUILTIN};
enum {Gray0, Gray1, Gray2, Gray3, Gray4, Gray5, Gray6, Gray7};
const byte Gray[] {0b000, 0b000, 0b000, 0b000, 0b000, 0b000, 0b000, 0b000};
enum {Relay1, Relay2, Relay3};
const byte RelayPin1 {2};
const byte RelayPin2 {3};
const byte RelayPin3 {4};
const byte analogPin {A5};
const byte Output [] {RelayPin1, RelayPin2, RelayPin3};
//----------
//---------- Functions
void writeRelay(int index) {
digitalWrite(Output[Relay1], Gray[index] & 0b001);
digitalWrite(Output[Relay2], Gray[index] & 0b010);
digitalWrite(Output[Relay3], Gray[index] & 0b100);
}
//----------
void setup() { // put your setup code here, to run once:
Serial.begin(9600);
Serial.print("File   : "), Serial.println(__FILE__);
Serial.print("Date   : "), Serial.println(__DATE__);
Serial.print("Project: ");
for (int n = 9; n >= 0 ; n--) Serial.print(F(" * ")), Serial.print(n), delay(n * 100);
Serial.println(F(".\nlet´s go! "));
//---------- Setup
pinMode (heartBeat, OUTPUT);
for (auto pin : Output) digitalWrite(pin, LOW),pinMode(pin, OUTPUT);
//----------
delay(1000);
}
void loop() {  // put your main code here, to run repeatedly:
digitalWrite(heartBeat, millis() / 500 % 2);
switch (analogRead(analogPin)) {
case 0*128 ... (1*128)-1: writeRelay(Gray0); break;
case 1*128 ... (2*128)-1: writeRelay(Gray1); break;
case 2*128 ... (3*128)-1: writeRelay(Gray2); break;
case 3*128 ... (4*128)-1: writeRelay(Gray3); break;
case 4*128 ... (5*128)-1: writeRelay(Gray4); break;
case 5*128 ... (6*128)-1: writeRelay(Gray5); break;
case 6*128 ... (7*128)-1: writeRelay(Gray6); break;
case 7*128 ... (8*128)-1: writeRelay(Gray7); break;
}
}
``````

You have to do the following mods:
1- asign input and output pins used
2- asign the gray code required to you application

divide the value by 100

I gave it a shot and made some progress but I have no idea what I’m doing and the other answers are way beyond my current skill level. It still doesn’t work how I’d like it to, is there a way to link the values. So that if the potvalue is 0 then the graycode B000 would be true and would perform that if command.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.

To add code please click this link;

Why are you writing to pins 0 and 1, they are the programming pins.
You need to post your ENTIRE code, what pin is “potpin”?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you thought to output to the IDE serial monitor?

Tom…    consider (tested on my HW)

``````
#define Pot A0

#undef MyHW
#ifdef MyHW
enum { Min = 700, Max = 800 };
byte ledPins [] = { 10, 11, 12, 13 };

#else
enum { Min = 0, Max = 1023 };
byte ledPins [] = { 3, 2, 0, 1 };
#endif

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

for (unsigned i = 0; i < sizeof(ledPins); i++)
pinMode (ledPins [i], OUTPUT);
}

void
setLeds (
byte d,
byte c,
byte b,
byte a)
{
digitalWrite (ledPins , a);
digitalWrite (ledPins , b);
digitalWrite (ledPins , c);
digitalWrite (ledPins , d);
}

void loop() {
int anlg = analogRead (Pot);
int val = map(anlg, Min, Max, 0, 8);

Serial.println (anlg);

switch (val)  {
case 8:
setLeds (1, 0, 0, 0);
break;
case 7:
setLeds (0, 1, 1, 1);
break;
case 6:
setLeds (0, 1, 1, 0);
break;
case 5:
setLeds (0, 1, 0, 1);
break;
case 4:
setLeds (0, 1, 0, 0);
break;
case 3:
setLeds (0, 0, 1, 1);
break;
case 2:
setLeds (0, 0, 1, 0);
break;
case 1:
setLeds (0, 0, 0, 1);
break;
default:
setLeds (0, 0, 0, 0);
break;
}
}

``````

This is what I had in mind

``````const byte potPin = A7;
const byte grayCodes[] = {0b1000000, 0b1000001, 0b1000011, 0b1000010, 0b1000110, 0b1000111, 0b1000101, 0b1000100};

void setup()
{
Serial.begin(115200);
while (!Serial);
}

void loop()
{
int potValue = analogRead(potPin);
byte mappedValue = map(potValue, 0, 1023, 0, 7);
Serial.print(potValue);
Serial.print("\t");
Serial.print(mappedValue);
Serial.print("\t");
Serial.println(grayCodes[mappedValue], BIN);
delay(500);
}``````

NOTE : I have set bit 7 to 1 solely to force all 8 bits of the Gray code to be printed by Serial.print() to keep the output consistent

Hi,
From another angle, but similar to @gcjr .
Open your IDE monitor at 115200 baud.

``````int RawVal = 0;
int potPin = A1;
int oldgreyVal = 10;
int greyVal;

void setup() { Serial.begin(115200); }

void loop() {
RawVal = analogRead(potPin);
greyVal = map(RawVal, 0, 1023, 0, 7);

if (greyVal != oldgreyVal) {

Serial.print(RawVal);
Serial.print("\t Grey Level = ");
Serial.print(greyVal);
Serial.print("\t   GreyCode ==");
oldgreyVal = greyVal;

switch (greyVal)  {

case 7:
Serial.print("0100");
break;

case 6:
Serial.print("0101");
break;

case 5:
Serial.print("0111");
break;

case 4:
Serial.print("0110");
break;

case 3:
Serial.print("0010");
break;

case 2:
Serial.print("0011");
break;

case 1:
Serial.print("0001");
break;

case 0:
Serial.print("0000");
break;

}

Serial.println(" ");

}
delay(250);
}
``````

Tom…    Hello
Using the map() function can save a lot lines of code C&P

``````void loop() {  // put your main code here, to run repeatedly:
digitalWrite(heartBeat, millis() / 500 % 2);
writeRelay(map(analogRead(analogPin),0,1023,0,7));
}
``````

When both ranges start with 0, map() is just the same as an integer multiply and integer divide.

(You left out the greycode translation.)

``````byte greycodes = {0b000, 0b001, 0b011, 0b010, 0b110, 0b111, 0b101, 0b100};

void loop()
{
digitalWrite(LED_BUILTIN, millis() / 500 % 2);
writeRelay(greycodes[(analogRead(analogPin) * 8) / 1024]);
}
``````
1 Like

Hello,
many thanks for your advice 1 Like