Control Digital Pot from Serial

I have the Arduino Mega and I have a AD5206 digital potentiometer connected to it. And on the digipot’s 1st wiper I have an LED hooked up. I would like to be able to control the digital potentiometers resistance by typing a number from 0 to 255 into the serial monitor window. But I am not that great at programming. I am getting an error that says “cannot convert ‘String’ to ‘int’ for argument ‘2’ to ‘int digitalPotWrite(int, int)’”. Here is my code:

#include <SPI.h>                                 // inslude the SPI library:
#define SS_PIN    53                             // define the SS_PIN
#define MOSI_PIN  51                             // define the MOSI_PIN
#define MISO_PIN  50                             // define the MISO_PIN
#define SCK_PIN   52                             // define the SCK_PIN

const int slaveSelectPin = 53;                   // set pin 10 as the slave select for the digital pot:
char val;                                        // variable to receive data from the serial port
String data;                                     // string to store val characters in
String dataold;                                  // string to store data for later use

void setup()
  pinMode (slaveSelectPin, OUTPUT);              // set the slaveSelectPin as an output:
  SPI.begin();                                   // initialize SPI:
  Serial.begin(9600);                            // initialize Serial:

void loop() {
  if( Serial.available() )                       // if data is available to read
    val =;                         // read it and store it in 'val'
    if( val != '~' )                             // if not an '~'
      data+=val;                                 // add it to the data string
    else                                         // if '~' was received (our designated termination char)
      Serial.println("string received: "+data);  // print the stored string
      dataold = data;                            // store it in dataold for later use
      data = "";                                 // empty data for next string reading
  if (dataold != "" && val == '~')               // if dataold is not blank and val is '~' (the designated termination character)
    Serial.println(dataold);                   // print dataold to serial to make sure it is being stored properly
    digitalPotWrite(0, dataold);                 // write dataold to first channel on digital pot
    val = NULL;                                  // clear '~' from val so if command doesn't keep getting run

int digitalPotWrite(int address, int value) {
  // take the SS pin low to select the chip:
  //  send in the address and value via SPI:
  // take the SS pin high to de-select the chip:

check to convert string to a char array - then check atoi() for converting a char array to integer.

check the actual params for toCharArray() from the library. (somwhere under ~arduino install folder)