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.
-
It is an open source software and can be downloaded from processing.org.
-
Processing is a great source for creating graphics. The Processing IDE works for a computer like the Arduino IDE works for a micro-controller. The Processing IDE is similar to Arduino in terms of structure.
-
It has setup functions and draw functions like an Arduino has a setup and loop function.
-
The Processing IDE can communicate with the Arduino IDE through serial communication. This way, we can send data from the Arduino to the Processing IDE and also from the Processing IDE to the Arduino.
Source : Maker.pro
Using Processing¶
Basic processing commands:
- setup() : Like the setup() function in arduino, runs only once.
- draw() : Updates the GUI, runs continuously like the kinda arduino loop() function.
- keyPressed() : Event initiated when a keystroke is detected.
- size(width,height) : Defines the dimension of the display window width and height in units of pixels.
- background(rgb) : The background() function sets the color used for the background of the Processing window.
- exit() : Quits/stops/exits the program. Programs without a draw() function stop automatically after the last line has run
Serial Library Functions:
- Serial(parent, portName, baudRate) : Initiates a serial port for communication. The sketch will freeze if port is not found. It can be avoided by wrapping the entire section in a try block.
- Serial.list() : Returns a string array of names of all the serial objects connected to the machine.
- serialEvent(Serial obj) : This event is triggered everytime a serial event occurs. It may be when serial data is received.
- serial.read() : Returns a number between 0 and 255 for the next byte that’s waiting in the buffer. Returns -1 if there is no byte, although this should be avoided by first cheacking available() to see if data is available.
- serial.write() : Writes bytes, chars, ints, bytes[], Strings to the serial port
- Pfont() : Used to create and set font for the text() function.
Reference : processing.org
Boards¶
The board I have used here is version 3 of the board I used during Input devices.
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 } }
- As you can see the screen displays the distance reading from the ultrasonic sensor connected to the board.
Video¶
This is the on-screen output
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