Course Description

Art B2050, Fall 2013, DIAP at CCNY
A survey of modern electromechanical construction. Lessons interweave hardware, firmware, software and networking. Specific focus on paper and cardboard prototyping.

Wednesday, October 16, 2013

Week 8, 10/23: Processing

Today we'll look at the art history of mechanical systems. Here's a link to the image gallery, here's a link to the reading, here's a link to the circuit symbol index. We'll also talk about how the four programming paradigms— imperative, functional, object-oriented, logic— and how they're all encapsulated in the familiar line, Serial.println("hello world");

The goal of today's workshop is to get Arduino communicating with Processing. It's easier to go from Processing to Arduino than the reverse, so we'll start there. The one trick to getting this working is to select the correct Serial port in Processing. When you first launch the Processing code, it will list the Serial ports in the debugging window. One will match the board ID# you selected in the Arduino/Tools menu. Whatever number in the list matches your board ID# will be substituted for 0 in the line port = new Serial(this, Serial.list()[0], 9600);


Arduino Code
const int ledPin = 13; 
int incomingByte;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if(Serial.available() > 0){
    incomingByte = Serial.read();
    if(incomingByte == 'H'){
      digitalWrite(ledPin, HIGH);
    } 
    if(incomingByte == 'L'){
      digitalWrite(ledPin, LOW);
    }
  }
}

Processing Code
import processing.serial.*; 
float boxX;
float boxY;
int boxSize = 20;
boolean mouseOverBox = false;
Serial port; 

void setup(){
  size(200, 200);
  boxX = width/2.0;
  boxY = height/2.0;
  rectMode(RADIUS); 
  println(Serial.list());  // find your Arduino in log list
  port = new Serial(this, Serial.list()[0], 9600);
}

void draw(){ 
  background(0);
  if (mouseX > boxX-boxSize && mouseX < boxX+boxSize && 
    mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
    mouseOverBox = true;  
    stroke(255); 
    fill(153);
    port.write('H');
  } 
  else {
    stroke(153);
    fill(153);
    port.write('L');      
    mouseOverBox = false;
  }

  rect(boxX, boxY, boxSize, boxSize);
}

2 comments:

  1. import processing.serial.*;
    Serial port;

    void setup(){
    size(200, 200);
    println(Serial.list()); // find your Arduino in log list
    port = new Serial(this, Serial.list()[8], 9600);
    }

    void draw(){
    writeToArduino();
    delay(1000);
    }

    void writeToArduino(){
    int rBright = int(random(255));
    fill(rBright);
    ellipse(width/2, height/2, 10, 10);
    port.write(rBright);
    }






    const int ledPin = 13; // Digital PWM pin for LED
    const int prPin = 0; // Analog pin for photoresistor
    int incomingByte; // Variable to hold data from Processing

    void setup(){
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
    pinMode(prPin, INPUT);
    }

    void loop(){
    readFromProcessing();
    // writeToProcessing();
    }

    void readFromProcessing(){
    if(Serial.available() > 0){
    incomingByte = Serial.read();
    analogWrite(ledPin, int(incomingByte));
    delay(20);
    }
    }

    void writeToProcessing(){
    int incomingLight = analogRead(prPin);
    delay(20);
    }

    ReplyDelete
  2. /* Knock Sensor

    This sketch reads a piezo element to detect a knocking sound.
    It reads an analog pin and compares the result to a set threshold.
    If the result is greater than the threshold, it writes
    "knock" to the serial port, and toggles the LED on pin 13.

    The circuit:
    * + connection of the piezo attached to analog in 0
    * - connection of the piezo attached to ground
    * 1-megohm resistor attached from analog in 0 to ground

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

    created 25 Mar 2007
    by David Cuartielles
    modified 30 Aug 2011
    by Tom Igoe

    This example code is in the public domain.

    */


    // these constants won't change:
    const int ledPin = 13; // led connected to digital pin 13
    const int knockSensor = A0; // the piezo is connected to analog pin 0
    const int threshold = 5; // threshold value to decide when the detected sound is a knock or not


    // these variables will change:
    int sensorReading = 0; // variable to store the value read from the sensor pin
    int ledState = LOW; // variable used to store the last LED status, to toggle the light

    void setup() {
    pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
    Serial.begin(9600); // use the serial port
    }

    void loop() {
    // read the sensor and store it in the variable sensorReading:
    sensorReading = analogRead(knockSensor);

    // if the sensor reading is greater than the threshold:
    if (sensorReading >= threshold) {
    // toggle the status of the ledPin:
    ledState = !ledState;
    // update the LED pin itself:
    digitalWrite(ledPin, ledState);
    // send the string "Knock!" back to the computer, followed by newline
    Serial.println(sensorReading);
    }
    delay(50); // delay to avoid overloading the serial port buffer
    }

    ReplyDelete