header
// 1-Principles and proyect management   // 2-Computer-aided design // 3-computer-controlled cutting
// 4-electronics production // 5-3D scanning and printing // 6-electronics design // 7-molding and casting
// 8-embedded programming // 9-computer-controlled machining // 10-input devices // 11-composites  
// 12-interface & application programming // 13-output devices // 14-networking & communications
// 15-mechanical design, machine design // 16-appplications and implications
// 17-invention, intellectual property and income // 18-project development
// FinalProject // 

Week12: Interface & App programming

Now its time to program something usable. This week we have to take an input pcb that we had made and make a program to interact with it.


Arduino and Processing



I decide to do this week assignment in Processing, because it was a new lenguaje for me. First of all we need to install the Proccessing IDE.

In my experience, the processing version must be according to the Java version. If you install the Java6 version, you could install any of the processing version (the stable 1.5.1 or the 2.0 Beta version), but if you install the Java 7 package, i recommend use the 2.0 version of Processing. The stable version give me some error with Java 7.

Download Processing

After downloading Processing, you must install java (in my case, Java 7)

Download Java

After this, processing is a portable program, so you dont need to install it.

PROBLEM

When you start processing as a normal user (at least in Ubuntu), this user dont have access to the USB port, so if you try to run some program that try to use this port, the system dont let them pass.

SOLUTION

Start the processing program as superuser. After spend three days searching troubles with FTDI os something like that, finally i started it with the command:

$ sudo ./processing

and finally i could read the data from the board.


My program with Hello.light



I want to play with the Hello.light i solder in the Input week, so i think the program could give me a good view of how many light there are in the ambient.

So basically the processing program will draw a sun if the valor of the sensor is lower than a valor and will draw a moon if the valor of the sensor is higher.

This sensor returns a higher value if there are less light.

Processing has two important blocks of code: setup and draw.

  • "void setup()" its the way to initialize the enviroment of the program (windows, canvas, colours).

  • "void draw()" is the main program, and it loops continuosly. Its like the "int main()" in C.

I decide to add some details, so while its daylight there are two little birds flying around the sun, and when its moonlight...watch the video and see it yourself :)

And here is the result:


You can download the processing sketch or see below:


/**

 * Simple Read - Combined with Neil Gershenfeld's hello.light.45.py

 * http://academy.cba.mit.edu/classes/input_devices/light/hello.light.45.py

 * adjusted values pulled from Neil's python visualazition program to make

 * sense of high and low readings.

 *

 * Read data from the serial port  and change the color of a rectangle

 * when the phototransistor connected to the hello light board is receiving

 * varing levels of light.

 * This example works with the Wiring / Arduino program that follows below.

 */


import processing.serial.*;

//VARIABLE DEFINITION


Serial myPort;  // Create object from Serial class

int val;      // Data received from the serial port

int sensorData; // Data recieved from the serial port with 1,2,3,4 framing numbers filtered out


int valorbueno;

boolean dibujado = false;

PImage imget;

float x,y,a,s=0.0;

boolean pajarogrande=false;

int tiempoahora=0,tiempoantes=0;

boolean hadchanged=false;



//////drawing the stars

void estrellas(){


  //only 30 stars was drawn, all rancomly positioned inside the canvas

  for (int i=0; i <= 30; i++){     

      stroke(255);

      point(random(width),random(height));


  }

}


//drawing the birds


void pajaro(){

 

    //draw a bird with two states, one bigger and one smaller

    translate(random(10),random(10));     //move the bird randomly in a 10x10 square

  if (pajarogrande){          //ask if i draw the big shape of the bird

    //draw the smaller bird

    scale (1,0.9);    //% of scaling in x and y

    pajarogrande=false;    

  }

  else{

      //draw the bigger bird

      scale (1,1.1);

      pajarogrande = true;

  }

 

  fill(255);

  line(100,100,110,110);  //left wing

  line(110,110,120,100);  //right wing

//  delay (100);    //avoid the epileptic movement in the bird

 

}


///drawing the sun

void sol(){

  fill(0,191,255);    //blue

  rect(0,0,700,400);    //draw the sky

  fill(255,255,0);  //yellow

  ellipse(300,200,300,300);    //draw the sun 

  pajaro();        //draw a bird


 


}


////drawing the moon

void luna(){

      background(0);

      fill(255);                 // white

      ellipse(300,200,300,300);  // draw a circle

      fill(0);                    // black fill

      stroke(0);                  // black stroke

      ellipse(200,100,300,300);    // draw the missing part of the moon circle

       estrellas();            //draw the stars

      

// put a image in the canvas       

      if (x<=width){        //if the image isnt aoutside the canvas

          y=y-2;            // y movement in 2 pixels by frame

          x=x+4;          // x movement in 4 pixels by frame

          translate(x,y);         // move the position of the below draw or image from the las frame

          image(imget,-100,400);   // show a GIF in the canvas. -100,400 is the initial position of the image

      }

     

}




///////////////////////MAIN PROGRAM/////////////////////////


void setup()

{

  size(700, 400);  //Anna Kaziunas code for hello.light

  // I know that the first port in the serial list on my mac

  // is always my FTDI adaptor, so I open Serial.list()[0].

  // On Windows machines, this generally opens COM1.

  // Open whatever port is the one you're using.

  String portName = Serial.list()[0];

  myPort = new Serial(this, portName, 9600);

 

  imget = loadImage("et.gif");  // variable that contains a image file

}


void draw()

{

  if (myPort.available() > 0) {    // If data is available

    val = myPort.read();           // read it and store it in val

    if (val > 4) {                // Filter out the framing numbers: 1,2,3,4

       println("The actualVal is " + val);  //drop the valor in the terminal to check

       valorbueno = val;          //i will use a variable with usable valors. 1,2,3,4 didnt store here.

   }

 


    if (valorbueno < 160) {    // if its lightly. This valor must change to fit the needs

             sol();             // draw the sun         

    }

    else {                      

         luna();    //  draw the moon


    }


  }

}


Working with processing


That was the hardest job, to learn the lenguaje. Fortunately the main web page of processing has a great learning documentation to get all the objetives you could get:


If you dont know how works a programation languaje, go to check step by step at this TUTORIALS, and you could see how it works.


If you have programming skill and only want to know the functions and the parameters, go to the BASICS.


Also check his amazing page of  TOPICS, where you can see that with a few lines could get incredible effects.


PROBLEM


The change in the sensor value isnt as quick as i expected. Maybe this photosensor isnt as sensitive and there are other in the market that could bring this results more efficient. Or maybe my board arent well solder, or my DIY FTDI cable isnt as good as i should be.

I must work with the optimization of the response time in the photosensor and the code inside.