Pages: [1]   Go Down
Author Topic: Reading input from potentiometer  (Read 701 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: May 03, 2013, 12:20:25 pm by fsimon » Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2945
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 03, 2013, 01:53:22 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24453
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
displayValue = 00;
Using octal literals when they're not warranted is not a good idea.
Logged

"Pete, it's a fool 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.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 443
Posts: 23837
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Getting the data to display is easy. analogRead returns a 10-bit value,
000 to 3FF.
Divide that by 4:
Code:
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!
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 8
Posts: 252
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

My 3x3x3 Led Cube

[url=http://arduino.cc/forum/index.php/topic,1642

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Sr. Member
****
Karma: 3
Posts: 435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 443
Posts: 23837
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Again, thanks for all the responses.
Logged

Pages: [1]   Go Up
Jump to: