Week 13 : Interface and application programming


For other developing environments check:
group page


Download files

Visualizing capacitive sensor value

Remote control LED from Processing

Visualizing capacitive sensor value

Goal

To visualize sensitive value from step response sensor I made on week11 using Processing.

Sensor module > Processing

Neil’s capacitive sensor board

Sparkfun Tutorial : Connecting Arduino to Processing

I struggled to make processing program to successfully receive the serial that is sent out from Neil’s sample board.

I found the document from former Fabacademy student Tiago that does exactly the same thing as I wanted to do.

Communicate with Neil’s Capacitive sensor board using serial

Tiago’s code

(Serial communication relevant part abstracted. Look source for the full code.)

void draw()
{
  while (myPort.available() > 0) {    // If data is available
    byte1 = byte2;
    byte2 = byte3;
    byte3 = byte4;
    byte4 = low;
    low = high;
    high = myPort.read();

    if ((byte1 == 1) & (byte2 == 2) & (byte3 == 
3) & (byte4 == 4)){                // Filter out the framing numbers: 1,2,3,4
       value = (256*high + low)/100;
       filt = (1-eps)*filt + eps*value;

       println("THE VALUE IS " + value); //print to the screen
    }
    //EXPERIMENT WITH THE VISUALIZATION BELOW
    background(322);             // Set background to white
    int scale = 10;
    rect (20,20,value,20);
  }
}

Rewrite

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
int sensorData; // Data received from the serial port with 1,2,3,4 framing numbers filtered out
int low=0;
int med=0;
int high=0;
int value=0;
int byte1 = 0;
int byte2 = 0;
int byte3 = 0;
int byte4 = 0;
float filt;
float eps = 0.75;
float s = 0.0;

void setup()
{
  size(640, 360, P3D);
  noStroke();
  colorMode(RGB, 1);
  fill(0.4);
  println(Serial.list());
  myPort = new Serial(this, "/dev/tty.usbserial-FTGNZXMP", 9600);

}

void draw()
{
  while (myPort.available() > 0) {    // If data is available
    byte1 = byte2;
    byte2 = byte3;
    byte3 = byte4;
    byte4 = low;
    low = high;
    high = myPort.read();

    if ((byte1 == 1) & (byte2 == 2) & (byte3 == 
3) & (byte4 == 4)){                // Filter out the framing numbers: 1,2,3,4
       value = (256*high + low)/100;
       filt = (1-eps)*filt + eps*value;
       
       s = float(value);
       /*
       s = s -500;
       s = s * 8 / 1000;
       if(s < 0){
       s = -s;
       }
       */
       s = map(s, 510.0, 550.0, 0.0, 1.0);
       if(s < 0){
       s = -s;
       }
}
  }
    //EXPERIMENT WITH THE VISUALIZATION BELOW
    
  background(0);
  translate(width / 2, height / 2);
  // Set the specular color of lights that follow
  lightSpecular(1, 1, 1);
  directionalLight(0.8, 0.8, 0.8, 0, 0, -1);
  specular(s, s, s);
  sphere(150);
  println(s); //print to the screen
}

Result

Worked at last.

Source

Remote control LED from processing

Capacitive sensor > Processing > ESP8266 (LED blink)

Send out data from sensor board to processing via serial as described above.
Then, I made ESP8266 work as a http server and sent http request to ESP8266 from processing that triggers LED pin high/low.

Result

Worked.

Source


Processing Tips

processing.serial

https://processing.org/reference/libraries/serial/Serial.html

Light commands

ref

Functions for calculation

map(value, start1, stop1, start2, stop2)
println(map(2, 0, 10, 100, 200)); // gives 120.

Replacing the code A to code B.

CodeA

s = s -500;
s = s * 8 / 1000;
if(s < 0){
s = -s;
 }

CodeB

s = map(s, 510.0, 550.0, 0.0, 1.0);
if(s < 0){
s = -s;
}

ref