Bob Danton PC Projects

From BenningtonWiki
Jump to: navigation, search

Week 2


Push-Button LED


const int LED = 2;
const int BUTTON = 7;

int val = 0;
int old_val = 0;
int state = 0;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop(){
  val = digitalRead(BUTTON);
  if ((val == HIGH) && (old_val == LOW)) {
    state = 1 - state;
    delay(10);
  }
  
  old_val = val;
  if (state == 1) {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
}

Week 3


Fading LED w/ PWM


const int LED = 9;
int i = 0;

void setup() {
  pinMode(LED, OUTPUT);
}
void loop(){

  for (i = 0; i < 255; i++) { // as long as i is less than 255, fade in
  
    analogWrite(LED, i);
    delay(10);           // Wait 10ms because analogWrite is instantaneaous
                        
  }
  
  for (i = 255; i > 0; i--) { // from 255 to 0, fade out
  
    analogWrite(LED, i);
    delay(10);
  }
}

Turn LED On/Off with Button; Fade when Button held

const int LED = 9;
const int BUTTON = 7;

int val = 0;

int old_val = 0; 
int state = 0;   // 0 = LED off, 1 = on

int brightness = 128;
unsigned long startTime = 0; // store when button pressed and held

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop() {
  
  val = digitalRead(BUTTON);
                       
  if ((val == HIGH) && (old_val == LOW)) { // check for change
    state = 1 - state; // change state
                       
    startTime = millis(); // millis() is Arduino clock
                          // it returns how many milliseconds
                          // have passed since the board has
                          // been reset.
                          
    delay(10);
  }
  
  if ((val == HIGH) && (old_val == HIGH)) {  // check if button is held
    
    if (state == 1 && (millis() - startTime) > 500) { //for more than 500ms.
      
      brightness++;
      delay(10);    //slow down
                    
      if (brightness > 255) {
        brightness = 0; // reset from max to 0
      }
    }
  }
  
  old_val = val;
  if (state == 1) {
    analogWrite(LED, brightness); // turn LED ON at  current brightness
  } 
  else {
    analogWrite(LED, 0);
  }
}

Blink LED at rate determined by Light with LDR

const int LED = 13;
int val = 0;
                    
void setup() {
  pinMode(LED, OUTPUT);
  // Note: Analogue pins are automatically inputs
}

void loop() {
  
  val = analogRead(0);
                       
  digitalWrite(LED, HIGH);
  
  delay(val); //delay by sensor value

  digitalWrite(LED, LOW);
  
  delay(val);
}

Fade LED with LDR and Serial Print Sensor Reading

const int LED = 9;
int val = 0;
                   
void setup() {
  
  Serial.begin(9600); //open the serial port at 9600 bits per second
  
  pinMode(LED, OUTPUT);
}

void loop() {
  
  val = analogRead(0);

  Serial.println(val); // print val to serial port
                       
  analogWrite(LED, val/4); //div by 4 to get 1023 range to 255
                           
  delay(10);
}

Week 4


Control Servo Angle with LDR

#include <Servo.h>  //include the servo library

Servo servoMotor; //create an instance of the servo object to control a servo
int servoPin = 2; // map to pin 2

void setup() { 
  Serial.begin(9600);
  servoMotor.attach(servoPin); //attach servo on pin 2 to servo object
} 

void loop() 
{ 
  int analogValue = analogRead(A0);
  Serial.println(analogValue);
  int servoAngle = map(analogValue, 5, 700, 0, 179); //map sensor range to 180 degrees
  servoMotor.write(servoAngle); //write angle to servo
}

Sweep

Uses same breadboard as above (just without accessing LDR)

//based on Sweep by BARRAGAN <http://barraganstudio.com> 

#include <Servo.h> 
 
Servo myservo; //create servo object to control a servo 
//note: a maximum of eight servo objects can be created 
 
int pos = 0; //store the servo position 
 
void setup() 
{ 
  myservo.attach(2);
} 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1) //goes from 0 to 180 degrees by 1 degree
  {
    myservo.write(pos); //go to position in variable 'pos' 
    delay(15); //wait 15ms for servo to reach position 
  } 
  for(pos = 180; pos>=1; pos-=1) //and reverse
  {
    myservo.write(pos);
    delay(15);
  } 
}

Midterm Collaboration with Erick Daniszewski


Erick and Bob

Week 6


Serial Communication W/ Potentiometer

Arduino:

void setup() {
	Serial.begin(9600);
}
void loop() {
    int analogValue = analogRead(A0) /4;      // read the pot value
    Serial.write(analogValue);        // print the value in the serial monitor as a binary value
}

Into Processing

First Code:

import processing.serial.*;
Serial myPort;

void setup () {
  size(800, 600);  //set window size
  println(Serial.list());  //list all available serial ports
}

And Results:

Final Code As Laid Out in Tutorial (obviously rewritten and recolored by me):

import processing.serial.*;
Serial myPort;
float xPos = 0;

