Converting int into char array.

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!

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() {

  val = analogRead(2);
  delay(300);
  val1 = analogRead(2);
  delay(300);
  val2 = analogRead(2);
  delay(300);
  val3 = analogRead(2);
  
  addfloat = val + val1 + val2 + val3;
  
  finalval = addfloat / 4;
  
 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);
  
}

You could do the maths (get the modulus of 10 etc.) but the simplest is probably to do sprintf into a buffer.

eg.

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

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

eg.

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.

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!

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() {

  val = analogRead(2);
  
  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);
  
}

I would temporarily echo your figures through the serial port to confirm you are getting and calculating what you expect.

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.

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.

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

zdillman:
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?

zdillman:
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 :smiley:

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

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.

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--; )
    {
        val = analogRead(pinANALOG_SENSOR);
        delay(SAMPLE_DELAY);
    }

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

void setup()
{
    pinMode(pinSPEAKER, OUTPUT);
    
    delay(THREE_SECONDS);
}

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.

Dyslexicbloke:
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],1. ,[4],[6],[\0] in 1 step.

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
[/quote]

a little late for the original poster but it might help others;

a simple way that works for me to convert int to char array is to convert the int to string and then to char.

int a = 128;
String b;
char c[3];

b = (string)a;
b.toCharArray(c, 3);

then I can send it in my case:

radio.write(&c, sizeof(c));

1 Like