W15 interface and application programming

W15.  interface and application programming

write an application that interfaces with an input &/or output device

 

This week we had to write or modify an application that interfaces with a device.

As I have never writed an application before so first I wanted to get introduced to Processing. What I did first is to upload some of the examples and modify them to see the results. First I used OnOff sketch and modify color, shape and movement making adjustments to the parameters in the code.

Captura de pantalla 2015-06-29 a las 18.31.52
Captura de pantalla 2015-06-29 a las 18.39.12
Captura de pantalla 2015-06-29 a las 18.39.56

After testing changes in the code and seen the results, I realize I needed to make a path to use Arduino and Processign because I will need it for my final project, in which I may  use a sound sensor or/ and move a servo with an interface.

For this I installed the library to use arduino+processing and use the code I already tested with the servo motor (see details in week 18). In order to use processing and arduino environment I follow the tutorial in processing page, you can follow it in the  link.

I download the firmata library to use in both Arduino and Processing.

First in Arduino I had to upload the code (example servo motor), then closing the serial monitor I opened Processing and simply uploading the firmata sketch I got to move the servo by the movement of the mouse from one side to the other. This example let me define the direction and angle of the movement in the servo, and also it could be useful as a on and off interface.

code for servo motor, example skecth in Arduino IDE

  1. #include <Servo.h>
  1. Servo servo;
  1. int pot = 3;
  1. void setup() {
  1. Serial.begin (9600);
  1. servo.attach(6);
  1. }
  1. void loop() {
  1. int valorpot = analogRead (pot);
  1. int val = map (valorpot, 0, 1023, 0, 180);
  1. Serial.println (val);
  1. servo.write (val);
  1. delay (100);
  1. }

I modify the size  and add a text for  the angle on the servo, I got the following.

Captura de pantalla 2015-06-29 a las 22.02.36

I have never used processign and it was easy, the dificult part was to use processign with a data base of Arduino, but I learned how to do some interfase that I could use for my final project.

Ih the video you can se how I move the servo motor from a side to the other.  I will continue learning about interfases, I was fascinated to see how many things I could do with it.

I am using the example skecth from firmata to a servo motor, modifying the port,  and including text for the range of the angle.

code for Processing:

  1. import processing.serial.*;
  1. import cc.arduino.*;
  1. Arduino arduino;
  1. void setup() {
  2. size(730, 220);
  1. println(Arduino.list());
  2. arduino = new Arduino(this, "/dev/tty.usbmodem1421", 57600);
  3. arduino.pinMode(4, Arduino.SERVO);
  4. arduino.pinMode(9, Arduino.SERVO);
  5. }
  1. void draw() {
  2. background(constrain(mouseX / 2, 0, 180));
  3. //texto para 90 0 180
  4. textSize(60);
  5. text("0º", 50, 130);
  6. fill(0, 240, 153);
  7. textSize(60);
  8. text("90º", 310, 130);
  9. fill(0, 155, 153);
  10. textSize(60);
  11. text("180º", 550, 130);
  12. fill(0, 255, 153);
  1. arduino.servoWrite(9, constrain(mouseX / 2, 0, 180));
  2. arduino.servoWrite(4, constrain(180 - mouseX / 2, 0, 180));
  1. }
  2.  
  3.