Reading input from potentiometer

Hi,

How can I make TWO DIGIT 7-segment display to read the value from the potentiometer? For example, if the resistance is greater than 35 ohms, the display shows the digit 1. If the resistance is greater than 50, the display shows the digit 2 and so on.

So basically, I want to manipulate the value of the resistance.

Here is what I need in pseudo-code :

if(value > 0 && value <= 35)
   displayValue = 00;
if(value > 35 && value <= 50)
   displayValue = 01;
if(value > 50 && value <= 75)
   displayValue = 02;

etc..

And if possible the PCB to have as much less parts as possible.

Thanks for the help.

Do you have any code that changes the displays? basically if you enter,displayValue = 02; does it show 02? If not then you need to use something like

display1 = val / 10;
display2 = val % 10;

So say you have val = 15,
display1 = 15 / 10; (display1 = 1)
display2 = 15 % 10; (display2 = 5)

Also, I should add; instead of using IF statements to give you those readings, just map them and then use the code I gave you to seperate them.

displayValue = 00;

Using octal literals when they're not warranted is not a good idea.

Getting the data to display is easy. analogRead returns a 10-bit value,
000 to 3FF.
Divide that by 4:

byte displayValue = (analogRead(5)>>2; // read pot on A5
result is 00 to FF
display the left half on one display, the right half on a second display.

rightDigit = displayValue & 0x0F; // data is now in bit positions 0 to 3
leftDigit =( displayValue & 0xF0)>>4; // move  bits into bit positions 0 to 3

Next, setup 2 arrays for the pins you drive the displays with:
digit1pinsArray[] = {2,3,4,5,6,7,8,}; // 2 = a, 3 = b, 4 = c, 5 = d, 6 = e, 7 = f, 8 = g - see  below
digit2pinsArray[] = {9,10,11,12,13,14,15}; // A0,1,2,3 are 14,15,16,17

don't forget current limit resistors

Now, create a font map for digits 0-9,A,b,c,d,E,F
byte fontArray [] = {
B00111111, // 0  1 = segment on
B00000110, // 1  bit 0 = segment A, 1 = b, 2 = c, 3 = d, 4 = e, 5 = f, 6 = g, 7 = not used or decimal point
B01011011, // 2                 a
B01001111, // 3         f              b
B00100110, // 4                g
:                                   e              c
:                                          d 
: etc for rest of digits
B01110001, // F
}
 
Now to show a number, make the link from fontArray[] to pinsArray[]
There's probably some fancier software loop way to do this, I'm just going to use some simple bit masking:

// bit 0, mask for segment A
if ((fontArray[leftDigit] & B00000001) == 1){
digitalWrite(digit1pinsArray[0], HIGH); // HIGH if driving LED Anodes. Use LOW if sinking current from LED Cathodes
}
else {
digitalWrite(digit1pinsArray[0]), LOW); // LOW if driving LED Anodes. Use HIGH if sinking current from LED Cathodes
}

// bit 1, mask for segment B
if ((fontArray[leftDigit] & B00000010) == 1){
digitalWrite(digit1pinsArray[1], HIGH);
}
else {
digitalWrite(digit1pinsArray[1]), LOW);
}

// bit 2, mask for segment C
if ((fontArray[leftDigit] & B00000100) == 1){
digitalWrite(digit1pinsArray[2], HIGH);
}
else {
digitalWrite(digit1pinsArray[2]), LOW);
}

// bit 3, mask for segment D
if ((fontArray[leftDigit] & B00001000) == 1){
digitalWrite(digit1pinsArray[3], HIGH);
}
else {
digitalWrite(digit1pinsArray[3]), LOW);
}

// bit 4, mask for segment E
if ((fontArray[leftDigit] & B00010000) == 1){
digitalWrite(digit1pinsArray[4], HIGH);
}
else {
digitalWrite(digit1pinsArray[4]), LOW);
}

// bit 5, mask for segment F
if ((fontArray[leftDigit] & B00100000) == 1){
digitalWrite(digit1pinsArray[5], HIGH);
}
else {
digitalWrite(digit1pinsArray[5]), LOW);
}

// bit 6, mask for segment G
if ((fontArray[leftDigit] & B0100000) == 1){
digitalWrite(digit1pinsArray[6], HIGH);
}
else {
digitalWrite(digit1pinsArray[6]), LOW);
}

Repeat for digit2pinsArray[] and rightDigit

Now put all the pieces together!

We need more info, like, what kind of 7 segment you using, and how you connecting them.

Well, in case you using ShiftRegisters. Here you have a very basic code, isn't the best, but works perfectly.

On the Shift Registers outputs, it's connected like this:

Q0 -> a Segment
Q1 -> b Segment
Q2 -> c Segment
Q3 -> d Segment
Q4 -> e Segment
Q5 -> f Segment
Q6 -> g Segment
Q7 -> DecimalPoint Segment

int potPin = A0;
int potValueMapped;
int digitOne;
int digitTwo;

const int latchPin = 8;  // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin  = 11;  // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 12;  // Pin connected to Pin 11 of 74HC595 (Clock)

