Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
DRAW ME - FINAL TECHNICAL CODE
Enna Kim, Mika Hirata, Vivian Wong
Professor Kate Hartman
DIGF 2004 - Atelier I
September 27, 2017
This sketch includes all of the technical code which controls
Arduino Firmata through Processing. It allows the user to interact
with a touch textile grid connected to two separate capacitive sensors,
one for X, and one for Y. The user can control where certain features appear
when interacting with different locations on the grid. In addition, a
pressure-sensor is used to control the shade of the colour on screen, mimicking
pen-pressure sensitivity used in drawing tablets.
This current sketch only includes technical code, as a result it can only
display eclipses as it's output to test the functionality. When combined with
"DRAW ME Visual Code," the user can control the on-screen position of a variety
of interesting and unique visual outputs.
Note: This sketch can only run after standardFirmata has been uploaded to the Arduino
through the Arduino IDE once. If run on a different computer from the original, please
use Processing File>Examples>ArduinoInput to identify which port the Arduino is connected
to. Then replace "[1]" with the correct value that corresponds to the port.
REFERENCES:
https://processing.org/reference/map_.html
https://processing.org/examples/map.html
http://playground.arduino.cc/Interfacing/Processing
https://processing.org/examples/mousepress.html
http://processingjs.org/reference/for_/
http://resistor.cherryjourney.pt/ used to identify the resistance of resistors
https://github.com/plusea/CODE/tree/master/EXAMPLE%20CODE/Resistive%20Sensors/LOCATION
https://www.youtube.com/watch?v=PAViBKCWrlg the guy talking about the matrix on the fabric
http://www.instructables.com/id/Analog-Fabric-Joypad/ analog fabric joypad (our first insipration)
https://www.youtube.com/watch?v=vuu3XORygbY grid made with conductive thread
https://www.youtube.com/watch?v=oecwY5hkXzw grid made with conductive tape(copper tape)
*/
// import all necessary libraries
import processing.serial.*;
import cc.arduino.*;
import org.firmata.*;
Arduino a1; // initialize Arduino as a1
int[] pin = new int[12]; // create new pin array with 12 items
int drawPosX; // initialize integer variable for drawPosX
int drawPosY; // initialize integer variable for drawPosY
int sensor1 = 2; // set sensor1 as analog pin 2
Serial myPort; // Name serial port:
// set up size of sketch and background
void setup() {
size(800, 800); // set size of sketch to be 800 by 800 pixels
background(0); // set background colour to black
a1 = new Arduino (this, Arduino.list()[1], 57600); // create new item a1 for Arduino, set to port [1]
// for integer i equal to 2, i less than 12, increment i by one
for (int i = 2; i < 12; i++) {
pin[i] = i; // set pin[i] equal to the value of i
a1.pinMode(pin[i], Arduino.INPUT); // set pin[i] as input (Pins 2-12 set as input)
println(pin[i]); // print pin[i] to test if forloop worked
}
printArray(Serial.list()); // list all the available serial ports
}
void draw() {
checkPositionAll(); // call checkPositionAll function
colourChange(); // call colourChange function
//drawScreen2(); // extra mechanic used with touchpad/mouse for more interactions
}
// Function which combines checkPosition functions for X and Y axis and drawScreen
void checkPositionAll() {
checkPositionX(); // call checkPositionX function
checkPositionY(); // call checkPositionY function
drawScreen(); // call drawScreen function
}
// Function which checks position of X-axis with capacitive sensor X
void checkPositionX() {
// for integer j equal to 2, j less than 7, increment j by 1
for (int j = 2; j < 7; j++) {
pin[j] = j; // set pin[j] equal to j
if (a1.digitalRead(pin[j]) == Arduino.LOW) { // check if value of checked pin is LOW
drawPosX = pin[j-2]; // set drawPosX equal to j-2
println("Pin " + pin[j-2] + " is ON" + " drawPosX x-axis: " + drawPosX); // print which pin is ON
} else {
println("Pin " + pin[j-2] + " is OFF"); // print which pin is off
}
}
}
// Function which checks position of X-axis with capacitive sensor Y
void checkPositionY() {
// for integer j equal to 7, j less than 11, increment j by 11
for (int j = 7; j < 11; j++) {
pin[j] = j; // set pin[j] equal to j
if (a1.digitalRead(pin[j]) == Arduino.LOW) { // check if value of checked pin is LOW
drawPosY = pin[j-2]; // set drawPosX equal to j-2
println("Pin " + pin[j-2] + " is ON" + " drawPosX y-axis: " + drawPosY); // print which pin is ON
} else {
println("Pin " + pin[j-2] + " is OFF"); // print which pin is off
}
}
}
// Function for pressure sensor colour-changing mechanic
// Changes from light green to navy blue
void colourChange () {
float val = a1.analogRead(sensor1); // set float value equal to analog value of sensor1 (textile pressure sensor)
float valA = map(val, 500, 1000, 75, 255); // map value to value A
float valB = map(val, 500, 1000, 10, 25); // map value to value B
// if val is less than or equal to 800
if (val <= 800) {
fill(valA*5, valA*10, valB*20, valB*10); // change fill colour to specified values relevant to ValA and ValB
rect(100, 150, 200, 200); // draw a 200 by 200 rectangle at 100 px right, 200 px down
}
//println(int(val), int(valA), int(valB)); // used to check values of valA and valB
// drawing function, currently with mouse, replace with pressure sensor
}
// Function for controlling the on-screen drawing process
void drawScreen() {
noStroke(); // remove stroke
ellipse(drawPosX*100+200, drawPosY*80, 20, 20); // draw an ellipse depending on touch sensor location detected
}
// Function for drawing on screen with mouse and pressure colour changing mechanic
// used for testing
void drawScreen2 () {
if (mousePressed) { // if mouse is pressed
noStroke(); // remove stroke
ellipse(mouseX, mouseY, 20, 20); // draw ellipse at position of mouse
}
}
/*
DRAW ME - FINAL TECHNICAL CODE
Enna Kim, Mika Hirata, Vivian Wong
Professor Kate Hartman
DIGF 2004 - Atelier I
September 27, 2017
This sketch includes all of the technical code which controls
Arduino Firmata through Processing. It allows the user to interact
with a touch textile grid connected to two separate capacitive sensors,
one for X, and one for Y. The user can control where certain features appear
when interacting with different locations on the grid. In addition, a
pressure-sensor is used to control the shade of the colour on screen, mimicking
pen-pressure sensitivity used in drawing tablets.
This current sketch only includes technical code, as a result it can only
display eclipses as it's output to test the functionality. When combined with
"DRAW ME Visual Code," the user can control the on-screen position of a variety
of interesting and unique visual outputs.
Note: This sketch can only run after standardFirmata has been uploaded to the Arduino
through the Arduino IDE once. If run on a different computer from the original, please
use Processing File>Examples>ArduinoInput to identify which port the Arduino is connected
to. Then replace "[1]" with the correct value that corresponds to the port.
REFERENCES:
https://processing.org/reference/map_.html
https://processing.org/examples/map.html
http://playground.arduino.cc/Interfacing/Processing
https://processing.org/examples/mousepress.html
http://processingjs.org/reference/for_/
http://resistor.cherryjourney.pt/ used to identify the resistance of resistors
https://github.com/plusea/CODE/tree/master/EXAMPLE%20CODE/Resistive%20Sensors/LOCATION
https://www.youtube.com/watch?v=PAViBKCWrlg the guy talking about the matrix on the fabric
http://www.instructables.com/id/Analog-Fabric-Joypad/ analog fabric joypad (our first insipration)
https://www.youtube.com/watch?v=vuu3XORygbY grid made with conductive thread
https://www.youtube.com/watch?v=oecwY5hkXzw grid made with conductive tape(copper tape)
*/
// import all necessary libraries
import processing.serial.*;
import cc.arduino.*;
import org.firmata.*;
Arduino a1; // initialize Arduino as a1
int[] pin = new int[12]; // create new pin array with 12 items
int drawPosX; // initialize integer variable for drawPosX
int drawPosY; // initialize integer variable for drawPosY
int sensor1 = 2; // set sensor1 as analog pin 2
Serial myPort; // Name serial port:
// set up size of sketch and background
void setup() {
size(800, 800); // set size of sketch to be 800 by 800 pixels
background(0); // set background colour to black
a1 = new Arduino (this, Arduino.list()[1], 57600); // create new item a1 for Arduino, set to port [1]
// for integer i equal to 2, i less than 12, increment i by one
for (int i = 2; i < 12; i++) {
pin[i] = i; // set pin[i] equal to the value of i
a1.pinMode(pin[i], Arduino.INPUT); // set pin[i] as input (Pins 2-12 set as input)
println(pin[i]); // print pin[i] to test if forloop worked
}
printArray(Serial.list()); // list all the available serial ports
}
void draw() {
checkPositionAll(); // call checkPositionAll function
colourChange(); // call colourChange function
//drawScreen2(); // extra mechanic used with touchpad/mouse for more interactions
}
// Function which combines checkPosition functions for X and Y axis and drawScreen
void checkPositionAll() {
checkPositionX(); // call checkPositionX function
checkPositionY(); // call checkPositionY function
drawScreen(); // call drawScreen function
}
// Function which checks position of X-axis with capacitive sensor X
void checkPositionX() {
// for integer j equal to 2, j less than 7, increment j by 1
for (int j = 2; j < 7; j++) {
pin[j] = j; // set pin[j] equal to j
if (a1.digitalRead(pin[j]) == Arduino.LOW) { // check if value of checked pin is LOW
drawPosX = pin[j-2]; // set drawPosX equal to j-2
println("Pin " + pin[j-2] + " is ON" + " drawPosX x-axis: " + drawPosX); // print which pin is ON
} else {
println("Pin " + pin[j-2] + " is OFF"); // print which pin is off
}
}
}
// Function which checks position of X-axis with capacitive sensor Y
void checkPositionY() {
// for integer j equal to 7, j less than 11, increment j by 11
for (int j = 7; j < 11; j++) {
pin[j] = j; // set pin[j] equal to j
if (a1.digitalRead(pin[j]) == Arduino.LOW) { // check if value of checked pin is LOW
drawPosY = pin[j-2]; // set drawPosX equal to j-2
println("Pin " + pin[j-2] + " is ON" + " drawPosX y-axis: " + drawPosY); // print which pin is ON
} else {
println("Pin " + pin[j-2] + " is OFF"); // print which pin is off
}
}
}
// Function for pressure sensor colour-changing mechanic
// Changes from light green to navy blue
void colourChange () {
float val = a1.analogRead(sensor1); // set float value equal to analog value of sensor1 (textile pressure sensor)
float valA = map(val, 500, 1000, 75, 255); // map value to value A
float valB = map(val, 500, 1000, 10, 25); // map value to value B
// if val is less than or equal to 800
if (val <= 800) {
fill(valA*5, valA*10, valB*20, valB*10); // change fill colour to specified values relevant to ValA and ValB
rect(100, 150, 200, 200); // draw a 200 by 200 rectangle at 100 px right, 200 px down
}
//println(int(val), int(valA), int(valB)); // used to check values of valA and valB
// drawing function, currently with mouse, replace with pressure sensor
}
// Function for controlling the on-screen drawing process
void drawScreen() {
noStroke(); // remove stroke
ellipse(drawPosX*100+200, drawPosY*80, 20, 20); // draw an ellipse depending on touch sensor location detected
}
// Function for drawing on screen with mouse and pressure colour changing mechanic
// used for testing
void drawScreen2 () {
if (mousePressed) { // if mouse is pressed
noStroke(); // remove stroke
ellipse(mouseX, mouseY, 20, 20); // draw ellipse at position of mouse
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment