Categories
11 Interface & Application Programming Fab Academy AS220 - Providence, RI

Using a GUI to Control Processing Output with ControlIP5, Firmata via Serial

The Fab Academy Assignment

  • Write a user interface for an input &/or output device

I created this simple interface to control turning on and off an LED that is attached to a microcontroller via the serial port on my Mac. I wanted to see if I could get the ControlIP5 (used to create the GUI), Firmata and serial libraries working together before I tried using more complex hardware. I intend to experiment with driving multiple servos and possibly tinkering with bluetooth using the NXT library as an “cheap” way (it’s “cheap” because I already own the Mindstorms hardware) to play around with bluetooth without buying additional bluetooth modules.

The Interface: Default / Initial State:

The interface is simple – the Turn On button turns the light on and the Turn Off button turns it off.

Here’s the Processing code:

/*-------------------------------------------------------------------
 * Fab Academy -- Module 09: Interface Programming
 *--------------------------------------------------------------------
 * Assignment: Write a user interface for an input &/or output
 * device.
 *--------------------------------------------------------------------
 * Purpose:  This program is a test to get the controlIP5, Firmata,
 * and serial libraries working together through the serial port.
 * This program uses a simple button GUI interface to turn on / of an
 * LED.
 *--------------------------------------------------------------------
 * Anna Kaziunas France - 30 March 2010
 * Combined / Modified example code from:
 * controlIP5 buttons example (included the library download)
 *------------------------------------------------------------------*/

import processing.serial.*;
import cc.arduino.*;
import controlP5.*;

ControlP5 controlP5;
// we have to use controlP5.Button here since there
// would be a conflict if we only use Button to declare button b.
controlP5.Button b;
Arduino arduino;

// Variables
int ledPin = 11;
int buttonValue = 0;
int myColor = color(0);

void setup() {
  arduino = new Arduino(this, Arduino.list()[2], 57600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  size(640,480);
  smooth();
  frameRate(30);
  controlP5 = new ControlP5(this);
  controlP5.addButton("Turn_On",255,200,80,100,70);
  controlP5.addButton("Turn_Off",0,200,160,100,70);
  println(Arduino.list());
}

void draw()
{
  background(myColor);
  fill(buttonValue);
  rect(20,20,width-40,height-40);
}

public void controlEvent(ControlEvent theEvent) {
  println(theEvent.controller().name());

}

// function buttonA will receive changes from
// controller with name Turn_On
public void Turn_On(int theValue) {
  println("a button event from Turn_On: "+theValue);
  myColor = theValue;
  arduino.digitalWrite(ledPin, Arduino.HIGH);
}

// function buttonB will receive changes from
// controller with name Turn_Off
public void Turn_Off(int theValue) {
  println("a button event from Turn_Off: "+theValue);
  myColor = theValue;
  arduino.digitalWrite(ledPin, Arduino.LOW);
}

Skills Learned

  • I learned how to find, utilize and manipulate additional Processing libraries that enabled me to create a simple user interface from a computer to a physical object (LED).

Tools Used

  • Processing, ControlIP5 & Firmata libraries
  • Arduino ATTMega, LED
Categories
10 Sensors, Actuators and Displays Arduino / Processing Fab Academy AS220 - Providence, RI Halloween

Motion Sensing Glow Skull

Voodoo Glow Skull

I am ready for Halloween early this year. For my I/O sensor project for Fab Academy I put together a Arduino-controlled motion sensing glowing skull. When motion is detected by the parallax motion sensor, the board turns on the LEDs in the mouth and fades in and out the LEDs glued into the eye sockets.

The Fab Academy Assignment

  • Interface an input device with an output device.

The Project: Motion Sensing Glowing Skull

Voodoo Glow SkullVoodoo Glow Skull

Parts List:

  • 1 Parallax PIR [Pyroelectric (“Passive”) InfraRed)] motion sensor
  • A few lengths of wire
  • 1 Arduino compatible board (I used a Seeduino (Seeed Studios) I had laying around). I like this board because I can flip the power source switch to turn it off / on.
  • 1 9 volt battery
  • Mouth: 10mm Red LED (3)
  • Eyes: 5mm Red Wide-Angle LED (2)
  • 5 in or so metal strip with holes (to house mouth LEDs
  • 3 plastic LED holders (to insulate the mouth LED wires from the metal strip)
  • super glue / hot glue

Prototyping the Circuit / Interaction + Putting It Together:

prototypingprototyping - jumbo LEDprototypingprototypingLED harnessLED harnessGlow!

Code:

The code works – but needs to be modified, right now after the motion is activated, the lights stay on / fade in and out in an infinite loop until the power is switched off. Look for an update to this post.

Arduino code for the LEDs and Parallax PIR Motion Sensor:

/* -----------------------------------------------------------------
 Anna Kaziunas France
 --------------------------------------------------------------------
 Fab Academy - Sensors I/O Module
 Glowing Skull Project
 03/02/2010
 --------------------------------------------------------------------
 Motion Sensor code:
 Motion Sensor code:I have seen this code a few places,
 it is never attributed to anyone in particular.
 I saw it last at: http://www.ladyada.net/learn/sensors/pir.html
 --------------------------------------------------------------------
 LED Fader code by: By David A. Mellis - Created 1 Nov 2008
 Modified 17 June 2009: By Tom Igoe
 http://arduino.cc/en/Tutorial/Fading
 --------------------------------------------------------------------
 Combined / Modified by Anna Kaziunas France - 03 March 2010
 --------------------------------------------------------------------

 --------------------------------------------------------------------
 Purpose of this Program
 --------------------------------------------------------------------
 Read input value from the sensor
 Determine if motion is present (input is HIGH)

 When motion is detected via motion sensor:
 1. Eyes slowly glow red (fade in and out - continue)
 2. Mouth glows (steady)

 When motion is not detected after (length of time)
 Switch off LEDs
 ------------------------------------------------------------------*/

// Variables
int ledPinSolid = 13; // choose the pin for the LED
int ledPinFade = 11; //
int inputPinSensor = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status

void setup() {
  pinMode(ledPinSolid, OUTPUT); // declare Solid LEDs as output
  pinMode(ledPinFade, OUTPUT); // declare Fader LEDs as output
  pinMode(inputPinSensor, INPUT); // declare sensor as input

  Serial.begin(9600);
}

// Begin Motion Detection
void loop() {
  val = digitalRead(inputPinSensor); // reading input value
  if (val == HIGH) { // if the input is HIGH
    digitalWrite(ledPinSolid, HIGH); // turn LED ON
    // sets the value (range from 0 to 255):
    analogWrite(ledPinFade, HIGH); // turn LED ON
    // fade in from min to max in increments of 5 points:
    for(int fadeValue = 0 ; fadeValue < = 255; fadeValue +=10) {
      // wait for 30 milliseconds to see the fade in effect
      delay(400);
    }
    // fade out from max to min in increments of 5 points:
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=10) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPinFade, fadeValue);
      // wait for 10 milliseconds to see the dimming effect
      delay(110);
    }
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected Huzzah!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  }
  else {
    digitalWrite(ledPinFade, LOW); // turn Fader LEDs OFF
    digitalWrite(ledPinSolid, LOW); // turn Solid LEDs OFF
    if (pirState == HIGH) {
      // we have just turned of
      Serial.println("Motion ended");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

Skills Learned

  • Basic wiring
  • Modifing Arduino code
  • How to read a sensor datasheet

Tools Used

  • Arduino
  • Paralax motion sensor