Pot of Gold

For the programming part of the lab, I created a visualization which starts with a circle at the corner of the screen and a rectangle in the middle. As force is exerted on the FSR, the circle on the left corner starts travelling diagonally across the screen and the edges of the rectangle start rounding. At a point, the circle hides behind the rectangle as if it were a coin that went into a pot/jar.

This inspired mechanical part of my lab today. I used a pot to make contact with the FSR. Then I used a small ziploc bag full of coins to exert pressure on the FSR. The pressure could be measured by the number of quarters.

Arduino Sketch:

/* FSR testing sketch.

Connect one end of FSR to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground

For more information see www.ladyada.net/learn/sensors/fsr.html */

int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 13; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int LEDbrightness;

void setup(void) {
Serial.begin(9600); // We’ll send debugging information via the Serial monitor
pinMode(LEDpin, OUTPUT);
}

void loop(void) {
fsrReading = analogRead(fsrAnalogPin);
//Serial.print(“Analog reading = “);
//Serial.println(fsrReading);

// we’ll need to change the range from the analog reading (0-1023) down to the range
// used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
// LED gets brighter the harder you press
analogWrite(LEDpin, LEDbrightness);

delay(100);
Serial.println(fsrReading);
}

Processing Sketch:

/* PROCESSING SKETCH
* Arduino Ball Paint
* (Arduino Ball, modified 2008)
* ———————-
*
* Draw a ball on the screen whose size is
* determined by serial input from Arduino.
*
* Created 21 September 2016
* Noura Howell
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = “COM3″;
Serial port;
String buf=””;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

void setup() {
size(700,700);
frameRate(10);
smooth();
background(40,40,40);
//noStroke();
port = new Serial(this, portname, 9600);
}

void draw() {
// erase the screen
background(40, 40, 40);

// draw the ball
fill(255, 255, 255);
//ellipse(150,150,serialVal,serialVal);
ellipse(serialVal,serialVal,150,150);
rect(200, 300, 300, 300, serialVal,serialVal, serialVal, serialVal);
}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println(“val=”+serialVal);
buf = “”;
}
}20160927_23281420160927_212835

 

Fit your backpack

Components:

  • 1 pot
  • 1 FSR
  • 1x 10KΩ resistor
  • 1x 220Ω resistor
  • jumper wires
  • USB cable
  • computer
  • Arduino Uno
  • Breadboard

Description

I created a program that lets you know if the weight of your backpack is well distributed among your two shoulders. In theory I should use  two FSR, one in each strap. However, since I only have one I am simulating one of them by using a POT.

The two analog signals are acquired by using the Arduino’s ADC, and then they are compared. The result of the comparison is sent to the computer via serial communication, and this information is used to show a graphical depiction of the situation.

Arduino code


int sensorF = A0; // select the input pin for the FSR
int sensorP = A1; // select the input pin for the POT

void setup() {
// declare the ledPin as an OUTPUT:

Serial.begin(9600); // Sets the data rate in bits per second (baud) for serial data transmission.
Serial.println(“WELCOME to the Sensor lab!”);
Serial.print(‘n’);
// analogWrite(redPin, 0);
// analogWrite(bluePin, 0);
// analogWrite(greenPin, 0);
}

void loop() {
int PotValue=0;
int FSRValue=0;
int x=0;
// read the values from the sensors:
PotValue = analogRead(sensorP);
FSRValue = analogRead(sensorF);
x = PotValue – FSRValue;

if (x > -265 && x < 256)
//Serial.println(“Igual”);
Serial.println(0);
else if (x > -265)
//Serial.println(“Izquierda”);
Serial.println(-1);
else
//Serial.println(“Derecha”);
Serial.println(1);

/* Serial.println(FSRValue);
Serial.print(“POT: “);
Serial.println(PotValue);
delay(1000); */
}

Processing code


import processing.serial.*; // Import the library for the serial communication. Serial library reads and writes data to and from external devices one byte at a time.
String portname = “COM4”; // or “COM5” //Providing the name of the serial port.
Serial port; //Creating an object (called “Port”) from the class “Serial”.
String buf=””; //Declare and initialize the object “buff” from the class “String”
int cr = 13; // ASCII carriage return == 13. A carriage return means moving the cursor to the beginning of the line. The code is r
int lf = 10; // ASCII linefeed == 10. Line feed: moving one line forward. The code is n.
int serialVal = 0; //declaring and initialize the variable “serialVal” where we will store the data read from the serial
PShape bot;
void setup() {
size(1024,1024); //Size of window
bot = loadShape(“pp.svg”);
port = new Serial(this, portname, 9600);
noStroke();
}
void draw(){
background(240,240,240);
shape(bot, 400, 200); // Draw at coordinate (280, 40) at the default size
textSize(48);
fill(36, 148, 245);
text(“Backpack FIT”, 350, 100);
if (serialVal==0)
{
textSize(24);
fill(8, 193, 30);
text(“Your back is safe. You are good to go”, 300, 900);
triangle(590, 300, 530, 300, 560, 350);
triangle(490, 300, 430, 300, 460, 350);
}
else if (serialVal==-1)
{
textSize(24);
fill(255, 0, 0);
text(“Fix the left strap of your backpack”, 310, 900);
triangle(590, 300, 530, 300, 560, 350);
}
else
{
textSize(24);
fill(255, 0, 0);
text(“Fix the right strap of your backpack”, 310, 900);
fill(255, 0, 0);
triangle(490, 300, 430, 300, 460, 350);
}
}
// called whenever serial data arrives
void serialEvent(Serial port) { //fromn the library. Called when data is available. Use one of the read() methods to capture this data.
int c = port.read(); //Returns a number between 0 and 255 for the next byte that’s waiting in the buffer. Returns -1 if there is no byte, although this should be avoided by first cheacking available() to see if data is available.
if (c != lf && c != cr) {
buf = buf + char(c); //If the value read is not lf nor keep reading the data.
}
if (c == lf) { //When done with the reading
serialVal = int(buf); // convert the string buf to int
println(“val=”+serialVal); //debugging
buf = “”; //Reset object to nothing
}
}

Piet Mondrian and Rubber Duck

Description

