Your browser does not support the canvas tag.

Week 7

Time to start looking for your artist for in-class presentations. We need someone who is doing physical computing that you find interesting. Cannot just be a single channel video artist!

We need a PONG title, and we need an image, asap. (D'oh!).

gear making websites
https://woodgears.ca/gear_cutting/template.html
http://geargenerator.com/

Casemaking website
makercase.com/

Rotary encoders
are cool, but not exactly easy to work with.

Unlit a potentiometer a rotary encoder will spen forever!
There are a few good sites to help us, but none have wiring diagrams, so here's the best way to wire the encoder to get the cleanest data.

Good video walkthtough explaining how they work. link

This site has some libraries for the arduino - very handy. link

This arduino code requires two librariesPinChangeInt and AdaEncoder. Links are availalbe from the site above.
The arduino uses the serial.write to send info to Processing.

#include <PinChangeInt.h> // necessary otherwise we get undefined reference errors.
#include <AdaEncoder.h>

#define a_PINA 2
#define a_PINB 3
#define b_PINA 5
#define b_PINB 6

int8_t clicks=0;
char id=0;
byte rotcount; 

void setup()
{
  Serial.begin(115200);
  AdaEncoder::addEncoder('a', a_PINA, a_PINB);
  AdaEncoder::addEncoder('b', b_PINA, b_PINB);  
}

void loop()
{
  encoder *thisEncoder;
  thisEncoder=AdaEncoder::genie(&clicks, &id);
  if (thisEncoder != NULL) {
  //  Serial.print(id); Serial.print(':');
    if (clicks > 0) {
    
      rotcount ++; 
        
    }
    if (clicks < 0) {
     
       rotcount --; 
        
    }
   Serial.write(rotcount); 
  }
   
}
This is the processing code that accepts incoming arduino data.
// Example based on code by Tom Igoe 
import processing.serial.*; 
Serial myPort;  // The serial port boolean 
hilow = true; 
int inByte, inByteOld; 
float r2; 
void setup() {   
  size(400, 400, P3D);   
  println(Serial.list());   
  myPort = new Serial(this, Serial.list()[17], 115200);
} 
void draw() {   
  background(200);   
  while (myPort.available () > 0) {     
    inByte = int(myPort.read());     
    println(inByte);
  }   
  if ( inByte > inByteOld ) {     
    r2 += .2;
  }   
  else if ( inByte < inByteOld ) {      
    r2 -= .2;
  }   
  inByteOld = inByte;    
  pushMatrix();   
  translate(width/2, height/2, 0);   
  rotateY(r2);   
  fill(200);   
  box(100);   
  popMatrix();
} 

This sketch uses the encoder with firmata, if you try it you'll notice that the encoder won't work at high speeds. It may be fine for your application, but it's not ideal.

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int pin1, pin2, oldPin1; 
long timer1, timer2; 

boolean dir; 
boolean stopped = true; 
float r2 = 2.5;
boolean position, oldposition; 

void setup() {
  size (300, 300, P3D);
  arduino = new Arduino(this, Arduino.list()[17], 115200); // 115200
  arduino.pinMode(2, Arduino.INPUT);// sets port 2 to input
  arduino.pinMode(3, Arduino.INPUT);// sets port 2 to input
}

void draw() {
  background(200); 
  pin1 = arduino.digitalRead(2); 
  pin2 = arduino.digitalRead(3); 
  if (pin1 == pin2) {
    position = false;
    if (oldPin1 == pin1) {
      dir = true;
    }
    else {
      dir = false;
    } 
    oldPin1 = pin1;
  }
  else {
    position = true; 
    if (oldPin1 == pin1) {
      dir = false;
    }
    else {
      dir = true;
    }
    oldPin1 = pin1;
  }
  if (position != oldposition ) {
    if (dir == true) {
      r2 += .1;
    }
    if (dir == false) {
      r2 -= .1;
    }
  }
  pushMatrix(); 
  translate(width/2, height/2, 0); 
  rotateY(r2); 
  fill(200); 
  box(100); 
  popMatrix(); 

  if (pin1 == 0) {
    fill(255, 255, 0);
  }
  else {
    fill(0, 80, 255);
  }
  ellipse(50, 50, 50, 50); 
  if (pin2 == 0) {
    fill(255, 255, 0);
  }
  else {
    fill(0, 80, 255);
  }
  ellipse(width - 50, 50, 50, 50); 
  oldposition = position; 
}

acclerometers!

This can tell us rotation! The Adafruit website looks great, but there are lots of mistakes adafruit accelerometer First - the image in the diagram is all wrong. Go by the wirtten instructions for I2C Wiring, but iggnore the picture. Second - you do need two arduino libraries, the Adafruit_LIS3DH is right but the scond one is not the right library, this is the one that you need. I2C library
The Adafruit_LIS3DH library you can install through the arduino interface the second you have to install by hand. You can either write your own firmata or senyou your own data to processing with serialWrite