Quad Alphanumeric Display displaying double value code?

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
bane
 
Posts: 5
Joined: Tue Nov 19, 2013 8:50 pm

Quad Alphanumeric Display displaying double value code?

Post by bane »

Hello all,
I just got around hooking up my Quad Alphanumeric display and tested out the example code. Everything looks works great but I'm trying to display a temperature value and having issues showing the decimal point as part of a character. The temperature value is a double "XX.XX" so I figured out how to convert a double to a char array. However, this makes five chars converting the decimal to a char (as it should), but I would like to have the decimal be displayed as part of the appropriate character. I don't have any problem writing the logic to take in the char value and reassign it to a "raw" hex value but I don't know what that hex value is. For example, my temp sensor gives me 76.45 and current code breaks it up in to char [7][6][.][4][5] , I would like it to do char [7][6.][4][5]. Does anyone know of a table that has [0.] through [9.] and what it would be in hex? I've googled around and have found such tables for 7 segment displays but nothing with the characters that I want for one this size.
Thanks in advance!

Code: Select all

#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <OneWire.h>
#include <DallasTemperature.h>

Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

double temp;
char displaybuffer[4] = {' ', ' ', ' ', ' '};

void setup()
{
  Serial.begin(9600);
  sensors.begin();
  alpha4.begin(0x70);  
}

void loop()
{
sensors.requestTemperatures(); //request temperature
temp=sensors.getTempCByIndex(0)*1.8+32; //convert to f
dtostrf(temp,2,2,displaybuffer); //convert double to char array
 alpha4.writeDigitAscii(0, displaybuffer[0]); //display char 1
 alpha4.writeDigitAscii(1, displaybuffer[1]); //display char 2
 alpha4.writeDigitAscii(2, displaybuffer[2]); //display char 3
 alpha4.writeDigitAscii(3, displaybuffer[3]); //display char 4

  alpha4.writeDisplay();
  delay(200);
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Quad Alphanumeric Display displaying double value code?

Post by adafruit_support_rick »

There is a third, optional argument to writeDigitAscii. If you pass true, it will light the decimal point to the lower right of the character. if you pass false (the default), the decimal point will remain off.

This routine will print a string such as you have, but will check for a '.' character and pass the appropriate boolean.

Code: Select all

void ledprint(char* str, Adafruit_AlphaNum4 *alphanum)
{
  for (int i = 0; i < strlen(str); i++)      //for each character in str
  {
    if ('.' == str[i+1])                    //if the next character after this one is '.'
    {
      alphanum->writeDigitAscii(i, str[i], true);  //write the '.' along with the character
      i++;                                  //skip over the '.'
    }
    else
    {
      alphanum->writeDigitAscii(i, str[i]);  //write the character
    }
  } 
  alphanum->writeDisplay();  //write to the display.
}

User avatar
bane
 
Posts: 5
Joined: Tue Nov 19, 2013 8:50 pm

Re: Quad Alphanumeric Display displaying double value code?

Post by bane »

Awesome, works great now
Thanks!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Quad Alphanumeric Display displaying double value code?

Post by adafruit_support_rick »

You're welcome!

User avatar
disasterarea
 
Posts: 7
Joined: Fri May 16, 2014 2:22 pm

Re: Quad Alphanumeric Display displaying double value code?

Post by disasterarea »

Rick, I was trying to use this code to print a string with a decimal and ran into some issues. I thought I'd post here to show how I got around them.

If you use the code as supplied, the for() loop checks the next upcoming character for a decimal point "." and if it finds one, appends the decimal to the currently printing digit.

'120.4' -> when i == 2, the look ahead will put the decimal point on the 3rd digit. This works fine. But the i++ skips the digit that would contain the "." and then the next thing that happens is:

display.writedigitAscii(i, str); This means it will try to print the fourth char in the string (ok so far) to digit 4 (3++). There is no digit 4 on the quad backpack, so you end up printing '120. '.

Here's how I fixed it:

Code: Select all

void ledprint(char* str)
{
  byte shift = 0;
  
  for (int i = 0; i < strlen(str); i++)      //for each character in str
  {
    if (str[i] == '.')  // if there is a decimal point
    {
      display.writeDigitAscii(i-1, str[i-1], true);  // go back and rewrite the last digit with the dec point
      shift++;  // increment the shift counter
    }
    else
    {
      display.writeDigitAscii(i-shift, str[i]);  //write the character
    }
  } 
  display.writeDisplay();  //write to the display.
}
Instead of looking ahead, we look at the current digit for the presence of the decimal point. If we see the decimal point, we go back and re-write the preceding value with the decimal added, and add one to the shift counter. When we go back to write to next char in the string, we write it to the correct location on the display.

User avatar
chicagoedist
 
Posts: 13
Joined: Sat Sep 07, 2013 10:21 am

Re: Quad Alphanumeric Display displaying double value code?

Post by chicagoedist »

I have a really dumb question, how do we call the ledprint function that Rick showed above?

void ledprint(char* str, Adafruit_AlphaNum4 *alphanum)

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Quad Alphanumeric Display displaying double value code?

Post by adafruit_support_rick »

Assuming you have an Adafruit_AlphaNum4 object , like so:

Code: Select all

Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
You can call it like this:

Code: Select all

ledprint("12.34", &alpha4);

Joeytribbiani
 
Posts: 7
Joined: Tue Feb 04, 2014 7:22 am

Re: Quad Alphanumeric Display displaying double value code?

Post by Joeytribbiani »

Hi disasterarea (or any other with a working code)

Will you post your whole code? I have tried to use the part you have posted, but i do not have the skills to get it working with my load cell..

I have tried Rick's code from this tropic: viewtopic.php?f=47&t=59724&hilit=quad+display


But I have some problemes with the ledprint function.
float m = samples.getMedian()/10.0; // the value from the load cell
ledprint("m", &alpha4);
It just shows an "m" on display, I would like the display to show the value from the load cell.

Code: Select all

#include "HX711.h"
#include <EEPROMex.h>
#include <EEPROMVar.h>
#include <Wire.h>
#include "OneButton.h"
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <LiquidCrystal.h>
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
LiquidCrystal lcd(4);


// Til quad display, men virker ikke ordenligt, kan ikke vise dicimal tal korrekt
// double temp;
// char displaybuffer[4] = {' ', ' ', ' ', ' '};



/////////// Quad Display for ledprint
void ledprint(char* str, Adafruit_AlphaNum4 *alphanum)
{
  int pos = 0;
  for (int index = 0; index < strlen(str); index++)      //for each character in str
  {
    if ('.' == str[index+1])                    //if the next character after this one is '.'
    {
      alphanum->writeDigitAscii(pos, str[index], true);  //write the '.' along with the character
      index++;                                  //skip over the '.'
    }
    else
    {
      alphanum->writeDigitAscii(pos, str[index]);  //write the character
    }
    pos++;
  } 
  alphanum->writeDisplay();  //write to the display.
}


///////

#define Button_PIN 13

OneButton Button1(Button_PIN, true);

HX711 scale(A0, A1);		// parameter "gain" is ommited; the default value 128 is used by the library

#include "RunningMedian.h"

RunningMedian samples = RunningMedian(20);

long count = 0;


//

void setup() {


Button1.setClickTicks(10);

Button1.setPressTicks(2000);

pinMode (Button_PIN, INPUT_PULLUP);

// Noget med HX711 og CAl måske

                float Scale = EEPROM.readFloat(0);
                delay(25);
                scale.set_scale(Scale); // this value is obtained by calibrating the scale with known weights; see the README for details

                delay(300);

                scale.tare();  // reset the scale to 0
                delay(500);

       Serial.begin(38400);
      
       alpha4.begin(0x70);  // pass in the address
      
       lcd.begin(16, 2);

			 
        delay(100);
}

void loop() {
  
  if (count % 20 == 0);
  count++;

  long x = scale.get_units(5)*10; // jeg ganger med 10, for at få en decimal med inden værdien konverteres til en "long"

  samples.add(x);

//  LCD DISPLAY
         float m = samples.getMedian()/10.0;
         lcd.setCursor(0,0);
         lcd.print(m); 
           //     lcd.setCursor(0,1);
          //      lcd.print(scale.get_units(10), 1);

// Til Quad display

ledprint("m", &alpha4);  // It just show an "m" on the display, not the value from the load cell

  /* // Cannot show decimal numbers
 dtostrf(m,2,2,displaybuffer); //convert double to char array         
 alpha4.writeDigitAscii(0, displaybuffer[0]); //display char 1
 alpha4.writeDigitAscii(1, displaybuffer[1]); //display char 2
 alpha4.writeDigitAscii(2, displaybuffer[2]); //display char 3
 alpha4.writeDigitAscii(3, displaybuffer[3]); //display char 4
alpha4.writeDisplay();
*/   
Kind regards

Joeytribbiani
 
Posts: 7
Joined: Tue Feb 04, 2014 7:22 am

Re: Quad Alphanumeric Display displaying double value code?

Post by Joeytribbiani »

I got some help from a friend and got it working. If any other in the future in counters same problem, then this may help you:

Code: Select all

#include "HX711.h"

#include <Wire.h>

// for quad display
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
char BUFFERSTRING[20]; // 

//for filter
#include "RunningMedian.h"
RunningMedian samples = RunningMedian(15);
long count = 0;



void setup() {


      
       alpha4.begin(0x70);  // pass in the address
      		 
        delay(100);
}

void loop() {
  
  if (count % 20 == 0);
  count++;

  long x = scale.get_units(6)*10; // jeg ganger med 10, for at få en decimal med inden værdien konverteres til en "long"

  samples.add(x);

//dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
//you can adjust "Widt" to move the dot and PRECSISION for more og less decimal numbers
dtostrf(m,5,1,BUFFERSTRING);  // 
//Serial.println(BUFFERSTRING);


ledprint(BUFFERSTRING, &alpha4); 
  // write it out!
  alpha4.writeDisplay();
 
}

void ledprint(char* str, Adafruit_AlphaNum4 *alphanum)
{
  int pos = 0;
  for (int index = 0; index < strlen(str); index++)      //for each character in str
  {
    if ('.' == str[index+1])                    //if the next character after this one is '.'
    {
      alphanum->writeDigitAscii(pos, str[index], true);  //write the '.' along with the character
      index++;                                  //skip over the '.'
    }
    else
    {
      alphanum->writeDigitAscii(pos, str[index]);  //write the character
    }
    pos++;
  } 
  alphanum->writeDisplay();  //write to the display.
}


           

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”