We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there,
I'm trying to import readings from a Flexiforce sensor however, the values in processing are different than the ones displayed in the Arduino serial monitor but they somehow respond to any force applied on the sensor.
Could anyone clarify what's going on?
Thanks in advance :)
Arduino Code:
void setup()
{
// Start serial at 9600 baud
Serial.begin(9600);
}
void loop()
{
// Read the input on analog pin 0:
int sensorValue = analogRead(A0);
//Serial.println(sensorValue);
Serial.write(sensorValue);
// Wait 100 milliseconds
delay(100);
}
Serial Monitor Value (150gr):
330
330
330
330
330
330
331
330
330
330
330
329
330
330
330
330
330
330
330
331
Processing Code:
import processing.serial.*;
Serial port; // Create object from Serial class
int val = 0;
void setup() {
size(200, 200);
frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
printArray(Serial.list());
port = new Serial(this, Serial.list()[3], 9600);
}
void draw() {
if (0 < port.available()) { // If data is available,
val = port.read(); // read it and store it in val
}
background(255); // Set background to white
println(val);
float dia = map(val, 0, 300, height*0.1, height*0.9);
fill(0);
ellipse(width*0.5, height*0.5, dia, dia);
}
Processing Println Values (150gr):
88
88
87
88
88
88
88
89
88
88
88
88
88
88
87
Answers
Ok, so I'm now sending the values as string using Serial.println() which is giving me the correct values, however when printing them in Processing I get a return between each values which I believe is causing my String to Int conversion ( Integer.parseInt(in) ) to fail??
I could change the Serial.println() to Serial.print() but as I understand it this allows me to correctly read to data?!
From: https://forum.Processing.org/two/discussion/14534/myport-available-always-0
I don't have Arduino nor any functioning Serial port. It's merely an attempt guess. :@)
@
Serial.write(sensorValue);
you're sending out anint
datatype value. That's 4 bytes (32 bits).@ Processing's side,
val = port.read();
receives 1 byte only! It's a far cry from 4 bytes. b-(In order to get the original sent
int
value, you're gonna need to accumulate 4 byte read()s and only then assemble them as 1int
. #:-SI've adapted "Efficient Serial Reading" to use readBytes() & buffer() in place of readString() & bufferUntil() below.
Again, I can't test the sketch for I don't have the hardware. Check it out for yourself: 8-X
i think Serial.write Arduino sends only 1 byte (0 to 255) for values greater use is made of "lowbyte()" and "highbyte()" (two bytes).
processing similar to :