here is the updated code I am working with, simplifying it even more--I took out the bit about parsing it, to see if it was a data type issue. I still need to convert string to a c style string (completely new concept to me, so its a lot to take in at the moment)
arduino
//SERVO stuff
#include <Servo.h>
Servo myservo; // create servo object, max 8 servo objects
//IR portion
int IRpin = 1;
//int IRpin2 = 2;
// incoming serial data from processing
String serialDataIn;
String data[3];
int counter,
inbyte,
newPosX;
void setup() {
Serial.begin(9600);
myservo.attach(3); // server is on pin 3
counter = 0;
int pos = 0;
}
void loop() {
//float distance = analogRead(IRpin);
int x = analogRead(IRpin);
int y = 20;
Serial.print("Data,");
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",\n");
// SERVO stuffs
//int incomingByte = 0;
if( x > 0 && x < 180){
//Serial.write("passed");
if (Serial.available() > 0) {
Serial.write("serial avail");
inbyte = Serial.read();
/*
if(inbyte >= '0' && inbyte <= '9')
serialDataIn += inbyte;
if (inbyte == ','){ // Handle delimiter
data[counter] = String(serialDataIn);
serialDataIn = String("");
counter = counter + 1;
}
if(inbyte == '\r'){ // end of line
//myservo.write(x);
}
*/
// say what you got:
newPosX = (int) inbyte;
//Serial.print("I received: ");
//Serial.println(incomingByte, DEC);
myservo.write(inbyte);
}
delay(25);
}
}
everything seems to come over fine (if I print "data array") except the end of the line I get "-65", which might be the line break...? One weird thing I am noticing is I have no idea how I am getting an array...I don't push and values at any point, does the serial connection compile an array every time something is printed, then send it at end of loop? or is that what bufferUntil does? sorry this serial stuff is really new to me still...Starting to realize there are a lot of gaps in my understanding of how it works.
data :[0] "Data"
[1] "328"
[2] "20"
-65
data :[0] "Data"
[1] "328"
[2] "20"
-65
data :[0] "Data"
[1] "303"
[2] "20"
-65
Processing:
import java.awt.AWTException;
import java.awt.Robot;
import processing.serial.*;
import java.awt.MouseInfo;
import java.awt.event.InputEvent;
import java.awt.PointerInfo.*;
Serial myPort; // Create object from Serial class
int windowHeight = 500;
int windowWidth = 500;
int lastVal = 1000;
int currentVal,
posX,
meetsThreshold;
float posXf, mappedPosXf;
void setup() {
myPort = new Serial(this, Serial.list()[4], 9600);
myPort.bufferUntil('\n');
size(windowWidth, windowHeight);
}
void draw(){
}
void serialEvent(Serial p) {
String message = myPort.readStringUntil('\n'); // read serial data
if(message != null) {
String [] data = splitTokens(message,",\n"); // Split the comma-separated message
if (data[0].equals("Data")){
posX = Integer.parseInt(data[1]);
print("X:"); println(posX);
if( data.length > 2){
posXf = float(posX);
mappedPosXf = map(posXf, 150, 740, 0, 180);
posX = int(mappedPosXf);
println(posX);
myPort.write(posX);
}
}
}
}
int downSample(int handSensor) {
if (handSensor % 3 == 0){
int meetsThreshold = 1;
println("meets threshold");
} else if (handSensor % 3 != 0){
meetsThreshold = 0;
println("doesnt meet threshold");
}
return meetsThreshold;
}
cleaning it up a bit, I realized that I still really have no idea how to monitor serial connection when I am sending and receiving to arduino. What is the most viable way of doing this? I cannot open serial monitor because it sends two sets of data...I could debug easier if I could monitor it