16. Interface and application programming

For Interfacing and Application, I decided to go to go with processing.

Processing

Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology. There are tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning and prototyping.

Source : Maker.pro

Using Processing

Basic processing commands:

Serial Library Functions:

Reference : processing.org

Boards

The board I have used here is version 3 of the board I used during Input devices.

n&c

Objective

The objective for this week is to make an interface that shows the distance reading from an ultrasonic sensor in form of a sliding bar. Thus when an object is placed at 20 cm from ultrasonic sensor, the screen will show this distance with a specific location in the sliding bar. If the object is moved away, to say 50 cm, the screen will show 50 cm and the slider will move to its right. When the distance decreases the slider moves to its left.

Arduino Code

#define trigger A2
#define echo A3
long timeNeeded;
float distance;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
    // wait for serial port to connect. Needed for native USB port only
    pinMode(trigger,OUTPUT); 
    pinMode(echo,INPUT); 
}

void loop() { // run over and over
      digitalWrite(trigger,LOW); 
      delayMicroseconds(2);
      digitalWrite(trigger, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigger, LOW);
      timeNeeded = pulseIn(echo, HIGH);
      distance= timeNeeded * 0.034/2;
      Serial.println(distance);
      delay(1000);               
}

Processing Code

import processing.serial.*;

Serial commPort;        // The serial por       // horizontal position of the graph
float distance = 0;
 String distance_measured;

void setup () {
    // set the window size:
    size(500, 100);
    //set the port to 4 cause that's the arduino port
    //set the baud rate to 9600
    commPort = new Serial(this, "com4", 9600);

    // don't generate a serialEvent() unless you get a newline character:
    commPort.bufferUntil('\n');
}

void draw () {
   background(0);   //color the backgound to black
   stroke(204, 102, 0);      //set the outline of the rectagle 
   rect (10, 49,480, 22);    //draw a rectangle with these dimensions and x,y coordinates 

    //draw triangle pointer
    stroke(204, 102, 0);    //set the outline of the rectagle
    rect(10,49,distance,22);  // draw another rectangle but with height differing depending on the distance measured
    textAlign(CENTER);
    text("Distance="+ distance_measured, 250, 25);  // write the distance measured   
}

void serialEvent (Serial commPort) {
    // get the ASCII string:
    distance_measured = commPort.readStringUntil('\n');

    if (distance_measured != null) {
      // trim off any whitespace:
      distance_measured = trim(distance_measured);
      // convert to an int and map to the screen height:
      distance = float(distance_measured);
      println(distance);
      distance = map(distance, 0, 5000, 15, 480); // map the distance measured depending on the size of the rectangle I drew 
    }
}

Video

This is the on-screen output

interfacing

All the files for this week are attached here

Original board files are attached here

Group Work

The Group Assignment was to compare as many tools as possible. The group page can be found here