0

I have a contraption that uses a HC-05 Bluetooth module and sends info to a processing program which controls the contraption.

The processing program is a follows (from a video by How To Mechatronics, slightly modified):

import processing.serial.*;
Serial myPort;
String ledStatus = "LED: OFF";

long prevMillis = 0;

void setup() {
  size(450, 500);
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}

void serialEvent (Serial myPort) {
 ledStatus = myPort.readStringUntil('\n');
}

void draw() {
  background(255);
  fill(20, 160, 133);
  stroke(33);
  strokeWeight(1);
  rect(50, 100, 150, 50, 10);
  rect(250, 100, 150, 50, 10);

  fill(100);
  textSize(32);
  text("Turn On", 60, 135);
  text("Turn Off", 255, 135);

  fill(99);
  text("Status:", 180, 200);

  text(ledStatus, 155, 240);

  // Turn On is pressed
  if (mousePressed && mouseX > 50 && mouseX < 200 && mouseY > 100 & mouseY < 150 && millis() - prevMillis >= 1000) {
    prevMillis = millis();
    myPort.write('1');
    stroke(255, 0, 0);
    strokeWeight(2);
    noFill();
    rect(50, 100, 150, 50, 10);
  }

  // Turn Off is pressed
  if (mousePressed && mouseX > 250 && mouseX < 400 && mouseY > 100 && mouseY < 150 && millis() - prevMillis >= 1000) {
    prevMillis = millis();
    myPort.write('0');
    stroke(255, 0, 0);
    strokeWeight(2);
    noFill();
    rect(250, 100, 150, 50, 10);
  }


}

My arduino program is (from same video):

const int ledPin = 7;
int ledState = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    ledState = Serial.read();
  }

  // Okay to say  '0' because it will be assigned to a value from ASCII
  if (ledState == '0') {
    digitalWrite(ledPin, LOW);
    Serial.println("LED: OFF");
    ledState = 0; // Makes sure that the code doesn't run again (ledState will be value and it will keep being the value meaning the code keeps rerunning)
  } 
  // Same thing here
  else if (ledState == '1') {
    digitalWrite(ledPin, HIGH);
    Serial.println("LED: ON");
    ledState = 0;
  }
}

I have everything wired correctly and the LED turns on and off upon clicking the on and off button, but the status that comes in is always messed up... Here are some screenshots:

Picture

Picture

What is the problem and how can I fix it? Thanks!

  • I've tried your code and it works properly (Processing version 1.5.1 with Arduino IDE version 1.0.6.2). Can you try adding myPort.clear(); to the serialEvent just below this line ledStatus = myPort.readStringUntil('\n');. Another diagnostic "tool" would be to use print(ledStatus); to print the "raw value" to the console window in the Processing IDE. When I use the print statement in the "mousePressed" if / if else, I get "the opposite" text displayed. Weird, but the GUI window and LED on my Uno works perfectly. – VE7JRO Jun 22 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.