Skip to content

PComp Lab: Multiple Serial Output

December 17, 2011


Admittedly, the Arduino code for this is pretty much an example sketch I came across for this sensor with a few tweaks:

// Touch screen library with X Y and Z (pressure) readings as well
// as oversampling to avoid 'bouncing'
// This demo code returns raw readings, public domain

#include <stdint.h>
#include "TouchScreen.h"

#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 8   // can be a digital pin
#define XP 9   // can be a digital pin

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

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

void loop(void) {
  // a point object holds x y and z coordinates
  Point p = ts.getPoint();
  
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
  if (p.z > ts.pressureThreshhold) {
     Serial.print("X"); Serial.print(p.x);
     Serial.print("Y"); Serial.print(p.y);
     Serial.print("P"); Serial.println(p.z);
  }

  delay(0);
}

Most of the hard work is done on the processing side to “unpack” the data and take the appropriate action:

float offset = 2.23;
float rot = 0.0;
import processing.serial.*;
int yPos =0;
int xPos = 0;

boolean colorRed = false;
boolean colorGreen = false;
boolean colorBlue = false;

Serial myPort;        // The serial port


void setup() {
  smooth();
  size(1200, 900);
  background(255);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);
  myPort.bufferUntil('\n');
}


void draw() {
}
void serialEvent (Serial myPort) {

  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    int xCharPos = inString.indexOf("X");
    int yCharPos = inString.indexOf("Y");
    int pCharPos = inString.indexOf("P");

    String xString = inString.substring(xCharPos + 1, yCharPos);
    String yString = inString.substring(yCharPos + 1, pCharPos);
    String pString = inString.substring(pCharPos + 1, inString.length());

    println(xString + "\t" + yString + "\t" + pString);

    xPos = int(map((Integer.parseInt(xString)), 90, 950, 0, width));
    yPos = int(map((Integer.parseInt(yString)), 920, 120, 0, height));
    int ellipseSize = max(int(map((Integer.parseInt(pString)), 300, 150, 1, 200)), 1) ;
    noStroke();
    fill(40, 40, 40, 20);
    if (colorRed)
      fill(255, 0, 0, 20);
    if (colorGreen)
      fill(0, 255, 0, 20);
    if (colorBlue)
      fill(0, 0, 255, 20);
    ellipse(xPos, yPos, ellipseSize, ellipseSize);
  }
}

void keyPressed() {
  if (key == 'r' || key == 'R') {
    colorRed =! colorRed;
    colorGreen = false;
    colorBlue = false;
  } 
  if (key == 'g' || key == 'G') {
    colorGreen =! colorGreen;
    colorRed = false;
    colorBlue = false;
  } 
  if (key == 'b' || key == 'B') {
    colorBlue =! colorBlue;
    colorGreen = false;
    colorRed = false;
  }
}

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: