Dear forum members,
I'm pretty new to arduino programming. I did a few obstacle avoiding robots in the past and would like to build a drawbot that sends and receives coordinates over serial. So I am building a machine that can move within a square and send its coordinates and put a dot wherever it wants. But before that's finished, I would like to check if the arduino can actually send and receive data. I have a working sketch in Processing and i am trying to write a sketch for the arduino.
For this I use a piece of code that I got from here.
And I believe it should work. However, it doesn't compile and I get the error message
'class String' has no member named 'parseInt'
So I think the IDE says that parseInt doesn't belong to a string. But according to the reference it should!
So does anybody have an idea on how to solve this?
I will post both my both my arduino and processing sketches down here.
So here's the arduino code (that doesn't compile)
unsigned int xPos ; //this integer sets the X position of the robot
unsigned int yPos ; //this integer sets the Y position of the robot
unsigned int xGoal ; //this integer sets the X location of the pixel where the mothership thinks the robot is. If the Robot is in position, it can put a dot.
unsigned int yGoal ; //this integer sets the X location of the pixel where the mothership thinks the robot is. If the Robot is in position, it can put a dot.
unsigned int GreyValue ;
char val;
int CanvasWidth = 182;
int CanvasHeight = 263;
String received ;
void setup() {
Serial.begin(9600); //Serial communication, we are using it
goHome(); //move to the 0,0 position.
}
void loop() {
Serial.print(xPos);
Serial.print(",");
Serial.println(yPos);
// The mothership (The Processing sketch on the pc) will receive this data
// and look up the corresponding pixel in a bitmap.
// Then it will send that data back in de form of "xxx,yyy,grevvalue,L",
// where L is our termination char of choice.
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
if( val != 'L' ) { // if not an 'L'
received+=val; // add it to the received string
}
else {
// if 'L' was received (our designated termination char)
int XGoal = received.parseInt(); //the first series of characters before a ","
int YGoal = received.parseInt(); //the next series of characters before a ","
int GreyValue = received.parseInt(); //the final series of characters before a "L"
String received = ""; // Flush the string so we can receive new data
}}
else {
delay (100); // wait a bit and try again
}
}
if (xGoal == xPos) {
if (yGoal == yPos){
MoveOn();
}}
void MoveOn() //this subroutine tells the robot how to move to the next position.
{
if (xPos != CanvasWidth) { //if we are not at the end of the x axis
xPos = xPos +1; //and adjust x position by increasing by one
}
else if (yPos != CanvasHeight) { //if we are not at the end of the y axis
xPos = xPos - CanvasWidth; //and adjust x position to its starting value
yPos = yPos+1; //and adjust y position by increasing by one
}
else {
delay(1) //Do nothing. This will make the robot print the last pixel over and over again.
}}
void goHome() //this subroutine tells the robot how to move to position 0,0
{
while (xPos != 0) { //if we are not x position 0
xPos = xPos -1; //and adjust x position by increasing by one
//this will repeat until we are at position x=0
}
while (yPos != 0) { //if we are not x position 0
yPos = yPos -1; //and adjust x position by increasing by one
}}
And for the sake of completeness, my processing sketch:
import processing.serial.*; //import the Serial library
Serial myPort; //the Serial port object
String strIn = ""; //Declare a string with name strIn, this will store the coordinates received from the arduino
String strOut = ""; //Declare a string with name strOut, this will store the coordinates and corrseponding geryvalue of a pixel in the bitmap.
String portName = ""; //apparently this is necessary
String coordinates;
int locX= 0;
int locY= 0;
PImage img;
void setup()
{
size(182,262); //This is the size of the canvas and the range of the printer //<>//
// Make a new instance of a PImage by loading an image file
img = loadImage("parel.jpg");
tint(255,100);
image (img,0,0);
String portName = Serial.list()[0];
myPort = new Serial(this, portName,9600);
} //<>//
void draw()
{
while (myPort.available() > 0) { // If data is available,
strIn = myPort.readStringUntil('\n'); // read it and store it in a string called strIn
}
String[] coordinates = split(strIn, ","); //<>//
//This should cut the strIn string in two integers, separated by a comma
//and called coordinates. We expect a message coming in something like "XXX YYY"
int locX = int(trim(coordinates[0])); //This says that the first integer is an integer called LocX
int locY = int(trim(coordinates[1])); //This says that the first integer is an integer called LocX
color pix = img.get(locX, locY);
stroke(pix);
point(locX, locY);
strOut = locX + "," + locY + "," + pix + "L" + "\n"; //here all the needed values are integrated into the string strOut
myPort.write(strOut); //which we send over the serial port to the arduino to parse
}