beginners serial question.

Hi all,

I have an accerometer (SEN00849) connected to two analog pins on an arduino pro mini. The analog values are then sent via xbee to an Arduino Mega.

Mini Pro code

const int analogInPin = 0; 
const int analogInPin1 = 1;

int sensorValue;       
int sensorValue1;       

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);     
  sensorValue1 = analogRead(analogInPin1);

  Serial.print(sensorValue);   
  Serial.print(" ");
  Serial.println(sensorValue1);   
 
  delay(10);                     
}

The resulting serial the the arduino mega is "234 432" (values range from 360 - 650 approx)

How might I go about splitting this result into 2 ints and assign them to pwm pins on the Arduino Mega end?

Best,
Adam

Use "atoi" to assign the string values to two "int"s, and then map them down to 0..255 before writing to the PWM.

Hi,
Thanks for the advice. I'm away from my arduino for a day so I'll use that time to read up on atoi.
Cheers,
Adam

I'm having problems with implementing atoi. I've tried a lot of the different methods for atoi form the forum but I can't seem to make it work. The only thing i seem to be able to do is convert

this:
345 650

into this
3
4
5

6
5
0

how could I conver

this

int inByte = Serial.read(); //which prints out as 345 650

to
int i = 345 and int b = 650

?

I know it's a question that gets asked a lot but I'm really struggling with this.

Cheers,
Adam

You need to form the characters '3 '4' '5' into a string, by storing them in a "char" array as you receive them, and terminating the array with a zero (that's a real zero, not an ASCII zero).

So, if you've got "char buffer [10];", then "buffer[0]" contains '3', "buffer [1]" contains '4', "buffer [2]" contains '5' and "buffer [3]" contains 0 (also written \0 ).

Now "int myInt = atoi (buffer);" will leave the integer 345 in "myInt".

Heya,
I feel like a complete idiot but am I at least on the right track?

I'm not sure how to add the char buffer into this.

Best,
Adam

#include <stdlib.h>
void setup(){
Serial.begin(9600);
Serial1.begin(9600);


}
int array[20];
     int num=0;
     char* val="0";


void loop() {
while(Serial1.available()>0){

      val[0]=Serial1.read();
      if (val [0] >= '0' && val [0] <= '9'){
    array[num]=atoi(val);
    Serial.println(val);
      }
    delay(10);
    }

}

Have a look at this thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273230655

    char val [10];
int index = 0;

void loop() {
  while(Serial1.available()>0){
    int inChar = Serial1.read ();
    if (inChar >= '0' && inChar <= '9') {
       val[index++]=inChar;
       val[index] = '\0'; // keep the string terminated.
       // may want to do some array bounds-checking
    } else {  // anything non-numeric terminates the read
      array[num]=atoi(val);
      index = 0;
      Serial.println(val);
   }
 }
}

Hi All,
Thanks a million for your time and advice. I followed your thread Pauls and have made something work. I'm not sure if it's anywhere near efficient but it works for now.
Cheers all,

heres what i've ended up going with on the xbee receiver end. It's not finished of course.

const int mosfetPin = 2;

// serial stuff
char inData[10];
int index;
boolean started = false;
boolean ended = false;

char inData1[10];
int index1;
boolean started1 = false;
boolean ended1 = false;


void setup(){
Serial.begin(9600);
Serial1.begin(9600);


}


void loop()
{
   while(Serial1.available() > 0)
   {
       char aChar = Serial1.read();
       if(aChar == '<')
       {
           started = true;
           index = 0;
           inData[index] = '\0';
       }
       else if(aChar == '>')
       {
           ended = true;
       }
       else if(started)
       {
           inData[index] = aChar;
           index++;
           inData[index] = '\0';
       }
    


        if(aChar == '{')
       {
           started1 = true;
           index1 = 0;
           inData1[index] = '\0';
       }
       else if(aChar == '}')
       {
           ended1 = true;
       }
       else if(started1)
       {
           inData1[index1] = aChar;
           index1++;
           inData1[index1] = '\0';
       }

   }

   if(started && ended)
   {
       // Convert the string to an integer
       int inInt = atoi(inData);
        Serial.print("x");
        Serial.println(inInt);
         inInt = map(inInt, 350, 650, 0, 255);
       // Use the value
        
        analogWrite(mosfetPin, inInt);
        ;
       // Get ready for the next time
       started = false;
       ended = false;
        
         

       index = 0;
       inData[index] = '\0';

   }
   if(started1 && ended1)
   {
       // Convert the string to an integer
       
         int inInt1 = atoi(inData1);

       // Use the value
      
        // Serial.print("y");
        //Serial.println(inInt1);
       // Get ready for the next time
       
        
         started1 = false;
       ended1 = false;

       

       index1 = 0;
       inData1[index1] = '\0';
   }
}