Skip to content

15. Interface and application programming

Week 15 May 4th

This week’s assignment:

Individual assignment:

  • write an application that interfaces a user with an
  • input &/or output device that you made

Group assignment:

  • compare as many tool options as possible

Write an Application that interfaces a user with an input and/or output device that you made.

This Week I have worked on an application that can help me to interface and control an output device I have made, more specifically a board controlling a servo motor.

As a programming language I have chosen Processing. I liked the idea to work with processing because I wanted to explore a different language and Processing is based on Java. Previously throughout the Fab Academy I have been working on the Arduino IDE, and with C code.

Part 1, controlling the servo.

The base template I used for processing presented the idea of controlling the servo with two buttons. One would make the servo move clockwise, and the other would make the servo move counterclockwise.

Thus with the help of Abdon, I developed a simple program to control the Servos by increasing PWM, using serial communication. The firmware, developed in Arduino IDE, allows for the increase or decrease of the PWM, in intervals of 1, and moving from 6 to 36, as extensively discussed in Week 11 Output Devices. This corresponds to moving the servo from 0 to (almost) 180 degrees.

Problems and how we solved them.

Initially we tried to control the servo not by PWM, but by Milliseconds, but it did not work. We did not receive a proper response in the form of a square signal on the oscilloscope. Thus we decided to redo the code based on controlling PWM by increasing or decreasing one unit as a result of loading an input of 1 or 0. This worked well, and we could check it on the monitor of Arduino IDE.

See the Code here (Arduino IDE):

#include <SoftwareSerial.h>

int rx = 2;    //Declares the RX 1 pin 3
int tx = 1;    //Declares the TX 2 pin 4

SoftwareSerial mySerial(rx, tx);  //Setting up the RX/TX pins as a SoftwareSerial

 int servoPin = 0; // Control pin for servo motor
 int minPulse = 6; // Minimum servo position
 int maxPulse = 36; // Maximum servo position
 int pulse = 7; // Amount to pulse the servo

char buttonState = '1';//Declares the character that represents the virtual button current state


void setup(){
  mySerial.begin(9600);       //Start the serial communication and select the its speed that deppends of the frequency that it will be program the attiny

  pinMode(servoPin, OUTPUT);      //Configures the LED pin as an output

}

void loop() {

  buttonState = mySerial.read();    //Reads the message a "1" or a "0" from the command line      

    if (buttonState == '1') {         // Condition For Motor ON
      if(pulse <maxPulse){
        pulse = pulse-1;
        if (pulse <=minPulse) {
          pulse=7;
        }
      }
      //mySerial.println(pulse);
    }
    else if (buttonState == '0'){      // Condition For Motor OFF
      if(pulse > minPulse){
        pulse = pulse+1;
        if (pulse >=maxPulse) {
          pulse=35;
        }
      }
      //mySerial.println(pulse);

    }
    analogWrite (servoPin, pulse);
    delay(50);
  }

Part 2 The Processing Application.

The processing part was pretty straightforward and worked properly. Working with the Arduino IDE we had tested how the PWM increased or decreased according to the input received in the form of either 1 or 0. Now, working on processing we could see the PWM increasing or decreasing graphically, and in real time, on the oscilloscope. This was very cool. Finally we tested the servo motor connected to the PCB Board, and verified that what we saw in the oscilloscope corresponded to the motion of the servo.

Problems and how we solved them.

Processing assigns a number to the port to which the board is connected, that is different from the number we found in the Device Manager on Windows. This implied that at first the board did not communicate properly with the Processing Interface. We found a post explaining this issue, and we could quickly debug.

See the code here (Processing):

/*
 Controlling servo motor from Processing.
 Processing Version.

 Based on Tom Igoe"s example:
 http://itp.nyu.edu/physcomp/Labs/Servo

 by Berio Molina
 Created 10 May 2008
 */

 import processing.serial.*;

 Serial myPort;

 color colorButtonR = #222222;
 color colorButtonL = #222222;

 void setup(){
 size(400, 300);
 //println(Serial.list());
 myPort = new Serial(this, Serial.list()[2], 9600);
 noStroke();
 smooth();
 }

 void draw(){
 background(0);
 fill(colorButtonR);
 rect(width-100, 0, 100, height);
 fill(colorButtonL);
 rect(0, 0, 100, height);

 if(mouseX<100){
 cursor(HAND);
 if(mousePressed){
 colorButtonL = #284c56;
 myPort.write('0'); // send 0 to the serial port
 }else{
 colorButtonL = #222222;
 }
 }else if(mouseX>width-100){
 cursor(HAND);
 colorButtonR = #284c56;
 if(mousePressed){
 myPort.write('1'); // send 1 to the serial port
 }else{
 colorButtonR = #222222;
 }
 }else{
 cursor(ARROW);
 }
 }

Videos:

Square PWM Signal increasing and Decreasing
Controlling Servo with Processing

Please see the comparisons that we developed as a group here:

CIDi’s comparison of different tools


Last update: July 12, 2022