Wednesday, February 8, 2012

Lab 2 Exercises

First, playing with StampPlot.
Ramps and sawtooths and sinusoids!


These consisted of super simple (ghetto) codes like the following:

(e.g. sawtooth; just a modified version of a ramp)

int ramp = 1;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  ramp++;
  Serial.println(ramp);
if (ramp == 100)
{
  ramp = 1;
}
  delay(100);
}

(e.g. sinusoid, really piecemeal)

float angle = 0;
const float pi = 3.14;
float sine = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  angle = angle + (1/(4*pi));
  sine = sin(angle);
  Serial.println(sine);
  delay(200);
}

"How fast can you tap your finger?" / "Make a lie detector." /  "Make a light meter."


So according to the lab sheet, we just had to use this RCtime code that was provided.

We chose a 620-ohm resistor and a photocell to play with. We also decided to try all of the aforementioned tasks in conjunction with each other because after we tried using the conductive wire setup for the finger-tapping assignment, we thought it would also be cool if we could use a photocell instead. The conductive wire setup was used for the lie detector, however, since it required us to measure galvanic skin resistance which was supposed to vary with sweating/changes in skin biochemistry due to nervousness. It wasn't particularly effective, though, and even when I tried blatant lies ("I am a purple elephant," "I am not the one updating this blog") it failed to detect them. :( Thus we moved on to the light detector.

First, we tried some silly things, like having our little device complain if it didn't get enough light (we think this should count as a light meter).....


int sensorPin = 4; // there's a 620 ohm resistor protecting this pin
long result = 0;
void setup()
{
Serial.begin(9600);
}
void loop() // run over and over again
{
 
delay(10);
int RC = RCtime(sensorPin);
if ((RC >=300) && (RC < 500))
{
  Serial.println("It's a little shady...");
}
else if ((RC >=500) && (RC <=1000))
{
  Serial.println("Why is it getting so dark?");
}
else if (RC >=1000)
{
  Serial.println("Help!");
}


}

long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
return result; // report results
}



We also saw how this might be used by our Arduino to "guess" which finger we are poking the photocell with. We tried setting some additional thresholds using our setup to distinguish one finger from another (larger fingers may generate longer charge times, if only because of an increased probability that smaller fingers won't fully cover the photocell surface). It generally worked, but the fact that the thresholds would have to be re-set for different individuals (depending on their hand size, etc.) made it impractical to carry forward into additional tests.

As for the finger-tapping assignment, one approach could be to increase a counter by one every time the RCtime reading increased past a certain threshold value. We attempted that, too:

Finger-Tap (with photocell)


int sensorPin = 4; // there's a 620 ohm resistor protecting this pin
long result = 0;
int rise = 0;
int count = 0;
int time1 = 0;


void setup()
{
Serial.begin(9600);
}
void loop() // run over and over again
{
 
delay(10);
int RC = RCtime(sensorPin);
Serial.println(RC);

time1=millis();
if ((RC >= 270) && rise == 0)
{
  count++;
  rise = 1;
}
else if ((RC <=230) && rise == 1)
{
  rise = 0;
}
if (time1 >=10000)
{
  Serial.println(count);
  while(1) {}
 
}
}

long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT

digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
return result; // report results
}

One thing that definitely needed to be changed about our first draft of this approach was that the thresholds had to be made flexible according to ambient light conditions. (The RCtime value threshold for what was deemed "baseline," for instance, would certainly need to become lower if the board was placed in an area with lots of ambient light.)


"Make some noise."

Here are our awesome custom sound effects, courtesy of Winslow :) I think the "success" and "try again" sounds are my favorites.

Custom sounds:


int pin = 4;
char input;
unsigned int A = 440;
unsigned int AS = 466;
unsigned int B = 494;
unsigned int C = 262;
unsigned int D = 294;
unsigned int DS = 311;
unsigned int E = 330;
unsigned int F = 349;
unsigned int FS = 370;
unsigned int G = 392;
unsigned int GS = 415;

void setup(){
  Serial.begin(9600);
  Serial.println("start");
}

