Index  About Me

Interface and Application Programming

This assignment was hard and easy at the same time, being so many languages and IDEs to choose from, I decided to build on lessons learned.

This particular assignment was about making an app to interact with a device, be it input or output. 

interface and applications programming workflow
First, the simplest way to approach the assignment was to build on what was already done and build and app to go with it. So, I chose to create a simple app to operate the servos from my output devices assignment. Since I was using the Arduino IDE for programming, I chose Processing as the Java language and structure its based upon are very similar.


So, the strategy was to create a Processing-based interface to connect with the Arduino IDE. The first inspiration for this was the Dimmer example found in the Arduino IDE and Which includes code for Processing. The example was about three things:

  • mapping the mouse position to a number (0-255, to match the grayscale colors)
  • opening a Serial port to talk to the arduino board
  • Sending a number (0-255) to the arduino which will light a LED accordingly, since we want to "dim" that LED, an Analog Pin must be used. 
dimmer: arduino + processing
The key to the whole example is the way Processing and Arduino Communicate through the Serial Port, and based upon that I decided to write my app, building from several existing example codes.
First part of the code in Processing
The critical line of the code is marked on red. This is, opening the serial port for transmitting orders, in this case, I was using just a character as the trigger for the arduino board. The Arduino board would receive a character from the port and , depending on what it receives, it will trigger a specific action.

port opening in arduino
As I was testing, I realized that even as I was sending the characters via de Processing script, the arduino would not provide the intended answer (for this, I used my servo relay board from the Output Devices assignment. So that when the Arduino got the proper character, it would trigger one of the two servos in the board to activate. But it was not working,  some debuggging needed to be done.

comparing bytes between arduino and processing

In the end the problem was in the way both arduino and Processing encode what is sent through the port. While I was using Arduino to read the bytes sent using the statement (i. e. if(motor = 8) )  I was getting receiving the byte and activating the servos. This was not what was intended, and, in order to get the right byte the if statement  had to be enforced, as it can be seen in the image above, the statement  if( motor == 8)   is being used, this meaning that the precise byte will be entered  in order for the servo to be trigged.

Which brings as to the other end, so how could the bytes sent through the processing interface so they would be properly processed? The answer to this, after some consultation and tinkering was ASCII, yes, ASCII.

As it can be seen above, Arduino is expecting the characters '8' and '9' to set the servos... but the Processing script has the 'by' variable in the byte type, so instead of sending the characters as stated, it was sending its ASCII counterparts. This was easily Fixed by changing the bytes sent in Processing for its ASCII equivalents, this is:

  • byte(56) --> '8'
  • byte(57) --> '9'
With this, the program worked fine.

interfaz Servo
Finally, the source code for the Arduino and Processing can be found:
- Arduino Code
- Processing Code

Conclusions?

unfortunately, I had too little time to get this assignment done, so I had just to get on with it.

Second, The code written is not optimal and certainly can be improved.

Third, as previously stated, take into account Hofstadter's law into assignment planning, it -painfully- works.

Back to the Index