UP | HOME

Week 12 Assignments

Table of Contents

Week 12 - Interface and application programming

This week assignment involves creating an application that interacts with one of the previously created boards. I will connect my button board to the computer and program a processing skecth to read serial port values and react changing colors.

Programming the board

I had uploaded the hello.echo in my button board, but this is clearly non usable for this new puropose. I had to reprogram my board to send the button status on the serial port. I checked the hello.button sample code but it was prepared for attiny45 ports.

I changed the following sections of code to reflect my current setup:

#define input_port PORTA
#define input_direction DDRA
#define input_pin (1 << PA3)
#define input_pins PINA
#define serial_port PORTA
#define serial_direction DDRA
#define serial_pin_out (1 << PA1)

I also modifed the output to send 1/0 instead of u/d:

while (1) {
      //
      // wait for button down
      //
      while (0 != pin_test(input_pins,input_pin))
         ;
      put_char(&serial_port, serial_pin_out, '0');
      //
      // wait for button up
      //
      while (0 == pin_test(input_pins,input_pin))
         ;
      put_char(&serial_port, serial_pin_out, '1');
      }

Finally I had to modify my makefile changing the MMCU to attiny44 and the avrdude parameter to t44 instead of t45.

Programming the processing code

I downloaded and installed Processing 2.0.1, a program created initially at MIT to enable to produce rich interactive interfaces in a simple but powerful way. I explored some of the examples involving the serial library but found some errors using them. A pop-up window appeared explaining that some usual problems could be solved by the processing program. I chose to fix them automatically and I was able to run my program afterwards without problems.

This is my Processing code, based on SimpleRead sample, but modified to ressemble a Simon game that cycles through all colors. Code has been comented to explain what's doing on each step.

import processing.serial.*;

Serial myPort;  // Create a new object using the processing serial library
int val;        // Temporary variable used to store data received from the serial port
int cycle;      // Variable to cycle among all colors G->R->B->Y
void setup() 
{
  size(200, 200);
  String portName = Serial.list()[0]; //Opens first serial port available attached (FTDI)
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If there is some data incoming, store it in val
  val = myPort.read();         
  }

  background(0);               // Set background to black
                               // Draw four colour spheres. Button will cycle on them
  //Pattern inspired by original SIMON
  // Green/Red
  // Yellow/Blue
  fill(0,155,0); // Pale G
  ellipse(50, 50, 100, 100);
  fill(155,0,0); // Pale R
  ellipse(150, 50, 100, 100);
  fill(0,0,155); // Pale B
  ellipse(150, 150, 100, 100);
  fill(155,155,0); // Pale Y
  ellipse(50, 150, 100, 100);


  if (val == 0) {              // If button is not clicked, do nothing
    } 
  else {                       // Move to the next color and highlight it
    cycle++;
    }

  if (cycle == 1) {
  fill(0,255,0); // Paint it in bright G
  ellipse(50, 50, 100, 100); 
  } else if (cycle == 2) {
  fill(255,0,0); // Paint it in bright R
  ellipse(150, 50, 100, 100);
  } else if (cycle == 3) {
  fill(0,0,255); // Paint it in bright B
  ellipse(150, 150, 100, 100);
  } else if (cycle == 4) {
  fill(255,255,0); // Paint it in bright Y
  ellipse(50, 150, 100, 100);
  } else if (cycle == 5) {
  cycle = 0;     // Cycle back to all pale state
  }
  delay(100); // Included to prevent rebounds 
}

Video

In this video you can see how does it work:

Date: 2013-03-13 Wed

Author: César Garcíaa Sáez

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0