Go Down

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

zdillman

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

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

Nick Gammon

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.



zdillman

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

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

Nick Gammon

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

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.

Go Up