strange problem with serial communication maybe
in
Core Library Questions
•
3 years ago
Hello,
I'm trying to write a simple little program that interacts with an Arduino. Basically you step on 3 different Force Sensitive Resistors. Each FSR is assigned to a different picture on the computer monitor that Processing gives you. When I write this program with simple key presses (i.e - press the key '1', it gives you this picture, press the key '2', that picture) it works no problem.
However, when I have it communicate with the Arduino I can only get the last picture in my if statements to load but the print() statements all work and give me the value of the FSR.
And just in case here is a simple version of the Arduino code:
I'm trying to write a simple little program that interacts with an Arduino. Basically you step on 3 different Force Sensitive Resistors. Each FSR is assigned to a different picture on the computer monitor that Processing gives you. When I write this program with simple key presses (i.e - press the key '1', it gives you this picture, press the key '2', that picture) it works no problem.
However, when I have it communicate with the Arduino I can only get the last picture in my if statements to load but the print() statements all work and give me the value of the FSR.
- import processing.serial.*;
PImage[] imgs = new PImage[4];
int FSR1;
int FSR2;
int FSR3;
Serial myPort;
void setup() {
size (1200,1000);
String[] mypicnames = {"dancing_mam1.png","dancing_mam2.png","dancing_mam3.png","dancing_mam4.png"};
for (int i = 0; i < mypicnames.length; i++) {
imgs[i] = loadImage(mypicnames[i]);
}
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil('\n');
}
void draw() {
if (FSR1 > 1) {
background(255);
image(imgs[0], 400, 100); ///here the image does not load
print(FSR1); ///but here it prints out this value
print(",");
}
if (FSR2 > 1) {
background(255);
image(imgs[1], 400,100); //here the image does not load
print(FSR2); //but it prints out this value
print(",");
}
if (FSR3 >1) {
background(255);
image(imgs[2], 400,100); //here the image DOES load AND the value prints out
print(FSR3); //if I comment out this code, than the second if statement works.
print(",");
}
else {
background(255); //this code works no problem
image(imgs[3], 400, 100);
}
}
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
int[] FSRs = int(split(inString, ","));
if (FSRs.length >=3) {
FSR1 = FSRs[0];
FSR2 = FSRs[1];
FSR3 = FSRs[2];
}
}
}
And just in case here is a simple version of the Arduino code:
- int FSR1 = 0;
int FSR2 = 1;
int FSR3 = 2;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(analogRead(FSR1));
Serial.print(",");
Serial.print(analogRead(FSR2));
Serial.print(",");
Serial.print(analogRead(FSR3));
Serial.print(",");
Serial.println("");
delay(100);
}
1