void loop(){
  if(Serial.available()>0){
    input = Serial.read();
    Serial.print("Sound for ");
   
    if(input == 'y'){
      Serial.println("yes");
      tone(pin,F,300);
      delay(400);
      tone(pin,A,300);
      delay(400);
      tone(pin,AS,300);
      delay(400);
    }
   
    else if(input == 'n'){
      Serial.println("no");
      tone(pin,AS,300);
      delay(400);
      tone(pin,A,300);
      delay(400);
      tone(pin,F,300);
      delay(400);
    }
   
    else if(input == 't'){
      Serial.println("try again");
      tone(pin,D,300);
      delay(400);
      tone(pin,C,300);
      delay(400);
      tone(pin,E,200);
      delay(300);
      tone(pin,F,400);
      delay(500);
    }
   
    else if(input == 'w'){
      Serial.println("warning");
      tone(pin,5000,500);
      delay(600);
      tone(pin,1000,500);
      delay(600);
      tone(pin,5000,500);
      delay(600);
      tone(pin,1000,500);
      delay(600);
    }
   
    else if(input == 's'){
      Serial.println("success");
      tone(pin,F,300);
      delay(400);
      tone(pin,E,300);
      delay(450);
      tone(pin,C,150);
      delay(200);
      tone(pin,D,300);
      delay(400);
      tone(pin,C,300);
      delay(450);
      tone(pin,F,250);
      delay(350);
      tone(pin,A,300);
      delay(400);
    }
   
    else if(input == 'f'){
      Serial.println("fast");
      tone(pin,C,500);
      delay(550);
      tone(pin,D,400);
      delay(450);
      tone(pin,E,300);
      delay(350);
      tone(pin,F,200);
      delay(250);
      tone(pin,G,100);
      delay(150);
      tone(pin,G,100);
      delay(150);
      tone(pin,G,100);
      delay(150);
    }
   
    else if(input == 'l'){
      Serial.println("slow");
      tone(pin,C,100);
      delay(150);
      tone(pin,D,200);
      delay(250);
      tone(pin,E,300);
      delay(350);
      tone(pin,F,400);
      delay(450);
      tone(pin,G,500);
      delay(550);
      tone(pin,G,1000);
      delay(1000);
    }
   
    else{
      Serial.println(input);
    }
  }
}

"Turn it down. (Change brightness of LEDs)"


Brightness automatically changes:


int pin = 5;

void setup(){
  Serial.begin(9600);
  Serial.println("start");
  pinMode(pin, OUTPUT);
}

void loop(){
  for(int x = 1; x<1000; x++){
    for(int y = 1; y<1000; y++){
      if(x>=y){
        digitalWrite(pin,HIGH);
      }
      if(y>x){
        digitalWrite(pin,LOW);
      }
    }
  }
}





Brightness adjustable by user (by typing on the keyboard):



int pin = 5;
int input = 0;
int bright = 1;
int counter = 1;

void setup(){
  Serial.begin(9600);
  Serial.println("start");
  pinMode(pin,OUTPUT);
}

void loop(){
  if(Serial.available()>0){
    input = Serial.read();
    Serial.print("Brightness Level ");
    if(input == '1'){
      Serial.println("1");
      bright = 10;
    }
    else if(input == '2'){
      Serial.println("2");
      bright = 20;
    }
    else if(input == '3'){
      Serial.println("3");
      bright = 40;
    }
    else if(input == '4'){
      Serial.println("4");
      bright = 80;
    }
    else if(input == '5'){
      Serial.println("5");
      bright = 160;
    }
    else if(input == '6'){
      Serial.println("6");
      bright = 320;
    }
    else if(input == '7'){
      Serial.println("7");
      bright = 640;
    }
    else if(input == '8'){
      Serial.println("8");
      bright = 1280;
    }
    else if(input == '9'){
      Serial.println("9");
      bright = 2560;
    }
    else{
      Serial.println("undefined");
    }
  }
 
  if(bright>=counter){
    digitalWrite(pin,HIGH);
  }
  if(counter>bright){
    digitalWrite(pin,LOW);
  }
  if(counter>2600){
    counter = 1;
  }
  counter++;
}
 

"Use fritzing to document one of your circuits."


TA-DA! Winslow is the best :)
Here are the schematics (and a lousy, unbalanced picture) of one of our photocell circuits!


No comments:

Post a Comment