I created an interactive Piet Mondrian painting using Processing, Arduino, a LED and a rubber duck.

First, when I run Processing and click on the artboard, a piece of music will be played, and the color blocks of the Piet Mondrian will appear and start dancing.

Secondly, when I squeeze the rubber duck, a duck image will appear on my Processing artboard and move up and down based on the level of pressure I put on the actual rubber duck. The LED light inside the duck will also increase based on the level of pressure. The FSR inside the rubber duck transfers the pressure into Arduio, from Arduio to the port, from the port to the Processing. In Processing, the level of pressure converts into the position of a duck image.

Components

  • 1 Arduino
  • 1 LEDs
  • 4 Resistors
  • 1 FSR
  • 1 Breadboard
  • 1 Rubber Duck
  • 1 Processing

Code

Arduino

int fsrAnalogPin = A0;
int LEDpin = 11;     
int fsrReading;      
int LEDbrightness;

void setup(void) {
  Serial.begin(9600);   
  pinMode(LEDpin, OUTPUT);
}
 
void loop(void) {
  fsrReading = analogRead(fsrAnalogPin);
  Serial.println(fsrReading);
  LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  analogWrite(LEDpin, LEDbrightness);
  delay(100);
}

Processing
PImage img; // Declare variable 'img' of type PImage

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1411"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int serialVal = 0;

Maxim maxim;
AudioPlayer player;
float power=0;
//float threshold = 0.35;// vary this until the square appears on the beats
float threshold = 0.35;// vary this until the square appears on the beats
int wait = 0;
boolean playit = false;

float x = 0;
float y = 0;

void setup() {
  size(800,450);
  frameRate(20); // this is the framerate. Tweak for performance.
  maxim = new Maxim(this);
  player = maxim.loadFile("beat1.wav");
  player.setLooping(true);
  player.setAnalysing(true);
  smooth();
  noStroke();
  port = new Serial(this, portname, 9600);

  img = loadImage("duck.png");
  
  
}

void draw() {
  background(255);
  println(wait);
  stroke(0);
  strokeWeight(6);
  line(90,0,90,500); //first line left
  line(150,0,150,500); // second line left
  line(200,0,200,80); // third line left
  line(400,300,400,380); // fourth line left
  line(460,0,460,500); // middle line
  line(520,80,520,500); // fourth line right
  line(700,380,700,500); // third line right
  line(720,0,720,80); // second line right
  line(750,0,750,380); // first line right
  
  line(0,40,150,40);//first line top
  line(460,40,750,40); // first line top, middle
  line(150,80,800,80); //second line top
  line(460,150,800,150); //third line top
  line(0,300,460,300); //left, second line bottom
  line(0,380,800,380); //left, first line bottom
  line(150,420,700,420); //middle, first line bottom

if (playit) {
    //fill(0);
  player.play(); 
  power = player.getAveragePower(); 
  rectMode(CENTER);
  noStroke();
  fill(235,215,0);
    //ellipse(mouseX,mouseY,power*500,power*500);
  rect(x+120,y+18,32+(power*70),14+(power*70)); //second yellow rect left
  rect(x+635,y+265,200+(power*70),200+(power*70));//big yellow rect
  rect(x+776,y+115,25+(power*70),40+(power*70));//first yellow rect right
  fill(185,0,0);
  rect(x+305,y+190,250+(power*180),160+(power*180)); //big red rect
  rect(x+490,y+400,30+(power*70),11+(power*70)); //small red rect, bottom
 
  fill(0,40,105);
  rect(x+43,y+417,77+(power*20),57+(power*20));//left bottom blue rect 
  rect(x+610,y+437,165+(power*20),20+(power*20));//middle bottom blue rect
  rect(x+590,y+60,242+(power*20),24+(power*20));//top  blue rect
    if (power&gt;threshold &amp;&amp; wait &lt; 0) {
    noStroke();
    fill(235,215,0);
    rect(x+43,y+19,87,37);//first yellow rect left
    fill(185,0,0);
    rect(x+153+152,y+383+18,305,36); //long red rect 
    rect(x+735,y+19,25,38); //small red rect, top
    fill(0,40,105);
    rect(x+44,y+341,88,75);//left, second bottom blue rect
    wait=4;
  }
  wait--;
 
}

  int xDuck = int(random(800));
  int yDuck = int(random(450));
  image(img, (1600-serialVal)/2, (900-serialVal)/2);
  
}

// called whenever serial data arrives
void serialEvent(Serial p) {
 int c = port.read();
 if (c != lf &amp;&amp; c != cr) {
 buf += char(c);
 }
 if (c == lf) {
 serialVal = int(buf);
 println("val="+serialVal); 
 buf = ""; 
 }
}

void mousePressed() {
    playit = !playit;
    if (playit) {

         player.play();  
    } else {   
     player.stop();    
    }
  
}

Mood Jar with FSR

For “Mood Jar with FSR” I took the frosted cup I was working on last lab and added two FSRs. I positioned one of the FSRs to be a “picking up mug” pressure sensor, and prepared it to sit, for example, beneath a cardboard hot sleeve. The other FSR I added to the surface of the mug, for reasons. I then programmed the former to react to specific sensitivities, and the latter to be a mere “on/off” switch. The video will show what I mean.

I had a considerable amount of trouble getting processing to run using the serial outputs from the arduino environment, and will need to consult with someone who has experience with the program to debug what I’m sure is a profoundly simple problem…

The final image is of the results of my processing experiments.

Materials:

1 Jar

1 Arduino Micro

Wires

Solder/Soldering Iron

2 Potentiometers

3 Resistors (220 Ohms)

3 LEDs

Hot glue gun/hot glue

Frosted Glass spray paint

2 FSRs

2 Resistors (10k Ohms)

 

Video:

https://youtu.be/uz6HTCObOHg

 

Arduino code:

int fsrOne = A0;
int fsrTwo = A1;
int potPinColor = A4; // Potentiometer output connected to analog pin 3
int potPinBlink = A5; // Second potentiometer controlling blink speed
int potValColor = 0; // Variable to store the input from the potentiometer
int potValBlink = 0; // Variable to store the input from the second potentiometer (controlling blink)
int photoCellValOne = 0;
int photoCellValTwo = 0;

// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED’s cathodes should be connected to digital GND
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11

// Program variables
int redVal = 0; // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
Serial.begin(9600);
}

// Main program
void loop()
{
potValColor = analogRead(potPinColor); // read the potentiometer value at the input pin
potValBlink = analogRead(potPinBlink);
photoCellValOne = analogRead(fsrOne);
photoCellValTwo = analogRead(fsrTwo);

if (photoCellValOne > 50)
{
analogWrite(redPin, photoCellValOne/4); // Write values to LED pins
analogWrite(grnPin, photoCellValOne/4);
analogWrite(bluPin, photoCellValOne/4);
}

else if (photoCellValTwo > 200)
{
analogWrite(redPin, 0);
analogWrite(grnPin, 0);
analogWrite(bluPin, 0);
}

else {

if (potValColor < 341) // Lowest third of the potentiometer’s range (0-340)
{
potValColor = (potValColor * 3) / 4; // Normalize to 0-255

redVal = 256 – potValColor;
grnVal = potValColor; // Green from off to full
bluVal = 1; // Blue off
}
else if (potValColor < 682) // Middle third of potentiometer’s range (341-681)
{
potValColor = ( (potValColor-341) * 3) / 4; // Normalize to 0-255

redVal = 1;
grnVal = 256 – potValColor;
bluVal = potValColor; // Blue from off to full
}
else // Upper third of potentiometer”s range (682-1023)
{
potValColor = ( (potValColor-683) * 3) / 4; // Normalize to 0-255

redVal = potValColor; // Red from off to full
grnVal = 1; // Green off
bluVal = 256 – potValColor;
};
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
delay(potValBlink);
analogWrite(redPin, redVal*0); // Write values to LED pins
analogWrite(grnPin, grnVal*0);
analogWrite(bluPin, bluVal*0);
delay(potValBlink);
};
Serial.println(photoCellValOne);
}

 

Processing Code:

/* PROCESSING SKETCH
* Arduino Ball Paint
* (Arduino Ball, modified 2008)
* ———————-
*
* Draw a ball on the screen whose size is
* determined by serial input from Arduino.
*
* Created 21 September 2016
* Noura Howell
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = “COM11″; //”/dev/cu.usbmodem1421”; // or “COM5″
Serial port;
String buf=””;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

void setup() {
size(300,300);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}

void draw() {
// erase the screen
background(40, 40, 40);

// draw the ball
fill(255, 255, 255);
ellipse(150,150,serialVal,serialVal);
}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf &amp;&amp; c != cr) {   //syntax error:expecting RPAREN, found ‘;’
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println(“val=”+serialVal);
buf = “”;
}
}

Spaceship bouncing

Description:

For this exercise I worked with a force sensor and LED light to create a processing visual. The force sensor, when pressed, turns off the LED light. The visual is of a bouncing ball, and the gravity of how much the ball is able to bounce is increased when the sensor is pressed. My diffuser is a spaceship.

My force sensor is much like “turning off” the thruster on a spaceship – when the force sensor is on, the spaceship thruster (LED light) is off, and the ball falls to the floor. When the force sensor is not pressed, the ball bounces normally and the spaceship “thrusters” are on.

I tried to have my visual design be a series of cubes that would increase based on the force sensor input, but I found that working with classes in processing is difficult, so instead I opted to edit the bouncing ball example and adjust the gravity based on sensor input.

img_1577

sep-27-2016-22-24-09-bouncing

 

Components:

  • Arduino Uno
  • Breadboard
  • 1x force sensor
  • 1x LEDs (red)
  • 1x 220Ω resistors
  • 1x 10Ω resistor
  • jumper wires
  • USB cable
  • computer

Arduino Code:


int forcePin = A0; // the cell and 10K pulldown are connected to a0
int forceReading; // the analog reading from the sensor divider
int LEDpin = 13; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}

void loop(void) {
forceReading = analogRead(forcePin);

//Serial.print("Analog reading = ");
//Serial.println(forceReading); // the raw analog reading

// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
forceReading = 1023 - forceReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(forceReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness*4);
Serial.println(forceReading);
}

Processing Code:


import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;
int serial_factor = 0;
PVector location; // Location of shape
PVector velocity; // Velocity of shape
PVector gravity; // Gravity acts at the shape's acceleration

void setup() {
size(640,640);
location = new PVector(100,100);
velocity = new PVector(1.5,2.1);
gravity = new PVector(0,0.2);
port = new Serial(this, portname, 9600);
}

void draw() {
background(0);

// Add velocity to the location.
location.add(velocity);
// Add gravity to velocity
velocity.add(gravity);

// Bounce off edges
if ((location.x &gt; width) || (location.x &lt; 0)) {
velocity.x = velocity.x * -1;
}
if (location.y &gt; height) {
// We're reducing velocity ever so slightly
// when it hits the bottom of the window
velocity.y = velocity.y * -0.95;
serial_factor = serialVal/30000;
if (serialVal &gt; 1000) {
gravity.y = .3;
}
if (serialVal &lt; 1000 ) {
gravity.y = 1.2;
}
location.y = height;
}

// Display circle at location vector
stroke(255);
strokeWeight(2);
fill(127);
ellipse(location.x,location.y,48,48);
}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf &amp;&amp; c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println("val="+serialVal);
buf = "";
}
}

Lab 4: Pots + Processing

Description:
For this lab, I wanted to focus on transferring data from multiple variables and multiple sensors from Arduino into Processing. I was less concerned with what visualization to make than how to give someone more control over it and how to allow it to have multiple input streams. Because the photocell was too sensitive for easy daylight use, I stuck with the FSR for my main sensor. I kept the LED within the circuit so that it could provide me with a backup indication of the pressure on my sensor, and then I added three pots back into the circuit. Each pot controls RGB values, and can be adjusted independently. The FSR I covered with a stress-reliever type foam ball with a slit cut into it. This allowed me to slip it over the FSR and it could act as a diffuser for the squeeze pressure on the ball. This way I can squeeze on the ball in general and still affect the FSR value. The visualization responds quickly to changes in pressure and color. (The nice thing about using RGB values instead of RGB LEDs is that we can actually get additive white onscreen!)

I also ran into some problems in the making of this visualization: Processing was not correctly reading in the final value from the serial input. After troubleshooting to determine that the problem was indeed within Processing, I added in a dummy value as a final value in my Arduino output as a quick solution. I also had an issue where I was getting a “USB device drawing too much power” from my computer. After trying a few solutions, I realized that two of my pots had shifted on the desk and were touching bare wire at one spot. After rearranging and separating them, I was able to get the circuit working again.

Here is a GIF of my animation (I am squeezing with one hand and adjusting the pots with the other hand):
lab04-molly

And my circuit:
img_1221

img_1220

Components:
– Arduino Uno
– breadboard
– 1 LED
– 1 220 ohm resistor
– 1 FSR
– 1 10 ohm resistor
– 3 potentiometers
– jumper wires

Arduino Code:


/*
 Lab04 - Sensing: Photocells and FSR
 Molly Mahar

*/

