Go Down

Topic: converting char to int (Read 28 times) previous topic - next topic

viper2x21

Apr 28, 2012, 07:56 pm Last Edit: Nov 26, 2012, 07:41 am by Coding Badly Reason: 1
For our project, we are having trouble converting a char value, read in from Serial.read();, to an int value.  Here is our situation:

On one side, we have an arduino reading in values from a pot on a breadboard.  It is then sending these values, via a Bluetooth Bee module mounted on the arduino to the other side.

The data is received on the other side AS A CHAR and we are able to print the value of the pot on the serial monitor.  However, when we attempt to convert the value of the pot to an integer value, we always receive garbage data.  We have tried several methods to do this including:

Code: [Select]
value = atoi(inChar);

value = int(inChar);

value = (int)inChar;


Here is a snapshot of our constantly changing code:

Code: [Select]
#include <stdlib.h>

long DATARATE = 38400;  // default data rate for BT Bee

char inChar;
char inData[5];
int index = 0; //counter variable
int val = 0; //value to come from inData conversion using atoi()
int  LED = 13;   // Pin 13 is connected to a LED on many Arduinos

void setup() {

 Serial.begin(DATARATE);

 // bluetooth bee setup
 Serial.print("\r\n+STWMOD=0\r\n");     // set to slave
 delay(1000);
 Serial.print("\r\n+STNA=beiber\r\n");     // DSC = digital setting circles
 delay(1000);
 Serial.print("\r\n+STAUTO=1\r\n");     // don't permit auto-connect
 delay(1000);
 Serial.print("\r\n+STOAUT=1\r\n");     // existing default
 delay(1000);
 Serial.print("\r\n +STPIN=0000\r\n");  // existing default
 delay(2000);  // required

 // initiate BTBee connection
 Serial.print("\r\n+INQ=1\r\n");
 delay(2000);   // wait for pairing

 pinMode(6, OUTPUT);  
}

void loop()
{
 if (Serial.available())
 {
   inChar = Serial.read();
   //Serial.print("-------");
   Serial.print(inChar);
   //Serial.print("-------");
   //val = atoi(&inChar);
   //Serial.print(val);  
   //analogWrite(6, val); //send integer to pin 6
 }
}

 /*for (index = 0; index < 4; index++)
   {
     inData[index] = '\0';
   }*/

Please let me know if I am not clear or if more information is needed.  Thanks so much!


Moderator edit: [code] [/code] tags added.

AWOL

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

Arrch

Values read from an analog pin are likely going to be more than a single digit. When you run Serial.read(), it returns a single "digit", so if the value read was 560, the first serial.read() call will return '5', the second will return '6', and the third will return '0'. You need to know when the value is finished sending.

If you just want to convert a single character into an int:
Code: [Select]
int someInt = someChar - '0';

Techone

I did something like that using an Arduino to read a pot value, send it to a PC and the PC send back the voltage value.

I did a code for the Ardiuno and for the PC, a Processing code. I was doing a test using an DYI opto-coupler linker.

Here the tread : http://arduino.cc/forum/index.php/topic,94382.0.html  Just look at the latest code in the tread. I know, it got a bug in it, but the value being send by the Arduino, and the value being send by the PC is OK. The convertion program section of both code ( Arduino & Processing ) is just fine.  About the bug... I have to monitor the serial data going into the Arduino so I can investigated , maybe re-code the receiving section to get it right. The data convertion is just fine. Check it out.

EricRRosenbaum

Characters can be used for mathematics as the Arduino Reference Pages point out. For example it is possible to code:

char character[7]

int number = character[5]+0

and get a number in the variable number without errors as the result

Nick Gammon

Not really.

Code: [Select]
sketch_nov26a.cpp: In function 'void setup()':
sketch_nov26a:5: error: expected initializer before 'int'


guix

#6
Nov 26, 2012, 10:47 am Last Edit: Nov 26, 2012, 10:50 am by guix Reason: 1
Hello, you have to do something like this:
Code: [Select]

if ( Serial.available () > 0 )
{
 static char input[16];
 static uint8_t i;
 char c = Serial.read ();

 if ( c != '\r' && i < 15 ) // assuming "Carriage Return" is chosen in the Serial monitor as the line ending character
   input[i++] = c;
   
 else
 {
   input[i] = '\0';
   i = 0;
     
   int number = atoi( input );
   Serial.println( number );
 }
}


Edit: oups, just noticed the date :D

Steve_edge

If i use Guix's suggested code in the following sketch and entre an interer into the serial monitor, it returns that same number until I get to numbers in the 35000 plus range then it retuns negative values that dont "seem" to have any relatioship to the number I entred.  Any idea why this is so?

Code: [Select]
void setup() {
 
  Serial.begin(9600);
}
void loop() {
if ( Serial.available () > 0 )
{
  static char input[16];
  static uint8_t i;
  char c = Serial.read ();

  if ( c != '\r' && i < 15 ) // assuming "Carriage Return" is chosen in the Serial monitor as the line ending character
    input[i++] = c;
   
  else
  {
    input[i] = '\0';
    i = 0;
     
    int number = atoi( input );
   
    Serial.println( number );
  }
}}

guix

#8
Nov 29, 2012, 03:05 am Last Edit: Nov 29, 2012, 04:03 am by guix Reason: 1
Hello, yes it's because of the type of the variable "number": http://arduino.cc/en/Reference/Int

You can change:
Code: [Select]

int number = atoi( input );

to
Code: [Select]

long number = atol( input ); //notice the function change to atoL

or, if you want to use only positive values:
Code: [Select]

unsigned long number = strtoul( input, NULL, 10 );


:)

Steve_edge


Go Up