14. Interface and application programming¶
This week we learn to write an application that interfaces with an input or output device. And I use one Arduino board and a Alisauino board to upload the code seperately.
Arduino Board¶
We use the processing software work with the Arduino board to program at first. Before use the Processing software, upload the standard Firmata to the board by Arduino. Open the Processing, click File-Example-Add Examples
, add the Arduimo (Firmata) library.
Processing input examples
import cc.arduino.*;
Arduino arduino;
color off = color(0, 0, 111);// color value, such as 0 means white, 255,0,0 means red.
color on = color(255, 145, 158);
void setup() {
size(500, 300); //size of interface
println("COM5");
arduino = new Arduino(this, "COM5", 57600);
for (int i = 0; i <= 13; i++)
arduino.pinMode(i, Arduino.INPUT);
}
void draw() {
background(off);
stroke(on);// Draw a filled box for each digital pin that's HIGH (5 volts).
for (int i = 0; i <= 13; i++) {
if (arduino.digitalRead(i) == Arduino.HIGH)
fill(on);
else
fill(off);
rect(420 - i * 30, 30, 30, 25);
}
// Draw a circle whose size corresponds to the value of an analog input.
noFill();
for (int i = 0; i <= 5; i++) {
ellipse(200 + i * 30, 200, arduino.analogRead(i) / 6, arduino.analogRead(i) / 3);
}
}
At first, I couldn’t understand why I connect one pin, but the rest several circles also changes and more than one square blink. My instructor tell me that as the 14 squares means all the DIGITAL (PWM) pins and 6 circles represent all the ANALOG IN pins. As the rest pins don’t be connected and the microcontroller don’t know whether the pins should be off or not. If connect the rest pins and tell the IC they are off, then only one circle changes.
Alisauino board with ATtiny412¶
At first, We use the standarFirmata to progama the Alisauino and failed. The we use Example-Libraries-Serial-SimpleWritesimple chart example
, and it works well.
Simple Write¶
- Program the Alisauino board.
/*
//Wiring/Arduino code:
//Read data from the serial and turn ON or OFF a light depending on the value
char val; // Data received from the serial port
int ledPin = 4; // Set the pin to digital I/O 4
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
while (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
}
delay(100); // Wait 100 milliseconds for next reading
}
- Use Processing interface with mouse.
* Check if the mouse is over a rectangle and writes the status to the serial port.
* This example works with the Wiring / Arduino program that follows below.
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(360, 360);
String portName = "COM3";
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(0,191,255);
if (mouseOverRect() == true) { // If mouse is over square,
fill(255,250,205); // change color and
myPort.write('H'); // send an H to indicate mouse is over square
}
else { // If mouse is not over square,
fill(255,193,193); // change color and
myPort.write('L'); // send an L otherwise
}
rect(90, 90, 160, 160); // Draw a square
}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 90) && (mouseX <= 250) && (mouseY >= 90) && (mouseY <= 250));
}
Simple Reader¶
- Program ATtiny412.
// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.
int switchPin = 4; // Switch connected to pin 4
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.write(1); // send 1 to Processing
} else { // If the switch is not ON,
Serial.write(0); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}
*/
- Use Processing interface with mouse.
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
int angle = 0;
void setup()
{
size(640, 640);
background(154,255,154);
noStroke();
fill(255,192,203);
String portName = "COM3";
myPort = new Serial(this, portName, 9600);
}
void draw() {
// Draw only when mouse is pressed
if (mousePressed == true) {
angle += 5;
float val = cos(radians(angle)) * 12.0;
for (int a = 0; a < 360; a += 75) {
float xoff = cos(radians(a)) * val;
float yoff = sin(radians(a)) * val;
fill(255,192,203);
ellipse(mouseX + xoff, mouseY + yoff, val, val);
}
fill(255);
ellipse(mouseX, mouseY, 5, 5);
A basic interface¶
- I install several images libraries.
-
I use the Alisauino 2 board to run the code in Arduino software, sarvo motor as the output device and processing interface as the input part.
-
The code for Alisauino.
- The code for Processing.
Video
Problems and Errors
- I forget to add the
Serial.swap(1)
to the Arduino code. As Processing allow us to see the input message, which works like serial monitor in Arduino software . And theSerial.swap(1)
should be alway in front ofSerial.begin(9600)
. - I forget to close the serial monitor in Arduino and couldn’t run the processing. It shows “Port busy”.
Group Assignment¶
We are interested many softwares, considering the limited time we tried several and program this week.
jQuery: jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
Qt : not a open resource, can be used for students to learn the raspberry pie device.
wxPython: a cross-platform GUI toolkit for the Python language. It runs on Windows, Macs and Linux or other unix-like systems.
Processing:a flexible software sketchbook and a language for learning how to code within the context of the visual arts.