int FSRcellReading = 0;  // initialize value read from the FSR
int ledPin = 11;
int LEDbrightness = 0;
int rPot = A1;
int rVal = 0;
int gPot = A2;
int gVal = 0;
int bPot = A3;
int bVal = 0;
 
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // declare the led pin(s) as output:
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // this will map the LED brightness according to the pressure placed on the FSR:
  FSRcellReading = analogRead(A0);
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses.
  LEDbrightness = map(FSRcellReading, 0, 1023, 0, 255);
  // Also want to read in the values from the three pots, and map those to 0-255 RGB values:
  rVal = map(analogRead(rPot), 0, 1023, 0, 255);
  gVal = map(analogRead(gPot), 0, 1023, 0, 255);
  bVal = map(analogRead(bPot), 0, 1023, 0, 255);
  analogWrite(ledPin, LEDbrightness);  // PWM the LED with the pot value (divided by 4 to fit in a byte)
  //construct a message to Processing with multiple values in arduino
  //we can't just send a string from here since it only officially understands 'bytes'
  // Got help in this instance from this post: https://processing.org/discourse/beta/num_1193069410.html
  Serial.print(FSRcellReading);
  Serial.print(",");
  Serial.print(rVal);
  Serial.print(",");
  Serial.print(gVal);
  Serial.print(",");
  Serial.print(bVal);
  Serial.print(",");
  // Not sure why I'm having this problem, but Processing was somehow not reading the last value?
  // I checked in the serial monitor and it was getting sent from Arduino
  // So I'm adding a final value into the serial printout, so that all my rgb values will be read in Processing
  Serial.print(0,DEC);
  Serial.print("|");
  //Serial.println([FSRcellReading, rVal, gVal, bVal]);
  delay(100);  // pause at that level before looping through again
}

Processing Code:


/* PROCESSING SKETCH
 * Based on the original ball code by Noura Howell
 * Modified for Lab 4 by Molly Mahar
 */
import processing.serial.*;
String portname = "/dev/cu.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
//int serialVal = 0;
int ballSize = 0;
int rVal = 0;
int gVal = 0;
int bVal = 0;

void setup() {
 size(700,700);
 frameRate(10);
 smooth();
 background(40,40,40);
 noStroke();
 port = new Serial(this, portname, 9600); 
}

void draw() {
 // erase the screen
 background(40, 40, 40);
 // draw the ball
 fill(rVal, gVal, bVal);
 ellipse(350,350,ballSize,ballSize);
}

//read the message in Processing
//original version of this code (then edited) from: https://processing.org/discourse/beta/num_1193069410.html
void serialEvent(Serial myPort){
 String myString = myPort.readStringUntil(124); //the ascii value of the "|" character
 if(myString != null ){
   myString = trim(myString); //remove whitespace around our values
   int inputs[] = int(split(myString, ','));
   //now assign your values in processing
   if(inputs.length == 5){
     ballSize = inputs[0];
     rVal = inputs[1];
     gVal = inputs[2];
     bVal = inputs[3];
   }
 }
}

Pocket Tanks !

Description

 

We all (most of us) have played the popular game of Pocket Tanks. For those who dont know about it, it’s a two player turn based game where one player controls the velocity and the angle at which his/her tank will shoot so as to land a hit on the opponent’s tank. I thought of an interesting way in which pocket tanks can be played. I used a potentiometer to control the angle of the nozzle and the force sensor to decide the velocity with which one is supposed to hit the other tank. The harder you press on the force sensor , the further the shot goes. I mapped the values of the potentiometer from 0 to 180 and the values of the FSR from 0 to 100.

Materials

  • Arduino
  • 1 Force Sensitive Resistor (FSR)
  • 1 Potentiometer
  • 1 10 KΩ resistor

Code

Arduino


const int fsrPin = 0;
const int potPin = 1;

void setup()  {
    // Still need to set a baud rate, even for USB.
    Serial.begin(9600);
    // Poteniometer analogue pin is an input.
    pinMode(potPin, INPUT);
    pinMode(fsrPin, INPUT);
}

void loop()  {

    int velocity = map(analogRead(fsrPin),0,1023,0,100);
    int angle = map(analogRead(potPin),0,1023,0,180);
    Serial.print(String(velocity) +'a'+ String(angle)+' ');
    delay(50);
    
    
}

Processing


