Pages: [1]   Go Down
Author Topic: converting char to int  (Read 24942 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
value = atoi(inChar);

value = int(inChar);

value = (int)inChar;

Here is a snapshot of our constantly changing code:

Code:
#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.
« Last Edit: November 26, 2012, 01:41:27 am by Coding Badly » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Describe "garbage"
Logged

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int someInt = someChar - '0';
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 1
Hi.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Not really.

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

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

Hello, you have to do something like this:
Code:
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 smiley-grin
« Last Edit: November 26, 2012, 04:50:35 am by guix » Logged

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

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

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

Hello, yes it's because of the type of the variable "number": http://arduino.cc/en/Reference/Int

You can change:
Code:
int number = atoi( input );
to
Code:
long number = atol( input ); //notice the function change to atoL
or, if you want to use only positive values:
Code:
unsigned long number = strtoul( input, NULL, 10 );

smiley
« Last Edit: November 28, 2012, 10:03:45 pm by guix » Logged

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

Wow!  Thanks so much
Logged

Pages: [1]   Go Up
Jump to: