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:
value = atoi(inChar);
value = int(inChar);
value = (int)inChar;
Here is a snapshot of our constantly changing 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: </mark> <mark>[code]</mark> <mark>
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:
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.
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 );
}
}
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?
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 );
}
}}