Pages: [1]   Go Down
Author Topic: Split string into individual digits?  (Read 482 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
:D
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

I have a string that I read from analogRead and did a bit of math too. It looks something like this it can range from 1.95 to 10.25.
I need to write this to 3 seven segment displays. For that I chose a MAX7219 and the ledcontrol library. Now since im using only 3 of them, I get that I not only need to round the 10.25 to 10.3 or something like that.
My other problem now is to split that string and display it on the seven segments.

Thanks in advance,
Nadav
Logged

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

You need to post your code.
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.

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

Its still bits and pieces but here it is.
Code:
//We always have to include the library
#include "LedControl.h"
const int voltPin = 0;
float denominator;
int resistor1 = 6;
int resistor2 = 1.5;
LedControl lc=LedControl(7,6,5,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=250;

void setup() {
  denominator = (float)resistor2 / (resistor1 + resistor2);
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
}

/*
  This method will scroll all the hexa-decimal
 numbers and letters on the display. You will need at least
 four 7-Segment digits. otherwise it won't really look that good.
 */
void scrollDigits() {
  float voltage;
  voltage = analogRead(voltPin);
  voltage = (voltage / 1024) * 5.0;
  voltage = voltage / denominator;
 
    lc.setDigit(0,3,i,false);
    lc.setDigit(0,2,i+1,false);
    lc.setDigit(0,1,i+2,false);
    lc.setDigit(0,0,i+3,false);
    delay(delaytime);
  }
  lc.clearDisplay(0);
  delay(delaytime);
}

void loop() {

  scrollDigits();
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49022
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop() {
  scrollDigits();
}
Why? Having loop(), which is called in an endless loop() do nothing more than make a function call seems silly.

Code:
  float voltage;
  voltage = analogRead(voltPin);
  voltage = (voltage / 1024) * 5.0;
  voltage = voltage / denominator;
This could be done in one statement, with multiplication only, and no magic numbers, with a little forethought.

Store the value in an int, then, That will get you the whole number.
Quote
int wholeVoltage = voltage;

Then, subtract the whole number, multiply the rest by 100. Store the value in an int.
Code:
int fracVoltage = (voltage - wholeVoltage) * 100.0;

Then, add 5 and divide by 10.
Code:
fracVoltage += 5;
fracVoltage /=10;

If voltage was 3.262, wholeVoltage would be 3, and fracVoltage starts as 26, becomes 31, then become 3.

Logged

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

Right that makes sense, except that I only want to round if the voltage has 4 digits. So if the voltage is 10 or more then round. If not, then just display that number to the seven segment.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49022
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So if the voltage is 10 or more then round. If not, then just display that number to the seven segment.
Feel free to make that change.
Logged

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

voltage=analogRead(0);

int volt_int;
int volt_dec;

voltage=voltage+0.5; // round approssimation

volt_int=(int)voltage;
volt_dec=(int)((voltage-(int)voltage)*100)



then put them to decode driver of 7 segments...
Logged

Pages: [1]   Go Up
Jump to: