Go Down

### Topic: Reading input from potentiometer (Read 4801 times)previous topic - next topic

#### fsimon

##### May 03, 2013, 07:10 pmLast Edit: May 03, 2013, 07:20 pm by fsimon Reason: 1
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 :

Code: [Select]
`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.

#### HazardsMind

#1
##### May 03, 2013, 08:42 pmLast Edit: May 03, 2013, 08:53 pm by HazardsMind Reason: 1
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### AWOL

#2
##### May 03, 2013, 09:24 pm
Code: [Select]
`displayValue = 00;`
Using octal literals when they're not warranted is not a good idea.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#3
##### May 03, 2013, 10:21 pm
Getting the data to display is easy. analogRead returns a 10-bit value,
000 to 3FF.
Divide that by 4:
Code: [Select]
`byte displayValue = (analogRead(5)>>2; // read pot on A5result is 00 to FFdisplay the left half on one display, the right half on a second display.rightDigit = displayValue & 0x0F; // data is now in bit positions 0 to 3leftDigit =( displayValue & 0xF0)>>4; // move  bits into bit positions 0 to 3Next, 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  belowdigit2pinsArray[] = {9,10,11,12,13,14,15}; // A0,1,2,3 are 14,15,16,17don't forget current limit resistorsNow, create a font map for digits 0-9,A,b,c,d,E,Fbyte fontArray [] = {B00111111, // 0  1 = segment onB00000110, // 1  bit 0 = segment A, 1 = b, 2 = c, 3 = d, 4 = e, 5 = f, 6 = g, 7 = not used or decimal pointB01011011, // 2                 aB01001111, // 3         f              bB00100110, // 4                g:                                   e              c:                                          d : etc for rest of digitsB01110001, // 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 Aif ((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 Bif ((fontArray[leftDigit] & B00000010) == 1){digitalWrite(digit1pinsArray[1], HIGH);}else {digitalWrite(digit1pinsArray[1]), LOW);}// bit 2, mask for segment Cif ((fontArray[leftDigit] & B00000100) == 1){digitalWrite(digit1pinsArray[2], HIGH);}else {digitalWrite(digit1pinsArray[2]), LOW);}// bit 3, mask for segment Dif ((fontArray[leftDigit] & B00001000) == 1){digitalWrite(digit1pinsArray[3], HIGH);}else {digitalWrite(digit1pinsArray[3]), LOW);}// bit 4, mask for segment Eif ((fontArray[leftDigit] & B00010000) == 1){digitalWrite(digit1pinsArray[4], HIGH);}else {digitalWrite(digit1pinsArray[4]), LOW);}// bit 5, mask for segment Fif ((fontArray[leftDigit] & B00100000) == 1){digitalWrite(digit1pinsArray[5], HIGH);}else {digitalWrite(digit1pinsArray[5]), LOW);}// bit 6, mask for segment Gif ((fontArray[leftDigit] & B0100000) == 1){digitalWrite(digit1pinsArray[6], HIGH);}else {digitalWrite(digit1pinsArray[6]), LOW);}`
Repeat for digit2pinsArray[] and rightDigit

Now put all the pieces together!
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### American2020

#4
##### May 03, 2013, 11:20 pm
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

Code: [Select]
`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 Registersvoid 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.

#### fsimon

#5
##### May 04, 2013, 02:40 pm
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.

#### Klaus_ww

#6
##### May 04, 2013, 03:09 pm
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.
Wenn ich hier sehr knapp antworte dann wegen der elenden Tipperei auf dem Tablet.

#### fsimon

#7
##### May 04, 2013, 03:32 pm
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.

#### zoomkat

#8
##### May 04, 2013, 04:30 pm
Quote
Can anyone give me a done project so that atleast I can simulate and test it.

Is this a class assignment for a grade?
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

#### fsimon

#9
##### May 04, 2013, 05:00 pm
No, this is a project that I want to do as a hobby project - to learn and experiment more with the Arduino.

#10
##### May 04, 2013, 05:23 pm
Change this part of what I provided to be array based:
Code: [Select]
`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 mappingOr make it 1023 entries long and use this:byte displayValue = lookupArray[analogRead(5)]; // read pot on A5, then look up logarithmic mappingEither way the result is 00 to FFdisplay 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 3leftDigit =( displayValue & 0xF0)>>4; // move  bits into bit positions 0 to 3`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### fsimon

#11
##### May 06, 2013, 12:55 pm
Thank you, I will try this and will post if I get stuck somewhere.

Again, thanks for all the responses.

Go Up