Introduction

For this week we had to:
write an application that interfaces a user with an input &/or output device that you made
compare as many tool options as possible

First I want to make an interface with Processing to communicate with arduino board (ultrasonic sensor, drive a stepper motor). While researching 2019 fabacademy student's assignments I found Xiaofei Liu's assignment very interesting. He used a software called MAX that is an interactive media software. With tools for audio, graphics, interaction, and communication. I will try to make an application with that tool too.

For this process we will need:
Arduino UNO board
Arduino Motor Shield
Arduino IDE if you need to install it go here.
Processing software if you need to install it go here.
Ultrasonic sensor
Stepper motor
A-to-B USB cable
Pinheader wires, Bredboard

Connecting Processing to Arduino

I found this Tutorial very helpful. I'll setup a serial communication and send "Hello World" message from Arduino board and get it in my interface. Then I'll try to read analog signal (Ultrasonic sensor value) with Arduino and send the value to my interface.

Arduino side

Connect Arduino board to your PC, open the arduino IDE. Paste the code below.
void setup()
{
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}
void loop()
{
//send 'Hello, world!' over the serial port
Serial.println("Hello, world!");
//wait 100 milliseconds so we don't drive ourselves crazy
delay(100);
}

Let's understand the code:
Serial.begin(9600); starts serial communication from the Arduino to our computer at a baud rate of 9600. Serial.println("Hello, world!"); sends the string 'Hello, world!' over the serial port, over and over. All that's left to do is to plug in our Arduino board, select board type (under Tools -> Board Type) and Serial port (under Tools -> Serial Port). (See image below)

arduino1

Finally hit the 'upload' button to load the code onto the Arduino. To check if this works open Serial Monitor (under Tools -> Serial Monitor) (See image below)

arduino2

If you get "Hello World" messages everything works if not check your USB cable and try the steps above again.

Processing side

Now we need to recieve what our Arduino is sending into Processing. Processing comes with a Serial library designed for just this thing! To load the library open the Processing software go to Sketch->Import Library->Serial (See image below)

processing1

At this point we need to declare some variables that can be used anywhere in our processing program. Add these two lines under the import processing.serial.*;:


Serial myPort; // Create object from Serial class
String val; // Data received from the serial port

In order to read serial communication we have to get a Serial object (we called it myPort but it can be whatever we want), which lets us listen in on a serial port on our computer for any incoming data.
We also need a variable to store the actual data coming in. In our case, we're sending a String ('Hello, World!') from Arduino, we want to receive a String in Processing.

Dc motor cw and ccw

Usefull links

Conclusion