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);
  }

  if (sensorReading != 0)
    Serial.println(sensorReading);
  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);
}
4
  • 1
    You're better off asking on arduino.stackexchange.com Commented Mar 13, 2015 at 23:34
  • Do you know that in your Arduino sketch, you are doing Serial.println twice for each sensor read? I don't know if that's the issue, but was is that what you intended? (Both look like they're doing basically the same thing). Commented Mar 14, 2015 at 1:49
  • Oh thanks for pointing that out, but I don't think that's the issue. Commented Mar 14, 2015 at 5:06
  • Nevermind, resolved! Commented Mar 14, 2015 at 6:41

1 Answer 1

0

Maybe using Serial.write() will be better. So the code will look like this.

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);
    }

    if (sensorReading != 0)
        Serial.write(map(sensorReading, 0, 1023, 0, 255));
    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) 
{
    if (0 < port.available()) {
        val = map(port.read(), 0, 255, 0, 1023);
    }

    if (val > 0) {
        drawEllipse(val);
        drawLines(val);
        drawEllipse2(val);
        println(val);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.