Go Down

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

fsimon

May 03, 2013, 07:10 pm Last 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 pm Last 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.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

AWOL

Code: [Select]
displayValue = 00;
Using octal literals when they're not warranted is not a good idea.
"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.

CrossRoads

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

American2020

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 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.
My 3x3x3 Led Cube

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

Go Up