0

I am relatively new to both Arduino and Processing and I have been working on a code that utilizes serial communication between the two. My Arduino code is reading and printing values from a piezo sensor and then sending the values to Processing, which sketches certain shapes based on the values. The code has worked previously, but for some reason it is no longer working. Everything compiles, but when I run the Processing code the sketch window is empty and remains empty. A few times the "error, disabling serialEvent()" showed up, but I just unplugged my Arduino board, closed out the programs, and restarted everything. The error no longer shows up, but my Processing sketch is still not displaying on the screen. Can someone please let me know what is wrong with my code? I really appreciate the help.

Arduino Code:

int ledPin = 13;
int knockSensor = A0;
byte val = 0;         
int statePin = LOW;   
int THRESHOLD = 5;  
int sensorReading = 0;

void setup() {
 pinMode(ledPin, OUTPUT); 
 Serial.begin(9600);       
}

void loop() {
 sensorReading = analogRead(knockSensor);    

  if(sensorReading > 0){
   Serial.println(sensorReading, DEC);
   delay(100);
  }
}

Processing Code:

import processing.serial.*;

Serial port;

int centerX = 550;
int centerY = 400;

float val;
float ellipseX;
float ellipseY;
float ellipseW;
float ellipseH;
float ellipseXX;
float ellipseYY;
float ellipseWW;
float ellipseHH;
float lineX;
float lineY;
float lineXX;
float lineYY;

void setup(){
  background(255);
  size(1100,800); 
  frameRate(10);
  smooth();

  String portname = "/dev/tty.usbmodem1411"; 

  //String portname = Serial.list()[0];
  port = new Serial(this, portname, 9600);
  println(Serial.list());
  //port.bufferUntil('\n');
}

void drawEllipse(float val)
{
  if(val > 0 && val < 50)
  {
    ellipseX = random(540,560); 
    ellipseY = random(390,410);
    ellipseW = val + 10;
    ellipseH = val + 10;

    stroke(0);
    fill(random(255), random(200,255));
  }
}

void drawLines(float val)
{
  if(val > 50 && val < 70)
  {
    lineX = random(500, 600);
    lineY = random(360, 440);
    lineXX = random(500, 600);
    lineYY = random(360, 440);

    stroke(0);
  }
}

void drawEllipse2(float val)
{
  if(val > 70 && val < 120)
  {
    ellipseXX = random(460, 640); 
    ellipseYY = random(330, 470);
    ellipseWW = val + random(20);
    ellipseHH = val + 10;

    stroke(0);
    fill(random(50, 100), random(50, 100), random(50, 100), random(220, 255));
  }
}

void serialEvent(Serial port) 
{
  String inString = port.readStringUntil('\n');

  if (inString != null)
  {
      val = Float.parseFloat(inString);
  }

  drawEllipse(val);
  drawLines(val);
  drawEllipse2(val);
  println(val);
}
1
  • Nevermind, resolved! Commented Mar 14, 2015 at 6:41

2 Answers 2

1

It seems like you need to do some basic debugging:

  • Put a println() in setup. If this does not appear, you have a major problem talking to the Arduino board.
  • Put a println() statement in the loop() that prints out the sensor reading. That should separate hardware problems from software defects.
-1

Try using this code and run it on your Arduino

void setup() {
   Serial.begin(9600);
}

void loop() {

Serial.print('hello world');
}

After uploading the code open the serial monitor and see if it shows the string 'hello world' and make sure that the baud rate of the serial monitor is set on 9600. If nothing shows this could mean that there is something wrong with your Arduino board.

2
  • Serial.print('hello world'); is not correct. It should read: Serial.print("hello world"); and preferably: Serial.println("hello world"); Check out the difference between single and double quotes in C/C++.
    – Nick Gammon
    Commented Sep 10, 2015 at 20:42
  • oh sorry i wanted to use double quotes but i wrote it wrong :( @NickGammon Commented Oct 17, 2015 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.