Pages: [1]   Go Down
Author Topic: Converting int into char array.  (Read 7646 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
 
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

eg.

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

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

eg.

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


Logged

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

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:
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);
 
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Staffordshire UK
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Just a little bit out there ...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: October 10, 2012, 09:27:37 pm by Dyslexicbloke » Logged

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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 978
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Logged

Staffordshire UK
Offline Offline
Full Member
***
Karma: 1
Posts: 132
Just a little bit out there ...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

Pages: [1]   Go Up
Jump to: