For my robot, I am trying to merge two Processing programs(a graphics example(tutorial) and a radar program) to create a display, without success. The robot sends information for the Processing program to display. I want to use the end of the line as the end of data package character, but have had no success in finding the character I am looking for. I want each data packet to be on a single line so that I can read it.
The first(Second) draft of the robot program is as follows:
This post edited to incorporate comments.
#include <MeMegaPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
MeUltrasonicSensor ultraSensor(PORT_7); /* Ultrasonic module can ONLY be connected to port 3, 4, 6, 7, 8 of base shield. */
int Angle;
int Distance;
void setup() {
Serial.begin(115200);
MeGyro gyro;
}
void loop() {
int i;
for (i = 0; i < 360; i++) {
gyro.update();
Angle = gyro.getAngleZ();
Serial.print (Angle); // Sends the current angle into the Serial Port
Serial.print (","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexingIDEfor indexing
Distance = ultraSensor.distanceCm();
Serial.print (Distance); // Sends the distance value into the Serial Port
Serial.println("");
delay (100);
}
}
The Processing program code follows:
import processing.serial.*; // imports library for serial communication
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;
import toxi.color.*;
import toxi.geom.*;
import toxi.math.*;
// keeper of transition state
float transition;
// use a S-Curve to achieve an ease in/out effect
InterpolateStrategy is=new SigmoidInterpolation(3);
Serial myPort; // defines Object Serial
// defubes variables
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance, w2, h2;
int iAngle = 3/4*3, iDistance =300 ;
int index1=0;
int index2=0;
PFont orcFont;
void setup() {
float h2=height * 0.5;
// size(800, 800);
background(255);
smooth();
noStroke();
size (800, 800,P3D); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION***
smooth();
myPort = new Serial(this,"COM5", 115200); // starts the serial communication
myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance.
fill(98,245,31);
// background(255);
noStroke();
fill(0,4);
fill(98,245,31); // green color
translate(w2, h2);
}
void draw() {
if (frameCount % 1 == 0) {
fill(frameCount * 3 % 255, frameCount * 5 % 255,frameCount * 7 % 255);
pushMatrix();
translate(400, 400);
rotate(radians(iAngle % 360) );
rect(0, 0, 400, 1);
box(50.0,100.0,float(angle));
popMatrix();
// print(angle);
}
}
void serialEvent (Serial myPort) { // starts reading data from the Serial Port
// reads the data from the Serial Port up to the end of the line and puts it into the String variable "data".
data = myPort.readStringUntil('CR/LF'); // Is it LF(No), CR(No), EOL(No) OR 'LF', 'CR', 'EOL'(NO), \n(No)
data = data.substring(0,data.length()-1);
index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance
// converts the String variables into Integer
iAngle = int(angle);
iDistance = int(distance);
}