Help With Converting Char to Int

Im trying to take readings from my accelerometer and covert it to an int so i can send it to my servo

If it helps im using an arduino mega

But i get this error

invalid conversion from 'char' to 'const char*'

My code

// Example which uses the MMA_7455 library
// Moritz Kemper, IAD Physical Computing Lab
// moritz.kemper@zhdk.ch
// ZHdK, 03/04/2012
// Released under Creative Commons Licence

#include <Wire.h> //Include the Wire library
#include <MMA_7455.h> //Include the MMA_7455 library
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0; 
MMA_7455 mySensor = MMA_7455(); //Make an instance of MMA_7455

 char xVal, yVal, zVal; //Variables for the values from the sensor

void setup()
{
  
  myservo.attach(9);  
  Serial.begin(9600);
  // Set the sensitivity you want to use
  // 2 = 2g, 4 = 4g, 8 = 8g
  mySensor.initSensitivity(2);
  // Calibrate the Offset, that values corespond in 
  // flat position to: xVal = -30, yVal = -20, zVal = +20
  // !!!Activate this after having the first values read out!!!
  mySensor.calibrateOffset(0.23, 32, -79);
}

void loop()
{
  xVal = mySensor.readAxis('x'); //Read out the 'x' Axis
  yVal = mySensor.readAxis('y'); //Read out the 'y' Axis
  zVal = mySensor.readAxis('z'); //Read out the 'z' Axis
  
  pos = atoi(xVal);
  myservo.write(pos);     
  
    Serial.print(xVal, DEC);
  Serial.print("\t");
  Serial.print(yVal, DEC);
  Serial.print("\t");
  Serial.println(zVal, DEC);
  Serial.println("");
  
  delay(150);
}

Where does this error occur?

It happens when i try and upload it. then it highlights this line

pos = atoi(xVal);
char xVal,

Does that explain things?

Not quite, sorry for being a little daft with this i'm sure its pretty much obvious. Please could you explain your thought on this a little bit.

Well, atoi works on strings, and strings are arrays of char (with a zero terminator), not single chars.

OH right thank you.

I'll change it. Thank you

is there a version of this for char ?

"This"?
Surely the accelerometer return value isn't a string? (sorry, I don't have this library)

Hint -- char is an integer type ...... that you're trying to use a function to convert to an integer. :smiley:

If xVal is always a single digit character, you don't need to convert it to a string. You can use:

pos = xVal - '0';   // '0' = a zero character

For example, suppose you get the single character '5' and assign it into xVal. If you look up the ASCII code for '5', you will find it equals 53. Likewise, the ASCII value for a zero character is 48. Then:

pos = xVal -'0';
pos = '5' - '0';
pos = 53 - 48;
pos = 5;

and is now a numeric value, rather than a character. If you get more than a single character, you'' need to convert it to a string.

OH MY GOD I AM SO SODDING STUPID

I can't beleive i have done this. Ive been up 30 hours maybe thats it but i cant beleive if forgot this. Thanks man

It's one of those "flat forehead" mistakes...you know, where you slam the heel of your hand into your forehead, wondering how you could forget something so obvious. We've all been there...

Oh i know. and i ts ALWAYS the simple stuff. I have a tendency to over complicate things