Can't use serial data coming from Arduino!
in
Integration and Hardware
•
28 days ago
Hi everyone! I have a major problem! I am trying to use an if statement to do something when the serial data (in the form of a string) equals "received". But whatever I do, the if statement wont respond to ANY change in the serial data. I can send things to the arduino just fine, but receiving is the big problem. Also any programming tips are helpful, I'm a noob.
Thanks to anyone who tries to help me! I don't care if you solve the problem or not, just that you try to help means a lot to me :D
Arduino code:
int DataIn = 0; // data coming from computer
int ComStat = 0;
void setup() {
Serial.begin(19200); // I picked 19200bps (not the problem, I tried 9600)
}
void loop() {
if (Serial.read() == 73) {
ComStat = 1;
}
if (Serial.read() == 48) {
ComStat = 0;
}
while (ComStat == 1) { // The computer won't pick it up at all without it constantly sending data
Serial.println("received"); // send "received" to the computer
delay(100);
}
}
int ComStat = 0;
void setup() {
Serial.begin(19200); // I picked 19200bps (not the problem, I tried 9600)
}
void loop() {
if (Serial.read() == 73) {
ComStat = 1;
}
if (Serial.read() == 48) {
ComStat = 0;
}
while (ComStat == 1) { // The computer won't pick it up at all without it constantly sending data
Serial.println("received"); // send "received" to the computer
delay(100);
}
}
(I know I'm not very efficient, I need all the help I can get!)
Now here is probably the worst written code in the history of processing!
Processing code:
import processing.serial.*;
Serial Arduino; // named the port "Arduino"
String ConStat = "Connect";
String DataIn; // global variable for data coming from arduino
String GoodIn; // global variable for processed data (arduino data processed)
void setup() {
Arduino = new Serial(this, "COM3", 19200); // set up port on COM3
Arduino.bufferUntil(10);
size(1000, 600);
background(225);
}
String ConStat = "Connect";
String DataIn; // global variable for data coming from arduino
String GoodIn; // global variable for processed data (arduino data processed)
void setup() {
Arduino = new Serial(this, "COM3", 19200); // set up port on COM3
Arduino.bufferUntil(10);
size(1000, 600);
background(225);
}
void draw() {
fill(130);
rect(50, 40, 80, 40, 7);
fill(255);
textSize(16);
text(ConStat, 59, 67);
println(GoodIn); // this is not necessary, just for debugging (doesn't help much)
if (mousePressed == true && mouseX >= 50 && mouseX <= 130 && mouseY >= 40 && mouseY <= 80) {
fill(50);
rect(50, 40, 80, 40, 7);
Arduino.write(73); //Send capital letter I to arduino.
ConStat = "Found"; // indicates that there is something connected to COM3
if (GoodIn == "received") { // PROBLEM HERE!!! Even though the monitor says that it is = "received", it never changes the outcome of this conditional!
ConStat = "Active";
println("Active");
}
}
}
void serialEvent(Serial Arduino) {
// this is where the data processing occurs
DataIn = Arduino.readStringUntil('\n');
if (DataIn != null) {
DataIn=trim(DataIn);
GoodIn = DataIn;
}
}
if (DataIn != null) {
DataIn=trim(DataIn);
GoodIn = DataIn;
}
}
PLEASE REPLY IF YOU SEE ANYTHING WRONG WITH THIS!!!
1