Assignment 7.1

Post date: Jul 06, 2010 7:39:19 PM

Using my piano sketch from assignment 3.2, I used a potentiometer connected to analog pin 1 to press the keys on the "piano." When the knob on the potentiometer is all the way left, the left-most key is presssed, and as you turn the knob right each key in order is then pressed, with only one key at a time able to be pressed.

Arduino:

int potPin = 1; // select the input pin for the potentiometer

int val = 0; // variable to store the value coming from the sensor

void setup() {

Serial.begin(9600); // begins serial communication at 9600 bps

pinMode(potPin, INPUT); // declares potPin as input

} // not sure if this is necessary,

// I got it working and don't want to mess it up

void loop() {

val = analogRead(potPin); // read the value from the sensor

if(val >= 0 && val <= 127) { // if the value is between these numbers,

Serial.print(1, BYTE); // send this to the computer

}

else if(val >= 128 && val <= 255) { // if not,

Serial.print(2, BYTE); // do this

}

else if(val >= 256 && val <= 383) {

Serial.print(3, BYTE);

}

else if(val >= 384 && val <= 511) {

Serial.print(4, BYTE);

}

else if(val >= 512 && val <= 639) {

Serial.print(6, BYTE);

}

else if(val >= 640 && val <= 767) {

Serial.print(7, BYTE);

}

else if(val >= 768 && val <= 895) {

Serial.print(8, BYTE);

}

else if(val >= 896 && val <= 1023) {

Serial.print(9, BYTE);

}

delay(30); // delays for 30 milliseconds to allow Processing to keep up

}

Processing:

import processing.serial.*; // import Processing library

Serial myPort; // declare variables

int val;

void setup() {

size(600, 400); // sets size

background(225); // sets background color to white

String portName = Serial.list()[0];

myPort = new Serial(this, portName, 9600);

}

void draw() {

int val = 0; // resets val to 0

println(val); // prints value of val (should be 0)

val = myPort.read(); // read the port and store it in val

println(val); // prints new value of val

fill(0); // sets fill color to black

rect(50, 50, 500, 300); // draws rectangle for backboard of piano

fill(225); // sets fill color to white

rect(60, 100, 50, 240); // draws rectangles for keys

rect(121, 100, 50, 240);

rect(182, 100, 50, 240);

rect(243, 100, 50, 240);

rect(307, 100, 50, 240);

rect(368, 100, 50, 240);

rect(429, 100, 50, 240);

rect(490, 100, 50, 240);

println(val); // prints value of val, making sure it hasn't changed

if (val == 1) { // if val is this, then

fill(0); // change fill color,

rect(60, 100, 50, 240); // fill over white key, make it black

fill(50);

rect(60, 100, 50, 210); // draw new rectangle indicating key being pressed, and

delay(27); // delay for 27 milliseconds

} // stops flickering of keys

else if (val == 2) {

fill(0);

rect(121, 100, 50, 240);

fill(50);

rect(121, 100, 50, 210);

delay(27);

}

else if (val == 3) {

fill(0);

rect(182, 100, 50, 240);

fill(50);

rect(182, 100, 50, 210);

delay(27);

}

else if (val == 4) {

fill(0);

rect(243, 100, 50, 240);

fill(50);

rect(243, 100, 50, 210);

delay(27);

}

else if (val == 6) {

fill(0);

rect(307, 100, 50, 240);

fill(50);

rect(307, 100, 50, 210);

delay(27);

}

else if (val == 7) {

fill(0);

rect(368, 100, 50, 240);

fill(50);

rect(368, 100, 50, 210);

delay(27);

}

else if (val == 8) {

fill(0);

rect(429, 100, 50, 240);

fill(50);

rect(429, 100, 50, 210);

delay(27);

}

else if (val == 9) {

fill(0);

rect(490, 100, 50, 240);

fill(50);

rect(490, 100, 50, 210);

delay(27);

}

}

I had a lot of trouble with this one. My first attempt at this wouldn't change the key that was pressed when the potentiometer was adjusted, so thanks to Mr. Dickie I solved that problem by changing what was being written by the program each time the potentiometer was adjusted. This would move the keys, though there was a delay between adjusting the potentiometer and Processing actually showing the changes, so I had to insert delays in both the Arduino sketch and the Processing sketch so it would slow down how fast the data was being written and read. This helped negate the delay between the adjustment and the keys being pressed. I had to play with the delay times a little bit because too short of a delay would cause the keys to flicker and too long of a delay would cause a lag between the potentiometer being adjusted and the keys being pressed again. So after much trial and error, I am happy to get this sketch working and done with.