PHYSICAL COMPUTING

Projects

[Blink] [Runway] [Move] [Pixel Bend]

Project 4: Pixel Bend

Description

The goal of this assignment is to create a coherent interactive experience that involves the Arduino communicating with another device (computer, 2nd Arduino, MIDI instrument ...). For my project, I decided to create a movement detector for controlling the speed of a motor.

The materials needed for this projects are the following:

  1. 1 Arduino module
  2. 1 Photoresistor
  3. 1 Solderless breadboard
  4. 1 Accelerometer ADXL3xx
  5. 1 DC Motor
  6. 1 H-Bridge
  7. 6 LED's
  8. 6 resistors

The schematic diagram for this project is the following

Figure 1


Procedure

The steps for controlling the motor are as follow:

  1. The arduino has to be connected to the computer and the lamp must be turned off.
  2. The computer will show a graphic that will indicate the stability of the motor.
  3. When the lamp is turned on, the arduino will activate the motor but it will not move.
  4. Then the user will move his hand around the accelerometer (which will detect the movements based on the coordinate system of your hands).
  5. The velocity of the motor will either increase or decrease depending on the the movements of your hands.
  6. Finally the computer will receive the calculation of the speed sent by arduino through the serial port which will represent the stability of the motor.

Figure 2

Figure 3

Code


Arduino
 

Processing
 
int accelerometer[2];
int motor1Pin = 8;
int motor2Pin = 10;
int enablePin = 9;
int ledPin = 13;
int i, irReader = 5;
int pins[] = { 2, 3, 4, 5, 6, 7 };

void setup() {
  Serial.begin(9600);
  Serial.println("0,0,");
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  for (i = 0; i < 6; i++)
    pinMode(pins[i], OUTPUT);
}
void loop() {
  i = analogRead(irReader);
 
  if(i > 5 && i < 45)
      digitalWrite(pins[0], HIGH);
  else
      digitalWrite(pins[0], LOW);
  if(i > 45 && i < 90)
      digitalWrite(pins[1], HIGH);
  else
      digitalWrite(pins[1], LOW);
  if(i > 90 && i < 135)
      digitalWrite(pins[2], HIGH);
  else
      digitalWrite(pins[2], LOW);
  if(i > 135 && i < 180)
      digitalWrite(pins[3], HIGH);
  else
      digitalWrite(pins[3], LOW);
  if(i > 180 && i < 225)
      digitalWrite(pins[4], HIGH);
  else
      digitalWrite(pins[4], LOW);
  if(i > 225 && i < 260)
      digitalWrite(pins[5], HIGH);
  else
      digitalWrite(pins[5], LOW);
  digitalWrite(motor1Pin, LOW);
  digitalWrite(motor2Pin, HIGH);
  analogWrite(enablePin, i);
  for (int j = 0; j < 2; j++) {
    accelerometer[j] = analogRead(j);
    delay(10);
  }
 
  if (Serial.available() > 0) {
    int inByte = Serial.read();
   
    for (int j = 0; j < 2; j++) {
      Serial.print(accelerometer[j], DEC);
      Serial.print(",");
    }
    Serial.println();
  }
  delay(30);
}
import processing.serial.*;
int graphPosition = 0;
int[] vals = new int[2];
int[] maximum = new int[2];
int[] minimum = new int[2];
int[] range = new int[2];
float[] attitude = new float[2];
float position;
Serial myPort;
boolean madeContact = false;

void setup () {
  size(400, 400, P3D);
  background(0);
  for (int i = 0; i < 2; i++) {
    maximum[i] = 600;
    minimum[i] = 200;
    range[i] = maximum[i] - minimum[i];
  }
  position = width/2;
  PFont myFont = createFont(PFont.list()[2], 18);
  textFont(myFont);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.bufferUntil('\r');
  fill(90, 250, 250);
}
void draw() {
  background(0);
  text(vals[0] + " " + vals[1], -30, 10);
  if (madeContact == false) {
    myPort.write('\r');
  }
  setAttitude();
  filt();
}
void setAttitude() {
  for (int i = 0; i < 2; i++) {
    attitude[i] = (2*PI) * float(vals[i] -
      minimum[i]) /float(range[i]);
  }
}
void filt() {
  translate(position, position, position);
  rotateX(-attitude[1]);
  rotateY(-attitude[0] - PI/2);
  fill(90, 250, 250);
  ellipse(0, 0, width/4, width/4);
  fill(0);
  text(vals[0] + " " + vals[1], -30, 10, 1);
}
void serialEvent(Serial myPort) {
  madeContact = true;
  String myString = myPort.readStringUntil('\n');
  if (myString != null) {
    myString = trim(myString);
    int sensors[] = int(split(myString, ','));
    if (sensors.length >= 2) {
      vals[0] = sensors[0];
      vals[1] = sensors[1];
      myPort.write('\r');
    }
  }