Write an application that interfaces a user with an input and/or output device that you made.
1- Create a code to controll the servo motor using serial connection in Arduino IDE.
2- Create a code interface between the user and the other code using Processing software.
3- Interface the two software.
This week I wanted to create an interface that moves the rotor of a servo motor, which is an output device. I used the ATmega328 board that was designed in the Output Devices week. Since I know only one coding language, which is C++, I decided to use Processing software. This software is a free graphical library and integrated development environment built for the electronic arts, new media art, and visual design communities. To learn more about this software and how to connect it Arduino IDE, I read two tutorials: Instructables and Sparkfun.
After I connected the ATmega328 board by an FTDI cable, I ensure that the board selected in tools is "Arduino/Genuino Uno" and the port is "COM12".Then, I wrote the below code. I started by including the servo library and define my servo motor. In the setup section, I determine the pin that my servo motor will be attached to in the board. Then, I used "Serial.begin()" to start the serial connection. In the loop section, I check the availability of the connection and read it using "Serial.available()" and "Serial.read()", respectively. If the readed character is the letter "a", the rotor position will be at 0 degree. While if it was the letter "c", the rotor position will be at 180 degree.
// Controlling a sevo motor using Arduino IDE and Processing
#include Servo.h // Servo motor library
Servo myfirstservo; // Define my servo motor
void setup() {
myfirstservo.attach(5); // The first CR servo motor is attached to PD5 (pin 5) in ATMEGA328
Serial.begin (19200); // To begin the serial connection
}
void loop() {
if(Serial.available()){ // To ensure that the connection is availabe
char x = Serial.read();
if (x =='a'){
myfirstservo.write (0);
}
if (x =='c'){
myfirstservo.write (180);
}
}
}
Firstly, I downloaded the software from here. Then, I check for the "ControlP5" library since it enables the software to control wide range of microcontrollers. I imported this library and the "Processing.serial" library to connect Arduino IDE with Processing. After that, I introduced a serial port, a controlp5 object, and a font object. In the setup section, I choose the size and the font of the interface window. Then, I created the serial connection and controlp5 constructor using "myport= new Serial (this, "COM12", 19200)" and "cp5= new ControlP5 (this)", respectively. Then, I added the buttons, named them,and chose their position and sizes. In the draw, I selected the background color, text color, text font, text size, and the title position. To control the buttons, I added two functions include "myport.write('a')" command to write the data on the serial.
// Controlling a sevo motor using Arduino IDE and Processing
import controlP5.*; // Library because it imports various controllers
import processing.serial.*; // To connect Arduino IDE with Processing
Serial myport; // Introduce a serial port
ControlP5 cp5; // Introduce a controlp5 object
PFont myfont; // Introduce a font object
void setup (){
size (500,400); // To create a 500*400 window
myfont= loadFont("AdobeDevanagari-Italic-45.vlw"); // To create the font
printArray(Serial.list()); // To list the available serial ports
myport= new Serial (this, "COM12", 19200); // To create the serial connection
cp5= new ControlP5 (this) ; // To create controlp5 constructor
cp5.addButton ("Left") .setPosition (100, 230) .setSize (100, 100);
cp5.addButton ("Right") .setPosition (300, 230) .setSize (100, 100);
}
void draw (){
background (200, 300, 200); // To choose the color of the background
fill (0, 0, 0); // To choose the text color
textFont (myfont); // To choose the text font
textSize (70); // To choose the text size
text ("The Rotation", 100, 120); // To show the title and fix its position
}
// Function to the Left Button
public void Left() {
myport.write ('a');
}
// Function to the Right Button
public void Right() {
myport.write ('c');
}
When I ran the code, I obtained this window with the two button.
To connect the code, I uploaded the Arduino code in the ATmega328 board first using an FTDI cable. Then, I connected the sevro motor and ran the processing code. After that, the 500*400 window came up and I pressed "Right" button to move the rotor of servo motor to the right.
Then, I pressed "Left" button to move the rotor of servo motor to the left.
The link of the group assignment page is here.
Compare as many tool options as possible.
Write the advantages and disadvantages of using Pro
Processing is is a free graphical library and integrated development environment built for the electronic arts, new media art, and visual design communities.
* Advantages:
1- Free and can operate in MacOS, Windows, and Linux.
2- Open source.
3- User friendly and simple.
4- Various programming languages.
* Disadvantages:
1- Low level software.
# Challenges:
The major problem I faced was uploading Arduino IDE code while the processing serial window is open since both softwares using serial functions at the same time.