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:
[code] [/code] tags added.