//importing processing libraries
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM5"; // or "COM5"
//defining custom font
PFont mono;
//defingin position values for the two tanks
int xpos1, ypos1, xpos2, ypos2, xpos, ypos, distance = 0, angleX, angleY;
int velocity = 50, velocity1 = 50, velocity2 = 50;      //saves velocity and angles for each person
int angle = 90, angle1 = 90, angle2 = 90;
int fuel1 = 50, fuel2 = 50, fuel = 50;
int tS = 15;                //tank size
float bS = .1;            //ball size
boolean player1Turn = true, shoot = false, gameEnd = false, loopOne = true, moveTank = false;
float ballX, ballY;
float vx, vy, grav = 9.8, t = 0;
int win1 = 0, win2 = 0;        //counts the wins for each person
int btnMoveRed = 255, btnMoveGreen = 255,btnMoveBlue = 255;      //change the color of the move button when clicked
int btnSSR = 255, btnSSG = 255,btnSSB = 255, btnSmallR = 37, btnSmallG = 116,btnSmallB = 169,btnMedB = 255, btnMedR = 255, btnMedG = 255,btnBigB = 255, btnBigR = 255, btnBigG = 255;    //change color of gamemode buttons
Serial myPort;
//Built on the base program provided by
/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/374777*@* */
//Jarod Searle
void setup() {
  size(1000, 800);
  smooth();
  do {
    xpos1 = int(random(1000));
    ypos1 = 600;
    xpos2 = int(random(1000));
    ypos2 = 600;
    distance = abs(xpos2 - xpos1);
  } while(distance < 75);
  textSize(13);
  stroke(0);
  mono = createFont("segoeui.ttf",32);
  textFont(mono);
  // List all the available serial ports:
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
  //buffering each input till the delimiter for easy read
  myPort.bufferUntil(' ');
}

void draw() {
  
  //drawing the various elements in the game
  textSize(13);
  background(236,236,236);
  fill(0);
  line(0, 600, 1000, 600);
  fill(22, 160, 133);
  ellipse(xpos1, ypos1, tS, tS);    //player 1 = red
  text("Player 1 Color", 50, 650);
  text("Fuel: " + fuel1, 50, 670);
  text("Player 1 Wins: " + win1, 700, 125);
  fill(214,69,65);
  ellipse(xpos2, ypos2, tS, tS);    //player 2 = blue
  text("Player 2 Color", 825, 650);
  text("Fuel: " + fuel2, 825, 670);
  text("Player 2 Wins: " + win2, 700, 145);
  fill(0);
  text("Velocity: " + velocity + " m/s", 440, 690);
  text("Angle: " + angle + " degrees", 440, 710);
  text("Controls: Use the potentiometer to adjust the angle, and the FSR to adject the velocity, Spacebar to shoot", 280, 730);
  text("NOTE - to hit the other player, the shot must land within their circle.", 300, 60);
  text("This means that the shot can technically go through the upper half of the other player.", 250, 80);
  fill(btnMoveRed, btnMoveGreen, btnMoveBlue);
  rect(50, 690, 150, 75);
  fill(0);
  textSize(30);
  text("MOVE", 80, 740);
  fill(btnSSR, btnSSG, btnSSB);
  rect(20, 100, 100, 50);
  fill(btnSmallR, btnSmallG, btnSmallB);
  rect(140, 100, 100, 50);
  fill(btnMedR, btnMedG, btnMedB);
  rect(260, 100, 100, 50);
  fill(btnBigR, btnBigG, btnBigB);
  rect(380, 100, 100, 50);
  fill(0);
  textSize(13);
  text("Super Small", 30, 130);
  text("Small", 170, 130);
  text("Medium", 285, 130);
  text("Big", 420, 130);
  
  
  if (player1Turn == true) {      //Player 1's turn
    if (loopOne == true) {
      velocity = velocity1;    //saves the previous turn's velocity and angle
      angle = angle1;
      xpos = xpos1;
      ypos = ypos1;
      fuel = fuel1;
    }
    loopOne = false;
    velocity1 = velocity;
    angle1 = angle;
    xpos1 = xpos;
    ypos1 = ypos;
    fuel1 = fuel;
    
    //defining the trajectory
    angleX = int(xpos - velocity * cos(angle * (3.14/180)));      //velocity * the rest will make the line longer for more power
    angleY = int(ypos - velocity * sin(angle * (3.14/180)));
    line(xpos, ypos, angleX, angleY);
    
    if (shoot == true) {
      loopOne = true;
      shot();
       
       
       textSize(30);
       //code to check if the trajectory of the tank shot hits the other tank
       if ((ballX >= xpos2 - tS / 2 && ballX <= xpos2 + tS / 2) && (ballY >= ypos2 - tS)) {
         text("PLAYER 1 WINS", 350, 300);
         gameEnd = true;
         fill(255);
         rect(420, 350, 100, 50);
         fill(0);
         text("RESET", 430, 380);
         win1 += 1;
       }
       else {
         text("CLICK TO CONTINUE", 350, 300); 
         player1Turn = false;
         noLoop();
       }
    }
  }
  
  
  else if (player1Turn == false) {        //Player 2's turn
    if (loopOne == true) {
      velocity = velocity2;
      angle = angle2;
      xpos = xpos2;
      ypos = ypos2;
      fuel = fuel2;
    }
    loopOne = false;
    velocity2 = velocity;
    angle2 = angle;
    xpos2 = xpos;
    ypos2 = ypos;
    fuel2 = fuel;
    
    angleX = int(xpos - velocity * cos(angle * (3.14/180)));
    angleY = int(ypos - velocity * sin(angle * (3.14/180)));
    line(xpos, ypos, angleX, angleY);
    if (shoot == true) {
      loopOne = true;
      shot();
       
      textSize(30);
      
      //code to check if the trajectory of the tank shot hits the other tank
      if ((ballX >= xpos1 - tS / 2 && ballX <= xpos1 + tS / 2) && (ballY >= ypos1 - tS)) {
        text("PLAYER 2 WINS", 350, 300);
        gameEnd = true;
        fill(255);
        rect(420, 350, 100, 50);
        fill(0);
        text("RESET", 430, 380);
        win2 += 1;
      }
      else {
        text("CLICK TO CONTINUE", 350, 300);
        player1Turn = true;
        noLoop();
      } 
    }
  }
  
  if (gameEnd == true) {
    noLoop();
  }
}

void shot() {                //will shoot the ball, gets rid of code up above
  do {
        vx = velocity * cos(angle * (3.14/180));
        vy = velocity * sin(angle * (3.14/180));
        t += .02;
        ballX = xpos -(vx * t);
        ballY = ypos -(vy * t - (grav/2)*t*t);
        fill(0);
        ellipse(ballX, ballY, bS, bS);
      } while (!(ballY > 600));
}

void resetButtons() {
  btnSSR = 255;
  btnSSG = 255;
  btnSSB = 255;
  btnSmallR = 255;
  btnSmallG = 255;
  btnSmallB = 255;
  btnMedR = 255;
  btnMedG = 255;
  btnMedB = 255;
  btnBigR = 255;
  btnBigG = 255;
  btnBigB = 255;
}

// called whenever serial data arrives
void serialEvent(Serial p) {
  
  while (myPort.available() > 0) {
    
    //reading the buffer until ' ' which is the delimiter
    String inString = myPort.readStringUntil(' '); 
    //the two values of the sensors are separated by 'a'
    String nums[] = inString.split("a");
    velocity = int(nums[0]);
    nums[1] = trim(nums[1]);
    angle = int(nums[1]);
    printArray(nums);
  }
 
}

void keyPressed() {
  if (key == CODED) {    
    if (keyCode == RIGHT) {
      if (moveTank == true && fuel > 0) {
        xpos += 1;
        fuel -= 1;
      }
    }
    else if (keyCode == LEFT) {      
      if (moveTank == true && fuel > 0) {
        xpos -= 1;
        fuel -= 1;
      }
    }
  }
  else if (keyCode == ' ')
    shoot = true;  
}

void mousePressed() {
  if (shoot == true && gameEnd == false) {
    shoot = false;
    t = 0;
    moveTank = false;
    loop();
    btnMoveGreen = 255;
    btnMoveRed = 255;
    btnMoveBlue = 255;
  }
  else if (gameEnd == true) {
    if (mouseX >= 420 && mouseX <= 520 && mouseY >= 350 && mouseY <= 400) {
      do {
        xpos1 = int(random(1000));
        xpos2 = int(random(1000));
        distance = abs(xpos2 - xpos1);
      } while(distance < 75);
      angle1 = 90;
      angle2 = 90;
      velocity1 = 50;
      velocity2 = 50;
      fuel1 = 50;
      fuel2 = 50;
      textSize(13);
      shoot = false;
      gameEnd = false;
      loopOne = true;
      t = 0;
      
      if (win1 > win2) 
        player1Turn = false;
      else if (win2 > win1)
        player1Turn = true;
      else {
        int x = int(random(2));
        if (x == 1) 
          player1Turn = true;
        else
          player1Turn = false;
      }
      loop();
    }
  }
  else if (mouseX >= 50 && mouseX <= 200 && mouseY >= 690 && mouseY <= 765) {    //move button
    if (moveTank == false) {
      moveTank = true;
      btnMoveGreen = 116;
    btnMoveRed = 37;
    btnMoveBlue = 169;
    }
    else {
      moveTank = false;
      btnMoveGreen = 255;
    btnMoveRed = 255;
    btnMoveBlue = 255;
    }
  }
  else if (mouseX >= 20 && mouseX <= 120 && mouseY >= 100 && mouseY <= 150) {      //super small
    resetButtons();
    btnSSR = 37;
    btnSSG = 116;
    btnSSB = 169;
    tS = 5;
  } 
  else if (mouseX >= 140 && mouseX <= 240 && mouseY >= 100 && mouseY <= 150) {
    resetButtons();
    btnSmallR = 37;
    btnSmallG = 116;
    btnSmallB = 169;
    tS = 15;
  }
  else if (mouseX >= 260 && mouseX <= 360 && mouseY >= 100 && mouseY <= 150) {
    resetButtons();
    btnMedR = 37;
    btnMedG = 116;
    btnMedB = 169;
    tS = 100;
  }
  else if (mouseX >= 380 && mouseX <= 480 && mouseY >= 100 && mouseY <= 150) {
    resetButtons();
    btnBigR = 37;
    btnBigG = 116;
    btnBigB = 169;
    tS = 200;
  }
}

fsr

game

Lab 4: FSR game input

Description

I used the force sensitive resistor as an input for a game in Processing where the player flies an airplane to dodge walls. The force on the FSR determines the height of the airplane, which travels past obstacles at a constant speed. Originally, I wanted to have a trail following the airplane to show where it had been, but to improve the feel of the game, it was reduced to very small flames behind airplane to give a sense of the movement. An LED on the Arduino correlates its brightness with the FSR force for debugging when the serial connection has trouble.

I built a force diffuser from paper towels sandwiched between two stacks of sticky notes. The sticky note stacks were approximately the right size to be pushed with a hand, and the paper towels worked well because the amount of force diffused could be easy modulated by adding or removing paper towels. The sticky note stacks could be replaced with any hard object of the same size. While diffusing force is very useful for measuring forces outside of the FSR’s range, I found that when using the FSR as an input device for a game, it was best to have as little diffusing as possible to allow a single finger to have the most control.

Materials

  • Arduino
  • 1 Force Sensitive Resistor (FSR)
  • 1 LED
  • 1 22 KΩ resistor
  • 1 10 KΩ resistor
  • 10 wires

Images

Sticky Note force diffuser
Screenshot from FSR game

Arduino Code

int forceSensorPin = A0;    // select the input pin for the rate potentiometer
int ledPin = 9;      // select the pin for the LED
int forceValue = 0;  // variable to store rate pot value
void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensors:
  forceValue = analogRead(forceSensorPin);
  
  // turn the ledPin on, convert from 0-1023 scale to 0-255
  analogWrite(ledPin, forceValue / 4);
  // stop the program for 10 milliseconds:
  delay(10);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  Serial.print(forceValue);
  Serial.println();
}

Processing Code

/* PROCESSING FORCE GAME
 * ----------------------
 *
 * Using a force input, guide an airplane
 * to avoid obstacles.
 *
 * Created 23 September 2016
 * Sam Meyer
 */
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1411"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;
int xPos = 1000;
int rectHeight = 50;
float rand;
boolean gameOver = false;

EvilRectangle er;
ArrayList<EvilRectangle> erList;
int rCounter = 0;
long iDraw = 0;
int rectRate = 60;
PlayerIcon airplane;

class PlayerIcon {
  float x;
  float y;
  float y2; // green flame
  float y3; // blue flame
  
  PlayerIcon() {
    x = 150;
    y = 0;
    y2 = 0;
    y3 = 0;
  }
  