void setup() {
  Serial.begin(9600); //Remove after testings
  
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {

  potValueMapped = map(analogRead(potPin), 0, 1023, 0, 50); //Reads the Analog Input and map it with values from 0 to 50
  
  digitOne = (potValueMapped/10); //We get the 1st digit, simply dividing by 10
  
  Serial.println(digitOne); //Remove after testings
  displayDigitOne(digitOne); //This function will ShiftOut the 1st digit (It's made this way, bc only using 1 7 segment and digitone I want it with the decimal point ON)
  delay(500); //This delay you have to remove it's just so I can see both numbers in only 1 7 Segments
  
    
  digitTwo = (potValueMapped - (10*digitOne)); //We get the 2nd digit with this math. Example, for a value of 39 it is making 39 - 3 * 10 = 9
  
  Serial.println(digitTwo); //Remove after testings
  displayDigitTwo(digitTwo); //This function will ShiftOut the 2nd digit (This function doesn't turn on the decimal point)
  delay(500); // This is your refresh time
}

//The reason for 2 "functions" is bc the numbers on digit one, will always have the decimal point
//This code should be replaced depending on how you are sending data to the 7 segments
//In this code, it's done for using 2 Shift Registers

void displayDigitOne(int value) {
  switch (value) {
    case 0:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11111101);
      digitalWrite(latchPin, HIGH);
      break;
    case 1:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b01100001);
      digitalWrite(latchPin, HIGH);
      break;
    case 2:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11011011);
      digitalWrite(latchPin, HIGH);
      break;
    case 3:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11110011);
      digitalWrite(latchPin, HIGH);
      break;
    case 4:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b01100111);
      digitalWrite(latchPin, HIGH);
      break;
    case 5:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b10110111);
      digitalWrite(latchPin, HIGH);
      break;
    case 6:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b10111111);
      digitalWrite(latchPin, HIGH);
      break;
    case 7:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11100001);
      digitalWrite(latchPin, HIGH);
      break;
    case 8:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11111111);
      digitalWrite(latchPin, HIGH);
      break;
    case 9:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11100111);
      digitalWrite(latchPin, HIGH);
      break;
    case '.':
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000001);
      digitalWrite(latchPin, HIGH);
      break;
    case 'OFF':
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000000);
      digitalWrite(latchPin, HIGH);
      break;
    default:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000000);
      digitalWrite(latchPin, HIGH);
      break;
  }
}

void displayDigitTwo(int value) {
  switch (value) {
    case 0:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11111100);
      digitalWrite(latchPin, HIGH);
      break;
    case 1:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b01100000);
      digitalWrite(latchPin, HIGH);
      break;
    case 2:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11011010);
      digitalWrite(latchPin, HIGH);
      break;
    case 3:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11110010);
      digitalWrite(latchPin, HIGH);
      break;
    case 4:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b01100110);
      digitalWrite(latchPin, HIGH);
      break;
    case 5:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b10110110);
      digitalWrite(latchPin, HIGH);
      break;
    case 6:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b10111110);
      digitalWrite(latchPin, HIGH);
      break;
    case 7:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11100000);
      digitalWrite(latchPin, HIGH);
      break;
    case 8:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11111110);
      digitalWrite(latchPin, HIGH);
      break;
    case 9:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11100110);
      digitalWrite(latchPin, HIGH);
      break;
    case '.':
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000001);
      digitalWrite(latchPin, HIGH);
      break;
    case 'OFF':
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000000);
      digitalWrite(latchPin, HIGH);
      break;
    default:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000000);
      digitalWrite(latchPin, HIGH);
      break;
  }
}

But of course, you can use some Library for the outs.

Thank you all for the answers.

What I need is shown in this video :

In my case, the potentiometer is logarithmic.

Only what I need is instead of the voltage to regulate the digits, I need a potentiometer.

So, when there is an input from 35 to 50 ohms resistance, it shows a certain number for example 02.

So basically, I don't need the values to be linear. I need the values to be made by me manually - I tell which value corresponds to the resistance.

I hope I made myself clearer this time.

I forgot to mention that I'm a beginner at this, so I will need code if possible, I've only experimented a little with Arduino.

Thanks.

As I under stand you need a mapping table. Value a (pot) equals value b (result).
if it is linear you can use map, otherwise use an array.

Yes, thank you, that is what I need.

Unfortunately, maybe the other that answered had my solution but I can't put the code all together. Can anyone give me a done project so that atleast I can simulate and test it.

Once again, thank you all for your answers.

Can anyone give me a done project so that atleast I can simulate and test it.

Is this a class assignment for a grade?

No, this is a project that I want to do as a hobby project - to learn and experiment more with the Arduino.

Change this part of what I provided to be array based:

lookupArray[] = {0,0,0,0,1,1,1,1,2,2,2,2,4,4,4,4,8,8,8,8,16,16,16,16,32,32,32,32 ... ending with 255,};
Make the values the logarithmic sweep that you want. I'd use excel to find what those are.

You can make 255 values long, and use this to look up the result to display:
byte displayValue = lookupArray[(analogRead(5)>>2)]; // read pot on A5, divide by 4, then look up logarithmic mapping

Or make it 1023 entries long and use this:
byte displayValue = lookupArray[analogRead(5)]; // read pot on A5, then look up logarithmic mapping

Either way the result is 00 to FF
display the left half on one display, the right half on a second display.

Continue as posted previously:

rightDigit = displayValue & 0x0F; // data is now in bit positions 0 to 3
leftDigit =( displayValue & 0xF0)>>4; // move  bits into bit positions 0 to 3

Thank you, I will try this and will post if I get stuck somewhere.

Again, thanks for all the responses.