Go Down

### Topic: Split string into individual digits? (Read 1 time)previous topic - next topic

##### Feb 11, 2012, 10:56 pm
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.

#### AWOL

#1
##### Feb 11, 2012, 11:11 pm
You need to post your code.
"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.
I speak for myself, not Arduino.

#2
##### Feb 11, 2012, 11:36 pm
Its still bits and pieces but here it is.
Code: [Select]
`//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();}`

#### PaulS

#3
##### Feb 12, 2012, 12:23 am
Code: [Select]
`void loop() {   scrollDigits();}`
Why? Having loop(), which is called in an endless loop() do nothing more than make a function call seems silly.

Code: [Select]
`  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: [Select]
`int fracVoltage = (voltage - wholeVoltage) * 100.0;`

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

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

#4
##### Feb 12, 2012, 12:33 am
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.

#### PaulS

#5
##### Feb 12, 2012, 12:41 am
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.

#### stanys

#6
##### Feb 12, 2012, 12:43 am

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

Go Up