  void draw() {
    //blue flame
    fill(0, 0, 255);
    //ellipse(x3, y3, 100, 15);
    triangle(x - 50, y3 + 8, x - 50, y3 - 8, x - 70, y3); 
    triangle(x - 50, y3 + 8, x - 50, y3 - 8, x - 48, y3); 
    // green flame
    fill(0, 128, 0);
    //ellipse(x2, y2, 100, 23);
    triangle(x - 50, y2 + 12, x - 50, y2 - 12, x - 60, y2); 
    triangle(x - 50, y2 + 12, x - 50, y2 - 12, x - 45, y2); 
    // airplane
    fill(255, 255, 255);
    ellipse(x, y, 100, 25);
    triangle(x - 10, y + 50, x - 10, y - 50, x + 30, y);
    triangle(x - 50, y + 20, x - 50, y - 20, x - 20, y);
  }
  
  void setY(float val) {
    // move y values back through the following shapes
    y3 = y2;
    y2 = y;
    y = val;
  }
}

class EvilRectangle {
  float x;
  float y;
  float w;
  float h;
  EvilRectangle(float a, float b, float c, float d) {
    x = a;
    y = b;
    w = c;
    h = d;
  }

  void draw() {
    fill(255, 0, 0);
    rect(x, y, w, h);
  }
  
  boolean checkCollision(float xC, float yC) {
    return (xC > x) && (xC < x + w) && (yC > y) && (yC < y + h);
  }
}

void setup() {
 size(1000,600);
 frameRate(30);
 smooth();
 background(40,40,40);
 noStroke();
 port = new Serial(this, portname, 9600);
 er = new EvilRectangle(xPos, 600-rectHeight, 55, rectHeight);
 erList = new ArrayList<EvilRectangle>();
 erList.add(er);
 airplane = new PlayerIcon();
}

void draw() {

 if (gameOver) {
   fill(200);
   textSize(52);
   text("Crashed!\nDistance: " + str(int(iDraw)), 100, 200);
 } else {
   // increment counters
   iDraw += 1;
   rCounter += 1;
   
   // erase the screen
   background(40, 40, 40);
     
   // draw the airplane
   airplane.setY(max(50, 600 - serialVal/2 - 50));
   airplane.draw();
   
   // check for collsions
   for (EvilRectangle er : erList) {
     if (er.checkCollision(airplane.x + 50, airplane.y) || 
         er.checkCollision(airplane.x - 50, airplane.y) ||
         er.checkCollision(airplane.x, airplane.y + 50) ||
         er.checkCollision(airplane.x, airplane.y - 50)) {
       gameOver = true;
     }
   }
  
   // draw the rectangle
   for (EvilRectangle er : erList) {
     er.draw();
     er.x -= 5;
   }
   
   // draw current level
   textSize(16);
   fill(200);
   text("Current\nlevel: " + str(61 - rectRate), 10, 40);
   
   // add new rectangles
   if (rCounter >= rectRate) {
     rCounter = 0;
     if (iDraw < 240) {
       rectHeight = 50 * int(iDraw) / 30;
     } else {
       rectHeight = int(random(50, 400));
     }
     
     // increase speed
     if (iDraw > 400 & rectRate > 25) {
       rectRate -= 1;
     }
     
     // pick rectangle direction
     rand = random(1);
     if (rand < 0.4) {
       // bottom rect
       erList.add(new EvilRectangle(xPos, 600-rectHeight, 55, rectHeight));
     } else if (rand > 0.7) {
       // top and bottom
       erList.add(new EvilRectangle(xPos, 600-rectHeight/2, 55, rectHeight/2));
       erList.add(new EvilRectangle(xPos, 0, 55, rectHeight/2));
     } else {
       // top rect
       erList.add(new EvilRectangle(xPos, 0, 55, rectHeight));
     }
     
     // clean up rectangles that have left the screen
     for (int i = erList.size() - 1; i >= 0; i--) {
       if (erList.get(i).x < -100) {
         erList.remove(i);
       }
     }
     
     // reset rCounter
     rCounter = 0;
   }
 }
}

// called whenever serial data arrives
void serialEvent(Serial p) {
 int c = port.read();
 if (c != lf && c != cr) {
 buf += char(c);
 }
 if (c == lf) {
 serialVal = int(buf);
 // println("val="+serialVal);
 buf = "";
 }
}

Lab 4: FSR Toothbrushing Indicator

Description

Brushing your teeth too lightly fails to remove plaque and debris, but brushing them too hard can harm the gums and strip tooth enamel. I used an Arduino, a force sensing resistor (FSR), and a toothbrush to indicate (roughly) appropriate force to apply when brushing your teeth. Too little pressure illuminates a green light, the right amount illuminates a blue light, and too much pressure illuminates a red light. The FSR data is also interpreted by Processing to depict either an underwhelmed, happy, or sad tooth (corresponding to too little, just right, and too much pressure, respectively).

Materials

  • Arduino UNO
  • Force sensing resistor (FSR)
  • toothbrush
  • breadboard, hookup wire, and resistors
  • LEDs
  • floral tape

Arduino Code

/*
send FSR values over a serial port
and set LED to corresponding brightness

By Leah Rosenbaum
*/

int forceSensor = A0; //input pin for the FSR
int bluePin = 11; // pins for LED output
int redPin = 9;
int greenPin = 10;

int forceValue = 0; // variable to store the value coming from the FSR

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);

Serial.begin(9600);
}

void loop() {
forceValue = analogRead(forceSensor); // read the value from the brightness sensor

//Serial.print("FSR value:");
Serial.println(forceValue);

if (0&lt;= forceValue &amp;&amp; forceValue &lt; 400) {
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
digitalWrite(redPin, LOW);
}
else if (320 &lt;= forceValue &amp;&amp; forceValue &lt; 900){
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(redPin, LOW);
}
else {
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(redPin, HIGH);
}

delay(500);
}

Processing Code

