Go Down

### Topic: Converting int into char array. (Read 28270 times)previous topic - next topic

#### zdillman

##### Oct 11, 2012, 12:30 am
Hello fellow arduinians!

I am trying to convert a three digit integer into three digits in a char array to analyze each one. My use for this is reading a voltage, seperating the digits of the number the arduino makes, and reading them. I am using a piezo buzzer to buzz out each digit with a pause in between. For some reason when I run this, it beeps a random number of times, pauses, then does that over and over. Here's my code for reference. Thanks!

Code: [Select]
int val = 0, val1 = 0, val2 = 0, val3 = 0, finalval = 0, addfloat = 0, first, second, third;
char array[10]; */
String convert;

void setup() {

pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
delay(3000);
}

void loop() {

delay(300);
delay(300);
delay(300);

addfloat = val + val1 + val2 + val3;

convert += finalval;

convert.toCharArray(array, 10);

first = 0;
while (first != array[1]) {

tone(1, 4500);
delay(250);
noTone(1);
delay(250);
first++;
}

delay(2000);

second = 0;
while (second != array[2]) {

tone(1, 4500);
delay(250);
noTone(1);
second++;
delay(250);
}

delay(2000);

third = 0;
while (third != array[3]) {

tone(1, 4500);
delay(250);
noTone(1);
third++;
delay(250);
}

delay(5000);

}

#### Nick Gammon

#1
##### Oct 11, 2012, 02:22 am
You could do the maths (get the modulus of 10 etc.) but the simplest is probably to do sprintf into a buffer.

eg.

Code: [Select]

char buf [4];
sprintf (buf, "%03i", finalval);

Then you just subtract '0' from each digit to get the "binary" number.

eg.

Code: [Select]

int digit1, digit2, digit3

digit1 = buf [0] - '0';
digit2 = buf [1] - '0';
digit3 = buf [2] - '0';

The code above assumes that finalval won't exceed 999.

Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### zdillman

#2
##### Oct 11, 2012, 02:45 am
Awesome! Thanks for this. I also found a way using modulus like you said. However I am getting roughly the same number no matter what voltage analogRead reads. Here is my revised code. I am going to use the sprintf method and see if it makes any difference. Thanks!

Code: [Select]
int val = 0, val1 = 0, val2 = 0, val3 = 0, finalval = 0, first, second, third;

void setup() {

pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
delay(5000);
}

void loop() {

finalval = val;

int one = finalval / 100;
int two = (finalval / 10) % 10;
int three = finalval % 10;

first = 0;
while (first != one) {

tone(1, 4500);
delay(250);
noTone(1);
delay(250);
first++;
}

delay(2000);

second = 0;
while (second != two) {

tone(1, 4500);
delay(250);
noTone(1);
second++;
delay(250);
}

delay(2000);

third = 0;
while (third != three) {

tone(1, 4500);
delay(250);
noTone(1);
third++;
delay(250);
}

delay(5000);

}

#### Nick Gammon

#3
##### Oct 11, 2012, 02:55 am
I would temporarily echo your figures through the serial port to confirm you are getting and calculating what you expect.
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### zdillman

#4
##### Oct 11, 2012, 03:17 am
There's the problem I'm running into. I'm using an ATTiny85. I can't print anything because there isn't a serial out. This is why I'm using the buzzer.

#### Dyslexicbloke

#5
##### Oct 11, 2012, 04:17 amLast Edit: Oct 11, 2012, 04:27 am by Dyslexicbloke Reason: 1
I am very new to all this but found that there are functions in the core library's that will 'format' a number and make a string.
Since strings are arrays, using dtostrf (DoubleToString formatted) you can go from 123.456 to [1],[2],[3],[.],[4],[6],[\0] in 1 step.
Code: [Select]

void TestCode(){
float iVarToCast = 123.456;
char buffer[7];
dtostrf(iVarToCast, 7, 0, buffer);
lcd.print(buffer);
}

