Go Down

Topic: Serial.read() help (Read 14 times) previous topic - next topic

thefatmoop

#5
Jan 13, 2009, 03:25 am Last Edit: Jan 13, 2009, 03:50 am by thefatmoop Reason: 1
yeah i'm working with a similar program.... sending values to another arduino and setting pwm accordingly

Code: [Select]
#include <math.h>
#include "Wire.h"
#include "WiiChuck.h"
//#include "nunchuck_funcs.h"

#define MAXANGLE 90
#define MINANGLE -90


WiiChuck chuck = WiiChuck();
int angleStart, currentAngle;
int tillerStart = 0;
double angle;

void setup() {
 //nunchuck_init();
 Serial.begin(1200);
 chuck.begin();
 chuck.update();
 chuck.calibrateJoy();
}


void loop() {
 delay(500);
 chuck.update();

  // Serial.print(",jx ");  
   Serial.print((int)chuck.readJoyX()*-1+100);
  // Serial.print(",jy ");  
   Serial.println((int)chuck.readJoyY()*-1+100);
}


this program simply reads joystick x y on wii nunchuck controller, makes it positive, and adds 100 to each (so that it's always 6 characters long)

example output: 233230

I searched the forum for atoi and found this example program
Code: [Select]
#include <stdlib.h>     // needed for [shighlight]atoi[/shighlight]
char buffer[4];
int received;
int ledPin = 9;
void setup()
{
   Serial.begin(9600);
   received = 0;
   buffer[received] = '\0';
}
void loop()
{
   if (Serial.available())
   {
       buffer[received++] = Serial.read();
       buffer[received] = '\0';
       if (received >= (sizeof(buffer)-1))
       {
           Serial.print(buffer);
int myInt = [shighlight]atoi[/shighlight](buffer);
   analogWrite(ledPin, myInt);
           received = 0;
       }
   }
}   }
}


when i did syntax check i had error:
Code: [Select]
In function 'void loop()':
error: expected primary-expression before '[' token At global scope:
[/color]

i don't have the library #include <stdlib.h>? where would i find this?


Jassper

#6
Jan 13, 2009, 04:40 am Last Edit: Jan 13, 2009, 04:41 am by Jassper Reason: 1
It compiles,

The issue is this line here,
Code: [Select]

int myInt = [shighlight]atoi[/shighlight](buffer);


Note the [ shighlight] and the [ /shighlight] these are inserted into the code block to Highlight the word you are searching for, "atoi", Take that out and get rid of the 2 extra }} at the bottom and it should compile.

The lib should already be there.

thefatmoop

#7
Jan 13, 2009, 06:50 am Last Edit: Jan 13, 2009, 06:51 am by thefatmoop Reason: 1
that's good because i read it and thought "what is that???
i'll try again and see what happens

thefatmoop

could i get a pointer in the right direction?
i'm trying to send variables in one long serial message:
Code: [Select]
#include <math.h>
#include "Wire.h"
#include "WiiChuck.h"
//#include "nunchuck_funcs.h"

#define MAXANGLE 90
#define MINANGLE -90


WiiChuck chuck = WiiChuck();
int angleStart, currentAngle;
int tillerStart = 0;
double angle;

void setup() {
 //nunchuck_init();
 Serial.begin(57600);
 chuck.begin();
 chuck.update();
 chuck.calibrateJoy();
}


void loop() {
 delay(1000);
 chuck.update();


  //Serial.print((int)9);
  // Serial.print(",jx ");  
   Serial.print((int)chuck.readJoyX()*-1+100);
  // Serial.print(",jy ");  
   Serial.println((int)chuck.readJoyY()*-1+100);
}


this sends for example 222232 (222 = joyX, 232=joyY)

how would i isolate the variables and be able to work with them in the program below?

Code: [Select]
#include <stdlib.h>     // needed for [shighlight]atoi[/shighlight]
char buffer[9];
int received;

void setup()
{
   Serial.begin(57600);
   received = 0;
   buffer[received] = '\0';
   Serial.print("setup");
}
void loop()
{
   if (Serial.available())
   {
       buffer[received++] = Serial.read();
       buffer[received] = '\0';
       if (received >= (sizeof(buffer)-1))
       {
     int myInt = atoi(buffer);      
Serial.println(buffer);

Serial.print("1: ");
Serial.println(myInt/10);
Serial.print("2: ");
Serial.println(myInt/100);
Serial.print("3: ");
Serial.println(myInt%10);
Serial.print("4: ");
Serial.println(myInt%100);
received = 0;
 }
}
}

Jassper

#9
Jan 13, 2009, 12:42 pm Last Edit: Jan 13, 2009, 12:43 pm by Jassper Reason: 1
I only know enough to be dangerous, but I'll give it a go.

I assume you mean to send the data "222232" from one to the other?

Maybe something like this
Code: [Select]

#include <stdlib.h>    
char X_buffer[3];
char Y_buffer[3];
int received;
int i=0;
int X_pos=0;
int Y_pos=0;

void setup()
{
   Serial.begin(57600);
   received = 0;
   X_buffer[received] = '\0';
   Y_buffer[received] = '\0';
   Serial.print("setup");
}
void loop()
{
   if (Serial.available())
   {
     // Get x cord
       for(i=0; i<3; i++)
         {
         X_buffer[i] = Serial.read();
         }
       // Get y cord
       for(i=0; i<3; i++)
         {
         Y_buffer[i] = Serial.read();
         }
     X_pos = atoi(X_buffer);
     Y_pos = atoi(Y_buffer);
   }
/*    
Serial.println(buffer);
Serial.print("1: ");
Serial.println(myInt/10);
Serial.print("2: ");
Serial.println(myInt/100);
Serial.print("3: ");
Serial.println(myInt%10);
Serial.print("4: ");
Serial.println(myInt%100);
received = 0;

 }
}*/
}


Go Up