/* PROCESSING SKETCH
* Arduino Ball Paint
* (Arduino Ball, modified 2008)
* ----------------------
*
* Draw a tooth whose face corresponds
* to pressure exerted on a toothbrush
*
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1411"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;
int width = 800;
int height = 800;

void setup() {
size(800,800);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}

void draw() {
// erase the screen
background(40, 40, 40);

// draw the tooth
fill(255,255,255);
// main rectangle
rect(width*3/8, height*3/8, width/4, height/4, 60);
// the top divot
fill(40,40,40);
triangle(width*3/8 + 60, height*3/8, width*5/8 - 60, height*3/8, width/2, height*3/8 + 30);
//the roots
fill(255,255,255);
triangle(width*3/8, height*9/16, width/2, height*9/16, width*7/16, height*3/4);
triangle(width/2, height*9/16, width*5/8, height*9/16, width*9/16, height*3/4);

//according to the pressure (serial input), give a face
fill(0, 0, 0);
//eyes
ellipse(width*7/16, height*15/32, 10, 10);
ellipse(width*9/16, height*15/32, 10, 10);
//mouth, depending on pressure;
//not enough pressure (flat mouth)
if (serialVal &lt; 400) {
fill(0, 0, 0);
rect(width*7/16, height*35/64, width/8, height/64);
}
// good pressure (smile)
else if (serialVal &lt; 900) {
fill(255, 0, 0);
arc(width/2, height*35/64, width/8, height/10, 0, PI);
}
// too much pressure (frown)
else {
fill(255, 0, 0);
arc(width/2, height*37/64, width/8, height/10, PI, 2*PI);
fill(255, 255, 255);
arc(width/2, height*37/64, width/8, height/15, PI, 2*PI);
}
}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf &amp;&amp; c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println("val="+serialVal);
buf = "";
}
}

setup

too-little-pressure  thats-great  too-much-pressure

 

 

Pressure Painting (Lab 04)

Description:

It is often ill-advised for a painter to apply much tension to their brush strokes. Not only does it harm the integrity of the bristles, but it also constricts the artist’s technique and ultimately the nature of their work. For this lab, I used an FSR potentiometer and Processing to indicate when there’s too much tension applied to the brush in a brushstroke. The LEDs also blink at a heightened pace when too much pressure is applied, as to alert the artist if they’re too preoccupied looking down at what they are painting to notice the color on the screen.

Components Used:

  • 1 Arduino
  • 3 LEDs (red, green, and blue)
  • 3 Resistors (220Ω)
  • 1 Resistor (10kΩ)
  • 1 Breadboard
  • 1 FSR Potentiometer
  • 1 post-it sheet

Code:

/*
 This example is meant to read three analog sensors (potentiometers are easiest)
 and sends their values serially. The Processing and Max/MSP programs at the bottom
 take those three values and use them to change the background color of the screen.

 [Dina's addition:
 In the event of only one potentiometer being attached, all three analog inputs
 will be affected in the same way.]

 The circuit:
 * potentiometers attached to analog inputs 0, 1, and 2

 http://www.arduino.cc/en/Tutorial/VirtualColorMixer

 created 2 Dec 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Tom Igoe and Scott Fitzgerald
 modified on 24 Sept 2016
 by Dina Bseiso

 This example code is in the public domain.

 */

int sensorPin = A0; // select the input pin for the potentiometer for pulse
int sensorValue = 0; // variable to store the value coming from the sensor

const int redledPin = 9;
const int blueledPin = 10;
const int greenledPin = 11; // select the pin for the LED

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(redledPin, OUTPUT);
 pinMode(blueledPin, OUTPUT);
 pinMode(greenledPin, OUTPUT);
 Serial.begin(9600);
}

void loop() {
 Serial.print(analogRead(redledPin));
 Serial.print(",");
 Serial.print(analogRead(greenledPin));
 Serial.print(",");
 Serial.println(analogRead(blueledPin));
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 // turn the ledPin on
 digitalWrite(redledPin, HIGH);
 digitalWrite(blueledPin, HIGH);
 digitalWrite(greenledPin, HIGH); 
 // stop the program for &lt;sensorValue&gt; milliseconds:
 delay(sensorValue);
 // turn the ledPin off:
 digitalWrite(redledPin, LOW);
 digitalWrite(blueledPin, LOW);
 digitalWrite(greenledPin, LOW);
 // stop the program for for &lt;sensorValue&gt; milliseconds:
 delay(sensorValue);
}

/* PROCESSING SKETCH
 * Arduino Painting Tension
 * (from Arduino Ball, modified 2008, and then by Dina on 2016)
 * ---------------------- 
 *
 * Change the color on the screen depending on
 * the amount of pressure applied to an FSR.
 *
 * Created 24 September 2016
 * Dina Bseiso
 */
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1441"; // or "COM5"
Serial myPort;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value

int serialVal = 0;
int serialValAmplified = 0;

void setup() {
 size(300,300);
 frameRate(10);
 smooth();
 ellipseMode(CENTER);
 background(40,40,40);
 noStroke();
 myPort = new Serial(this, portname, 9600); 
}

void draw() {
 // set the background color with the color values:
 background(redValue, greenValue, blueValue);
}

// called whenever serial data arrives
void serialEvent(Serial myPort) {
 // get the ASCII string:
 String inString = myPort.readStringUntil('\n');
 println(inString);

 if (inString != null) {
 // trim off any whitespace:
 inString = trim(inString);
 // split the string on the commas and convert the
 // resulting substrings into an integer array:
 float[] colors = float(split(inString, ","));
 // if the array has at least three elements, you know
 // you got the whole thing. Put the numbers in the
 // color variables:
 if (colors.length &gt;=3) {
 //println(colors);
 // map them to the range 0-255:
 if (colors[0] &gt;= 700) {
 blueValue = map(colors[2], 0, 1023, 150, 255);
 greenValue = map(colors[1], 1023, 0, 75, 150);
 redValue = map(colors[0], 1023, 0, 0, 75);
 }
 else if (colors[0] &gt;= 300) {
 greenValue = map(colors[1], 1023, 0, 150, 255);
 blueValue = map(colors[2], 0, 1023, 75, 150);
 redValue = map(colors[0], 1023, 0, 75, 150);
 }
 else if (colors[0] &gt;= 0) {
 redValue = map(colors[0], 1023, 0, 150, 255);
 greenValue = map(colors[1], 0, 1023, 75, 175);
 blueValue = map(colors[2], 0, 1023, 0, 75);
 }
 }
 }
}

img_6255

Pressure Painting