Assignment :
individual assignment:
write an application that interfaces with an input &/or output device
that you made
group assignment:
compare as many tool options as possible
This week our assignment is to write an application that can interface with an input or output device. Previously I have send data to computer and visualized using arduino serial Monitor and serial ploter . But creating a program that can intract with a board that i made in something different
I have done some projects with MIT app inventor. So I decided to try Processing this time .
Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology. There are tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning and prototyping.
We can easly downlod processing from Its official page processing.org/download
The User Interface of Processing is almost similar to arduino so it makes more esier to use. More detailed tutorials are available inthe Processing Totorial session.
Similarly the Processing coding is also similar to arduino . The structures in arduino and processing are almost similar. "void setup" serves a similar role of arduino "void setup" and also the "void draw" in processing is also doing a similar functionality of "void loop" in arduino.
For this week I didn't designed any PCB . I decided to choose my PCB from Input week . You can refer the details about the board in my Input week page.
Processing was an entirely new world to me . But it was easy than I thought . I resembled a lot about arduino .So I felt more confort in the envournment .
For this week I decided to make an application were a can stear a 3d car model using my capacitive track pad . So I have to do multiple tasks
1st I have to create my model in processing .
//*****************DRAW CYLINDER*********************
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
// If it is not a cone, draw the circular bottom cap
if (bottomRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, tall, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}
//*****************DRAW RECTANGLE**********************
void rct(float x_pos, float y_pos, float z_pos, float leng, float wid) {
pushMatrix();
translate(x_pos, y_pos, z_pos);
beginShape(QUADS);
vertex(-leng/2, -wid/2);
vertex(-leng/2, wid/2);
vertex(leng/2, wid/2);
vertex(leng/2, -wid/2);
endShape();
popMatrix();
}
//**********DRAW THE CAR*******************************
fill(255, 0, 0, 220);
noStroke();
box(30, 60, 150);
pushMatrix();
translate(-25, 0.0);
box(25, 60, 55);
popMatrix();
beginShape(TRIANGLES);
vertex(-15, 30, 40);
vertex(-15, 30, 27.5);
vertex(-40, 30, 27.5);
vertex(-15, -30, 40);
vertex(-15, -30, 27.5);
vertex(-40, -30, 27.5);
vertex(-15, 30, -40);
vertex(-15, 30, -27.5);
vertex(-40, 30, -27.5);
vertex(-15, -30, -40);
vertex(-15, -30, -27.5);
vertex(-40, -30, -27.5);
endShape();
beginShape(QUADS);
vertex(-15, 30, 40);
vertex(-40, 30, 27.5);
vertex(-40, -30, 27.5);
vertex(-15, -30, 40);
vertex(-15, 30, -40);
vertex(-40, 30, -27.5);
vertex(-40, -30, -27.5);
vertex(-15, -30, -40);
endShape();
beginShape(QUADS);
vertex(-15, 30, 40);
vertex(-40, 30, 27.5);
vertex(-40, -30, 27.5);
vertex(-15, -30, 40);
vertex(-15, 30, -40);
vertex(-40, 30, -27.5);
vertex(-40, -30, -27.5);
vertex(-15, -30, -40);
endShape();
beginShape(QUADS);
vertex(-40, 30, 27.5);
vertex(-40, -30, 27.5);
vertex(-40, -30, -27.5);
vertex(-40, 30, -27.5);
endShape();
fill(0, 0, 0, 220);
//***************************************************
beginShape(QUADS);
vertex(-15, 30.5, -35);
vertex(-15, 30.5, -2.5);
vertex(-35, 30.5, -2.5);
vertex(-35, 30.5, -22.5);
endShape();
beginShape(QUADS);
vertex(-15, 30.5, 35);
vertex(-15, 30.5, 2.5);
vertex(-35, 30.5, 2.5);
vertex(-35, 30.5, 22.5);
endShape();
beginShape(QUADS);
vertex(-15, -30.5, -35);
vertex(-15, -30.5, -2.5);
vertex(-35, -30.5, -2.5);
vertex(-35, -30.5, -22.5);
endShape();
beginShape(QUADS);
vertex(-15, -30.5, 35);
vertex(-15, -30.5, 2.5);
vertex(-35, -30.5, 2.5);
vertex(-35, -30.5, 22.5);
endShape();
beginShape(QUADS);
vertex(-15.5, -25, 39.99);
vertex(-15.5, 25, 39.99);
vertex(-36, 25, 30.5);
vertex(-36, -25, 30.5);
endShape();
beginShape(QUADS);
vertex(-15.5, -25, -39.99);
vertex(-15.5, 25, -39.99);
vertex(-36, 25, -30.5);
vertex(-36, -25, -30.5);
endShape();
//************************
pushMatrix();
translate(15, 21, 45);
drawCylinder(15, 15, 10, 50);
popMatrix();
pushMatrix();
translate(15, -31, 45);
drawCylinder(15, 15, 10, 50);
popMatrix();
pushMatrix();
translate(15, 21, -45);
drawCylinder(15, 15, 10, 50);
popMatrix();
pushMatrix();
translate(15, -31, -45);
drawCylinder(15, 15, 10, 50);
popMatrix();
fill(255, 255, 0, 220);
//*************************
pushMatrix();
translate(-5, 20, 74);
rotateX(PI/2);
drawCylinder(5, 5, 2, 50);
popMatrix();
pushMatrix();
translate(-5, -20, 74);
rotateX(PI/2);
drawCylinder(5, 5, 2, 50);
popMatrix();
pushMatrix();
translate(-5, 20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
pushMatrix();
translate(-5, -20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
pushMatrix();
translate(-1, 20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
pushMatrix();
translate(-1, -20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
fill(100, 100, 0, 220);
beginShape(QUADS);
vertex(10, 12, 75.1);
vertex(10, -12, 75.1);
vertex(-10, -12, 75.1);
vertex(-10, 12, 75.1);
endShape();
popMatrix();
import processing.serial.*;
Serial port;
int directionn = 0;
void setup() {
println(Serial.list());
String portName = Serial.list()[2];
port = new Serial(this, portName, 9600);
}
void Draw() {}
void serialEvent(Serial port) {
interval = millis();
if (port.available() > 0) {
directionn = port.read() - 90;
}
}
//*******ROTATE GEOMETRY ALONG AXISES IN DEGREES*****
void rotatedegree(float x, float y, float z) {
x=x*PI/180;
y=y*PI/180;
z=z*PI/180;
rotateX(x);
rotateY(y);
rotateZ(z);
}
//*******************ROAD MOVEMENT ********************
pushMatrix();
fill(20, 220, 220, 220);
translate(width / 2+50, height / 2);
rotatedegree(45, 0, 0);
for (int i=0; i<17; i++)
rct(0, xPos+i*250, -70, 10, 100);
rct(130, 0, -70, 6, 2000);
rct(-130, 0, -70, 6, 2000);
xPos=xPos+5;
if (xPos>width+20)
{
xPos=-width*6;
}
popMatrix();
This is my final code.
import processing.serial.*;
import processing.opengl.*;
import toxi.geom.*;
import toxi.processing.*;
ToxiclibsSupport gfx;
int xPos=0;
int xDir=1;
Serial port;
int synced = 0;
int interval = 0;
int directionn=0;
void setup() {
// 300px square viewport using OpenGL rendering
size(300, 300, OPENGL);
gfx = new ToxiclibsSupport(this);
// setup lights and antialiasing
lights();
smooth();
directionalLight(225, 255, 225, 100, -100, 100);
println(Serial.list());
String portName = Serial.list()[2];
port = new Serial(this, portName, 9600);
}
void draw() {
background(250);
//*******************ROAD MOVEMENT ********************
pushMatrix();
fill(20, 220, 220, 220);
translate(width / 2+50, height / 2);
rotatedegree(45, 0, 0);
for (int i=0; i<17; i++)
rct(0, xPos+i*250, -70, 10, 100);
rct(130, 0, -70, 6, 2000);
rct(-130, 0, -70, 6, 2000);
xPos=xPos+5;
if (xPos>width+20)
{
xPos=-width*6;
}
popMatrix();
//*****************************************************
// translate everything to the middle of the viewport
pushMatrix();
translate(width / 2, height / 2);
//**********ROTATE THE CAR WITH DIRECTION VALUE********
rotatedegree(130, directionn, 270); //( ver , hor , axis )
println(directionn);
//*****************************************************
//**********DRAW THE CAR*******************************
fill(255, 0, 0, 220);
noStroke();
box(30, 60, 150);
pushMatrix();
translate(-25, 0.0);
box(25, 60, 55);
popMatrix();
beginShape(TRIANGLES);
vertex(-15, 30, 40);
vertex(-15, 30, 27.5);
vertex(-40, 30, 27.5);
vertex(-15, -30, 40);
vertex(-15, -30, 27.5);
vertex(-40, -30, 27.5);
vertex(-15, 30, -40);
vertex(-15, 30, -27.5);
vertex(-40, 30, -27.5);
vertex(-15, -30, -40);
vertex(-15, -30, -27.5);
vertex(-40, -30, -27.5);
endShape();
beginShape(QUADS);
vertex(-15, 30, 40);
vertex(-40, 30, 27.5);
vertex(-40, -30, 27.5);
vertex(-15, -30, 40);
vertex(-15, 30, -40);
vertex(-40, 30, -27.5);
vertex(-40, -30, -27.5);
vertex(-15, -30, -40);
endShape();
beginShape(QUADS);
vertex(-15, 30, 40);
vertex(-40, 30, 27.5);
vertex(-40, -30, 27.5);
vertex(-15, -30, 40);
vertex(-15, 30, -40);
vertex(-40, 30, -27.5);
vertex(-40, -30, -27.5);
vertex(-15, -30, -40);
endShape();
beginShape(QUADS);
vertex(-40, 30, 27.5);
vertex(-40, -30, 27.5);
vertex(-40, -30, -27.5);
vertex(-40, 30, -27.5);
endShape();
fill(0, 0, 0, 220);
//***************************************************
beginShape(QUADS);
vertex(-15, 30.5, -35);
vertex(-15, 30.5, -2.5);
vertex(-35, 30.5, -2.5);
vertex(-35, 30.5, -22.5);
endShape();
beginShape(QUADS);
vertex(-15, 30.5, 35);
vertex(-15, 30.5, 2.5);
vertex(-35, 30.5, 2.5);
vertex(-35, 30.5, 22.5);
endShape();
beginShape(QUADS);
vertex(-15, -30.5, -35);
vertex(-15, -30.5, -2.5);
vertex(-35, -30.5, -2.5);
vertex(-35, -30.5, -22.5);
endShape();
beginShape(QUADS);
vertex(-15, -30.5, 35);
vertex(-15, -30.5, 2.5);
vertex(-35, -30.5, 2.5);
vertex(-35, -30.5, 22.5);
endShape();
beginShape(QUADS);
vertex(-15.5, -25, 39.99);
vertex(-15.5, 25, 39.99);
vertex(-36, 25, 30.5);
vertex(-36, -25, 30.5);
endShape();
beginShape(QUADS);
vertex(-15.5, -25, -39.99);
vertex(-15.5, 25, -39.99);
vertex(-36, 25, -30.5);
vertex(-36, -25, -30.5);
endShape();
//************************
pushMatrix();
translate(15, 21, 45);
drawCylinder(15, 15, 10, 50);
popMatrix();
pushMatrix();
translate(15, -31, 45);
drawCylinder(15, 15, 10, 50);
popMatrix();
pushMatrix();
translate(15, 21, -45);
drawCylinder(15, 15, 10, 50);
popMatrix();
pushMatrix();
translate(15, -31, -45);
drawCylinder(15, 15, 10, 50);
popMatrix();
fill(255, 255, 0, 220);
//*************************
pushMatrix();
translate(-5, 20, 74);
rotateX(PI/2);
drawCylinder(5, 5, 2, 50);
popMatrix();
pushMatrix();
translate(-5, -20, 74);
rotateX(PI/2);
drawCylinder(5, 5, 2, 50);
popMatrix();
pushMatrix();
translate(-5, 20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
pushMatrix();
translate(-5, -20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
pushMatrix();
translate(-1, 20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
pushMatrix();
translate(-1, -20, -76);
rotateX(PI/2);
drawCylinder(2, 2, 2, 50);
popMatrix();
fill(100, 100, 0, 220);
beginShape(QUADS);
vertex(10, 12, 75.1);
vertex(10, -12, 75.1);
vertex(-10, -12, 75.1);
vertex(-10, 12, 75.1);
endShape();
popMatrix();
}
//*****************SERIAL READ DATA******************
void serialEvent(Serial port) {
interval = millis();
if (port.available() > 0) {
directionn = port.read() -90;
}
}
//*******ROTATE GEOMETRY ALONG AXISES IN DEGREES*****
void rotatedegree(float x, float y, float z) {
x=x*PI/180;
y=y*PI/180;
z=z*PI/180;
rotateX(x);
rotateY(y);
rotateZ(z);
}
//*****************DRAW CYLINDER*********************
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
// If it is not a cone, draw the circular bottom cap
if (bottomRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, tall, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}
//*****************DRAW RECTANGLE**********************
void rct(float x_pos, float y_pos, float z_pos, float leng, float wid) {
pushMatrix();
translate(x_pos, y_pos, z_pos);
beginShape(QUADS);
vertex(-leng/2, -wid/2);
vertex(-leng/2, wid/2);
vertex(leng/2, wid/2);
vertex(leng/2, -wid/2);
endShape();
popMatrix();
}
the serialEvent function update the variable directionn everytime it receives a serial data. the rotatedegree(130, directionn, 270); function updates the rotation of all the objects commming below this command with the value of directionn.So in effect when we rotate the scrol wheel it sends the value to the application throw the serial interface. And the application updates this value as the rotation of the 3d model .
For the arduino code I just modified my Input week code
Arduino Code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(1, 0); // RX, TX
#define SEND_PIN 5 //PORTA // ARDUINO 5
#define REC2_PIN 2 //PORTA // ARDUINO 2
#define REC1_PIN 3 //PORTA // ARDUINO 3
#define REC3_PIN 4 //PORTA // ARDUINO 4
#define LEDL_PIN 7 //PORTA // ARDUINO 7
#define LEDR_PIN 2 //PORTB // ARDUINO 8
int rec_pin_mean[5] = {0, 0, 12, 8, 10};
int rec_pin_value[5];
int rec_pin_pastv[5];
int calib_time = 0;
int count = 0;
int count_past = 0;
long led_ontime = 0;
int loopTimingFactor = 310;
int CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000;
void setup()
{
mySerial.begin(9600);
pinMode(SEND_PIN, OUTPUT);
pinMode(REC1_PIN, INPUT);
pinMode(REC2_PIN, INPUT);
pinMode(REC3_PIN, INPUT);
digitalWrite(SEND_PIN, LOW);
DDRA |= (1 << LEDL_PIN);
DDRB |= (1 << LEDR_PIN);
PORTA |= (1 << LEDL_PIN);
PORTB |= (1 << LEDR_PIN);
delay(1000);
PORTA &= ~(1 << LEDL_PIN);
PORTB &= ~(1 << LEDR_PIN);
calib_time = millis();
calib();
}
void loop()
{
calib();
cs_read_value();
rotation();
}
void rotation() {
if ((rec_pin_value[REC1_PIN] != 0) || (rec_pin_value[REC2_PIN] != 0) || (rec_pin_value[REC3_PIN] != 0)) {
if ((rec_pin_pastv[REC2_PIN] != rec_pin_value[REC2_PIN]) || (rec_pin_pastv[REC1_PIN] != rec_pin_value[REC1_PIN]) ||
(rec_pin_pastv[REC3_PIN] != rec_pin_value[REC3_PIN])) {
if (((rec_pin_pastv[REC1_PIN] == 1) && (rec_pin_value[REC2_PIN] == 1)) || ((rec_pin_pastv[REC2_PIN] == 1) &&
(rec_pin_value[REC3_PIN] == 1)) || ((rec_pin_pastv[REC3_PIN] == 1) && (rec_pin_value[REC1_PIN] == 1))) {
count++;
PORTB |= (1 << LEDR_PIN);
PORTA &= ~(1 << LEDL_PIN);
led_ontime = millis();
mySerial.write(count);
}
if (((rec_pin_pastv[REC3_PIN] == 1) && (rec_pin_value[REC2_PIN] == 1)) || ((rec_pin_pastv[REC2_PIN] == 1)
&& (rec_pin_value[REC1_PIN] == 1)) || ((rec_pin_pastv[REC1_PIN] == 1) && (rec_pin_value[REC3_PIN] == 1))) {
count--;
PORTA |= (1 << LEDL_PIN);
PORTB &= ~(1 << LEDR_PIN);
led_ontime = millis();
mySerial.write(count);
}
}
}
}
void cs_read_value() {
rec_pin_pastv[REC1_PIN] = rec_pin_value[REC1_PIN];
rec_pin_pastv[REC2_PIN] = rec_pin_value[REC2_PIN];
rec_pin_pastv[REC3_PIN] = rec_pin_value[REC3_PIN];
rec_pin_value[REC1_PIN] = ((cs_mean(REC1_PIN) - rec_pin_mean[REC1_PIN]) > 3 ) ? 1 : 0;
rec_pin_value[REC2_PIN] = ((cs_mean(REC2_PIN) - rec_pin_mean[REC2_PIN]) > 3 ) ? 1 : 0;
rec_pin_value[REC3_PIN] = ((cs_mean(REC3_PIN) - rec_pin_mean[REC3_PIN]) > 3 ) ? 1 : 0;
}
void calib() {
if ((millis() - calib_time) < 5000) {
cs_calib(REC1_PIN);
cs_calib(REC2_PIN);
cs_calib(REC3_PIN);
calib_time = millis();
}
if ((millis() - led_ontime) > 1000) {
PORTA &= ~(1 << LEDL_PIN);
PORTB &= ~(1 << LEDR_PIN);
}
}
void cs_calib(uint8_t rec_pin) {
if (abs(rec_pin_mean[rec_pin] - cs_read_cycle(rec_pin)) < 3) {
cs_mean(rec_pin);
}
}
int cs_mean(uint8_t rec_pin) {
int mean = 0;
for (uint8_t i = 0; i < 30; i++) {
mean += cs_read_cycle(rec_pin);
}
return (mean / 30);
}
int cs_read_cycle(uint8_t rec_pin) {
DDRA |= (1 << SEND_PIN); // SEND PIN OUTPUT
PORTA &= ~(1 << SEND_PIN); // SEND PIN 0
DDRA |= (1 << rec_pin); // REC PIN OUTPUT
PORTA &= ~(1 << rec_pin); // REC PIN 0
delayMicroseconds(10);
DDRA &= ~(1 << rec_pin); // REC PIN INPUT
PORTA |= (1 << SEND_PIN); // SEND PIN 1
int cycles = CS_Timeout_Millis;
for (int i = 0; i < cycles; i++) {
if (PINA & (1 << rec_pin)) {
cycles = i;
break;
}
}
PORTA |= (1 << rec_pin);
DDRA |= (1 << rec_pin);
PORTA |= (1 << rec_pin);
DDRA &= ~(1 << rec_pin);
PORTA &= ~(1 << SEND_PIN);
return (cycles);
}
I just changed the Serial.println to Serial.write
The Serial.println command sends the data as human readable ascii values .Since I needed the data itself I choose Serial.write it sends the binery data directly.