dtostrf(YourNumber, TotalStringLength, NumberOfDecimals, TheTargetArray)
TotalStringLength    > Must include all characters the '.' and the null terminator
NumberOfDecimals  > The output is rounded .456 become .46 at 2 decimals
TheTargetArray      > I suspect 'char strBuffer[]' will work with out a preceding definition but I haven't tried yet

Hope this helps
Al
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

#### Nick Gammon

#6
##### Oct 11, 2012, 04:34 am

There's the problem I'm running into. I'm using an ATTiny85. I can't print anything because there isn't a serial out. This is why I'm using the buzzer.

There are ways of getting serial out, but are you sure you are getting good readings? How is it wired up?
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### guix

#7
##### Oct 11, 2012, 04:36 am

There's the problem I'm running into. I'm using an ATTiny85. I can't print anything because there isn't a serial out. This is why I'm using the buzzer.

Why not convert int to Morse then

#### Dyslexicbloke

#8
##### Oct 11, 2012, 04:47 am
Sorry ...
TheTargetArray      >  'char strBuffer[]' will not work, you have to initialise the array first.

sizeof() will just report the whole array size not its contents.
The string object will do what you need, conversion, finding length and picking individual characters but it is apparently memory hungry.
That said you could easily step through any array with a simple loop.
Al
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

#### lloyddean

#9
##### Oct 11, 2012, 06:35 am
Wrote this (untested code) before the discussion.

If I understood what you wanted to do this should do it (I believe) using less memory than any of the presented methods.

Code: [Select]

const uint8_t       pinSPEAKER          = 1;
const uint8_t       pinANALOG_SENSOR    = 2;

const unsigned long ONE_SECOND          = 1000UL;
const unsigned long TWO_SECONDS         = 2UL * ONE_SECOND;
const unsigned long THREE_SECONDS       = 3UL * ONE_SECOND;

const unsigned long TONE_TIME_ON        = 250UL;
const unsigned long TONE_TIME_OFF       = 250UL;

const unsigned long SAMPLE_DELAY        = 300UL;

const int           SAMPLE_COUNT        = 4;

void signal(const int n)
{
for ( int i = n; i--; )
{
tone(1, 4500);
delay(TONE_TIME_ON);

noTone(1);
delay(TONE_TIME_OFF);
}

delay(THREE_SECONDS);
}

void signal_number(int value)
{
int     place;
int     digit;

if ( value < 10 )
{
signal(value);
}
else
{
place = value / 10;
signal_number(place);

digit = value % 10;
signal(digit);
}
}

void loop()
{
int val     = 0;
for ( int i = SAMPLE_COUNT, val = 0; i--; )
{
delay(SAMPLE_DELAY);
}

int average = val / SAMPLE_COUNT;
signal_number(average);
}

void setup()
{
pinMode(pinSPEAKER, OUTPUT);

delay(THREE_SECONDS);
}

#### kbell101

#10
##### Jun 14, 2016, 06:17 pm
Dyslexic bloke, you are my hero for the day.  I've been looking for this function and it works perfectly for sending acquired doubles over a radio, which is expecting a string / array.

I am very new to all this but found that there are functions in the core library's that will 'format' a number and make a string.
Since strings are arrays, using dtostrf (DoubleToString formatted) you can go from 123.456 to [1],[2],[3],[.],[4],[6],[\0] in 1 step.
Code: [Select]

void TestCode(){
float iVarToCast = 123.456;
char buffer[7];
dtostrf(iVarToCast, 7, 0, buffer);
lcd.print(buffer);
}

dtostrf(YourNumber, TotalStringLength, NumberOfDecimals, TheTargetArray)
TotalStringLength    > Must include all characters the '.' and the null terminator
NumberOfDecimals  > The output is rounded .456 become .46 at 2 decimals
TheTargetArray      > I suspect 'char strBuffer[]' will work with out a preceding definition but I haven't tried yet

Hope this helps
Al

Go Up

Please enter a valid email to subscribe