void setup () {
  size(800, 600);  //set window size
  
  println(Serial.list());  //list all available serial ports
  
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
 
  background(#622E85);
}

void draw () {
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read(); //get one byte
  println(inByte);
  
  float yPos = height - inByte;
  stroke(#216E38);
  line(xPos, height, xPos, height - inByte);
  
  if (xPos >= width) {
    xPos = 0;
    background(#622E85); //clear screen
  } 
  else {
    xPos++;
  }
}

And Results:

I didn't like that my graph only went a third of the way up the window. A little multiplication fixed that.

import processing.serial.*;
Serial myPort;
float xPos = 0;

void setup () {
  size(800, 600);  //set window size
  
  println(Serial.list());  //list all available serial ports
  
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
 
  background(#622E85);
}

void draw () {
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read(); //get one byte
  println(inByte);
  
  float yPos = height - inByte;
  stroke(#216E38);
  line(xPos, height, xPos, height - (inByte * 2));
  
  if (xPos >= width) {
    xPos = 0;
    background(#622E85); //clear screen
  } 
  else {
    xPos++;
  }
}

And Results:

Then I did something crazy.

import processing.serial.*;
Serial myPort;
float xPos = 0;

void setup () {
  size(800, 600);  //set window size
  
  println(Serial.list());  //list all available serial ports
  
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
 
  background(#622E85);
}

void draw () {
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read(); //get one byte
  println(inByte);
  
  float yPos = height - inByte;
  stroke(#216E38);
  ellipse(xPos, height - inByte, xPos, yPos);
  
  if (xPos >= width) {
    xPos = 0;
    background(#622E85); //clear screen
  } 
  else {
    xPos++;
  }
}

It draws crazy moving ellipses.

I was going to mess around more with how the input and other variables were moving and resizing the ellipse, and I did try several other combinations in the ellipse() parameters, but Processing crashed my computer twice so I had to stop.

Later, In class:

A perfect circle in the center, scaling in size with the potentiometer!

import processing.serial.*;
Serial myPort;
float xPos = 0;

void setup () {
  size(800, 600);  //set window size
  
  println(Serial.list());  //list all available serial ports
  
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
 
  background(#622E85);
}

void draw () {
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read(); //get one byte
  println(inByte);
  
  float yPos = height - inByte;
  stroke(#216E38);
  ellipse(400, 300, (1.7 * inByte), (1.7 * inByte));
  
  if (xPos >= width) {
    xPos = 0;
    background(#622E85); //clear screen
  } 
  else {
    xPos++;
  }
}

Filled in:

import processing.serial.*;
Serial myPort;
float xPos = 0;

void setup () {
  size(800, 600);  //set window size
  
  println(Serial.list());  //list all available serial ports
  
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
 
  background(#622E85);
}

void draw () {
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read(); //get one byte
  println(inByte);
  
  float yPos = height - inByte;
  stroke(#216E38);
  fill(#216E38);
  ellipse(400, 300, (1.7 * inByte), (1.7 * inByte));
  
  if (xPos >= width) {
    xPos = 0;
    background(#622E85); //clear screen
  } 
  else {
    xPos++;
  }
}

Now, one where the circle zooms diagonally in and gets larger w/ the potentiometer. Smaller display so it doesn't crash again...

import processing.serial.*;
Serial myPort;
float xPos = 0;

void setup () {
  size(256, 256);  //set window size
  
  println(Serial.list());  //list all available serial ports
  
  String portName = Serial.list()[4];
  myPort = new Serial(this, portName, 9600);
 
  background(#622E85);
}

void draw () {
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read(); //get one byte
  println(inByte);
  
  float yPos = height - inByte;
  stroke(#000000);
  fill(#216E38);
  ellipse(inByte, inByte, inByte, inByte);
  
  if (xPos >= width) {
    xPos = 0;
    background(#622E85); //clear screen
  } 
  else {
    xPos++;
  }
}

Week 7


Variable Speed Motor W/ Potentiometer

I worked with Will Jackson this week, so our breadboards and code are the same.

const int mtrPin = 9;

 void setup() {
   pinMode(mtrPin, OUTPUT);
 }

 void loop() {
   int sensorValue = analogRead(A0);
   int outputValue = map(sensorValue, 0, 1023, 0, 255);
   analogWrite(mtrPin, outputValue);
 }

Bi-Directional Motor W/ H-Bridge and Button

const int switchPin = 2;    // button
  const int motor1Pin = 3;    //H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    //H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 9;    //H-bridge enable pin
  
void setup() {
    pinMode(switchPin, INPUT); 
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);
    digitalWrite(enablePin, HIGH); //set enablePin high so that motor can turn on:
  }
  
 void loop() {
    if (digitalRead(switchPin) == HIGH) { //if switch is high, motor turns one direction:
      digitalWrite(motor1Pin, LOW);  //set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH); //set leg 2 of the H-bridge high
    } 
    else { //low reverses direction
      digitalWrite(motor1Pin, HIGH);
      digitalWrite(motor2Pin, LOW);
    }
  }

FINAL PROJECT


Bob Danton Physical Computing Final