Exercise, Week 12 - Interface and Application Programming

 

This is week topic is Interface and Application Programming.

 

This is a group assignment done with Ting Kok Eng and Noel Kristian. We have to compare as many tool options as possible. Our collective work is documented on the SP Fablab Website Assignment 9 and hence only my learning and reflections are documented here. In group assignment, I had created an application using Processing to display a radar map and a warning message. The script will also create an ellipse to follow the movement of the mouse on the screen. 

 

 

For Individual Assignment

Our Fablab is currently closed because of COVID19, and I had to try doing my assignment using available resources and work at home. This week I had created an application using Processing Development Environment with Arduino UNO, a servo motor and an Ultrasonic Ranging Module HC-SR04C to display the detected objects.

 

 

Audrino UNO with Devices Used

I had used an Audrino UNO connected with a servo motor to turn an Ultrasonic Ranging

Module HC-SR04C back and forth to detect objects from 15 to 165 degrees (1).

 

 

Program Code for Arduino UNO

 

Description of Program Code for Arduino UNO

1. In the function setup(), serial communication is established between my computer and an Arduino UNO "Serial.begin(9600)". Initialized Ultrasonic Ranging Module Trigger pin as an output “pinMode(trigPin, OUTPUT)” and Echo pin as an input “pinMode(echoPin, INPUT”. And define the servo motor attach to Arduino UNO digital pin 12 “myServo.attach(12)”.

2. In the function loop(), using for loop “for(int i=15;i<=165;i++)” to rotates the servo motor from 15 to 165 degrees "myServo.write(i)", than calls a function calculateDistance() to calculate the distance measured by the Ultrasonic Ranging Module for each degree turned.

3. Sends the current degree into the Serial Port “Serial.print(i)”.

4. Sends addition character “Serial.print(",")” right next to the previous value needed later in the Processing IDE for indexing.

5. Sends the distance value into the Serial Port “Serial.print(distance)”.

6. Sends addition character “Serial.print(".") right next to the previous value needed later in the Processing IDE for indexing.

7. Then, repeats the previous instruction (point 2 to 6) in a for loop “for(int i=165;i>15;i--)” to rotates the servo motor from 165 to 15 degrees.

8. In the function calculateDistance(), set Trigger pin low “digitalWrite(trigPin, LOW)” for 2 microsecond “delayMicroseconds(2)”, then set Trigger pin high for 10 microsecond “delayMicroseconds(10)” to generate the Ultra sound wave which is the signal for sensor to measure distance.

9. Set Trigger pin low "digitalWrite(trigPin, LOW)" and then reads the echo pin “duration=pulseIn(echoPin, HIGH)” for the sound wave travel time in microseconds.

10. Lastly, calculate the distance measure by the Ultrasonic Ranging Module “distance= duration*0.034/2” before returning the distance measure.

 

 

Processing

I am using Windows10, so I downloaded Processing 3.5.4 Windows 64-bit

from https://www.processing.org/download/

 

After download Processing, copy the folder “processing-3.5.4” to “C:\Program Files” folder.

 

Open this folder, select “processing.exe” and right click Pin to Start. And execute the Processing icon.

 

Coding Processing

 

Inside the function “void setup()”, instruction “loadFont” uses a font type called OCRAExtended-30.vlw.

 

To use this font, select Tools > Create Font…

Choose OCRAExtended from the list, enter file name “OCRAExtended-30”.

 

 

Program Code for Processing

 

Description of Program Code for Processing

1. In function setup(), starts the serial communication “myPort = new Serial(this,"COM4", 9600) to reads the data from the serial port up to the character '.' ”myPort.bufferUntil('.')”. Using the function serialEvent() “serialEvent (Serial myPort)”, the data received from the Serial Port are place into the String variable "data" “data = myPort.readStringUntil('.')”. The data will be used to determine the angle “angle= data.substring(0, index1)” and distance “distance= data.substring(index1+1, data.length())” of detection by the Ultrasonic Ranging Module. The angle and distance will be converted into variables iAngle “iAngle = int(angle)” and iDistance “iDistance = int(distance)”, which will be used for drawing the radar and the draw object detected.

2. In the function draw(), 4 different function are continuously call for drawing the radar “drawRadar()”, “drawLine()”, “drawObject()” and “drawText()”.

3. Radar are created in the function drawRadar(), using the arc() function to draw the radar arc and the line() function to draw to radar line.

4. Line moving along radar are created in the function drawLine(), the centre of rotation (i.e. starting coordinates to new location) is make using translate() “translate(960,1000)”. And using the line function “line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle)))” in which the iAngle variable is used, and the line is draws the line according to the angle.

5. Detected object are created in the function drawObject(), getting the distance (cm) from the Ultrasonic Ranging Module and translate into pixels “pixsDistance = iDistance*22.5”. And draws the line of detection on the radar according to the angle using the line function “line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle)))”.

6. Texts on the screen are created in the function drawText() using text function “text()”.

 

 

Data Processed and Graphic Generated in Processing

Using Serial communication, Processing will receive the values (data from Serial Port) of the angle and the distance measured by the Audrino UNO and the Ultrasonic Ranging Module. Processing will than display an Ultrasonic sensor map with a moving line that sweep back and forth across the map, displaying detected object in red onto the map at specific position with respect to angle and distance measured (1).

 

 

Interfacing Arduino UNO to Processing

Upload the program code onto Arduino Uno, and Processing using Serial Comunication will receive the values (data from Serial Port) of the angle and the distance measured by the Audrino UNO and the Ultrasonic Ranging Module.

 

 

Video of Object Detection

This the the video showing the application using Processing Development Environment with Arduino UNO, a servo motor and an Ultrasonic Ranging Module HC-SR04C to display the detected objects.

 

 

Source Codes for Arduino UNO and Processing

1. Arduino Code for controlling electronic Setup, right mouse click and save link: wk12servoprocessing.ino

2. Processing Code, right mouse click and save link: wk12radar.zip

 

 

What I have learned: 

 

1. Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts.

2. Processing software is free and open source, and runs on the Mac, Windows, and GNU/Linux platforms.

3. Arduino UNO and Processing works well together.

 

Citation

1. Reference from URL on 29/04/2020: https://howtomechatronics.com/projects/arduino-radar-project/

2. Reference from URL on 29/04/2020: https://drive.google.com/file/d/1qk3uX8hXT_s0Efy2cFTjb8cy3bbxGtNl/view?usp=sharing

3. Reference from URL on 29/04/2020: https://drive.google.com/file/d/1PfmT_ChZM5pxvBQbRcfyYt7VAV_2dgGW/view?usp=sharing