Processing + Arduino + Flexiforce

edited March 2016 in Arduino

Hi there,

I'm trying to import readings from a Flexiforce sensor however, the values in processing are different than the ones displayed in the Arduino serial monitor but they somehow respond to any force applied on the sensor.

Could anyone clarify what's going on?

Thanks in advance :)

Arduino Code:

void setup()
{
  // Start serial at 9600 baud
  Serial.begin(9600);
}

void loop()
{
  // Read the input on analog pin 0:
  int sensorValue = analogRead(A0);

  //Serial.println(sensorValue);
  Serial.write(sensorValue);

  // Wait 100 milliseconds
  delay(100);
}

Serial Monitor Value (150gr):

    330
    330
    330
    330
    330
    330
    331
    330
    330
    330
    330
    329
    330
    330
    330
    330
    330
    330
    330
    331

Processing Code:

import processing.serial.*; 

Serial port;                             // Create object from Serial class 
int val = 0;

void setup() { 
  size(200, 200); 
  frameRate(10); 
  // Open the port that the board is connected to and use the same speed (9600 bps) 
  printArray(Serial.list());
  port = new Serial(this, Serial.list()[3], 9600);
} 

void draw() { 

  if (0 < port.available()) {         // If data is available,
    val = port.read();     // read it and store it in val
  }

  background(255);                       // Set background to white
  println(val);
  float dia = map(val, 0, 300, height*0.1, height*0.9);
  fill(0);
  ellipse(width*0.5, height*0.5, dia, dia);
} 

Processing Println Values (150gr):

    88
    88
    87
    88
    88
    88
    88
    89
    88
    88
    88
    88
    88
    88
    87

Answers

  • edited March 2016

    Ok, so I'm now sending the values as string using Serial.println() which is giving me the correct values, however when printing them in Processing I get a return between each values which I believe is causing my String to Int conversion ( Integer.parseInt(in) ) to fail??

    I could change the Serial.println() to Serial.print() but as I understand it this allows me to correctly read to data?!

    import processing.serial.*; 
    
    Serial port;                             // Create object from Serial class 
    String val = "";
    
    void setup() { 
      size(200, 200); 
      frameRate(10); 
      // Open the port that the board is connected to and use the same speed (9600 bps) 
      printArray(Serial.list());
      port = new Serial(this, Serial.list()[3], 9600);
    } 
    
    void draw() { 
    
      if (0 < port.available()) {         // If data is available,
        val = port.readStringUntil('\n');
      }
      println(val);
    } 
    
  • edited March 2016

    From: https://forum.Processing.org/two/discussion/14534/myport-available-always-0

    /**
     * Efficient Serial Reading (v1.0)
     * GoToLoop (2016-Jan-19)
     * forum.Processing.org/two/discussion/14534/myport-available-always-0
     */
    
    import processing.serial.Serial;
    
    static final int PORT_IDX = 3, BAUDS = 9600;
    String myString;
    
    void setup() {
      noLoop();
      final String[] ports = Serial.list();
      println(ports);
      new Serial(this, ports[PORT_IDX], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      println(myString + " \t" + int(myString));
    }
    
    void serialEvent(final Serial s) {
      myString = s.readString().trim();
      redraw = true;
    }
    
  • edited March 2016

    Could anyone clarify what's going on?

    I don't have Arduino nor any functioning Serial port. It's merely an attempt guess. :@)

    @ Serial.write(sensorValue); you're sending out an int datatype value. That's 4 bytes (32 bits).

    @ Processing's side, val = port.read(); receives 1 byte only! It's a far cry from 4 bytes. b-(

    In order to get the original sent int value, you're gonna need to accumulate 4 byte read()s and only then assemble them as 1 int. #:-S

    I've adapted "Efficient Serial Reading" to use readBytes() & buffer() in place of readString() & bufferUntil() below.

    1. https://Processing.org/reference/libraries/serial/Serial_readBytes_.html
    2. https://Processing.org/reference/libraries/serial/Serial_buffer_.html
    3. https://Processing.org/reference/libraries/serial/Serial_readString_.html
    4. https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.html

    Again, I can't test the sketch for I don't have the hardware. Check it out for yourself: 8-X

    /**
     * Efficient Serial ReadBytes (v1.0)
     * GoToLoop (2016-Mar-04)
     * forum.Processing.org/two/discussion/15278/processing-arduino-flexiforce
     */
    
    import processing.serial.Serial;
    
    static final int PORT_IDX = 3, BAUDS = 9600;
    final byte[] receivedInt = new byte[Integer.BYTES];
    int actualInt;
    
    void setup() {
      noLoop();
      final String[] ports = Serial.list();
      printArray(ports);
      new Serial(this, ports[PORT_IDX], BAUDS).buffer(Integer.BYTES);
    }
    
    void draw() {
      println(actualInt);
    }
    
    void serialEvent(final Serial s) {
      s.readBytes(receivedInt);
      actualInt = bytesToInt(receivedInt);
      redraw = true;
    }
    
    static final int bytesToInt(final byte... b) { // Big-Endian
      return b[0]<<030 | b[1]<<020 | b[2]<<010 | b[3];
    }
    
  • Answer ✓

    i think Serial.write Arduino sends only 1 byte (0 to 255) for values greater use is made of "lowbyte()" and "highbyte()" (two bytes).

     //Serial.println(sensorValue);
       Serial.write(lowByte(sensorValue));
       Serial.write(highByte(sensorValue));
      // Wait 100 milliseconds
      delay(100);
    

    processing similar to :

      if (0 < port.available()) {         // If data is available,
       //val    =  (port.read() & 0xff)  |   ((port.read()) << 8);
        val  = port.read(); // read the least significant byte
        val  = port.read() * 256 + val ; // add the most significant byte
      }
    
Sign In or Register to comment.