Tuesday, February 28, 2012

Lab 4 Exercises

Hi world,

Long time, no post (kind of...)! Here are Lab 4 exercises...


Storing in non-volatile memory.


One approach:


#include <EEPROM.h>

int readvalue;
int command;
int input;
int buffer;
int address = 0;
int valueArray[] = {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}; // Used only for testing code

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

void loop() {
command = Serial.read();

if (command == 'w')
{
Serial.print("What would you like to store in address #");
Serial.print(address);
Serial.println("?");
Serial.flush();
input = Serial.read();
//Serial.println(input);
while (input == -1) {
if(Serial.available()>0){
delay(15);
buffer = 0;
input = 0;
 
while(Serial.available()>0){
buffer = Serial.read();
input = input*10 + (buffer-'0');
}
}
}

EEPROM.write(address,input);
valueArray[address] = input;
Serial.println(address);
Serial.println(input);
Serial.println("Okay!");
address++;
if (address == 512)
{
  address = 0;
}
}

if (command == 'r') {

readvalue = 0;
for (int i = 0; readvalue != 255; i++) {
readvalue = EEPROM.read(i);
//readvalue = valueArray[i];

if (readvalue != 255){
Serial.println(readvalue);
}
else if (readvalue == 255)
{
 Serial.println("The address #");
 Serial.print(i);
 Serial.println(" is empty");
}
}
}

This one doesn't have a clear-all feature :(

And another approach (arguably superior, haha):


#include <EEPROM.h>

int buffer = 0;
int value = 0;
byte valueByte;
int address = 1;
int counterAddress = 0;
int keyPress = 0;
//int valueArray[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Used only for testing code

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

void loop(){
keyPress = 0;
if(Serial.available()>0){
delay(5);
keyPress = Serial.read();
if(keyPress == 'w'){
Serial.print("Enter number to address ");
Serial.println(address);
while(1){
if(Serial.available()>0){
delay(15);
value = 0;
valueByte = 0;
while(Serial.available()>0){
buffer = Serial.read();
value = value*10 + (buffer-'0');
}
       
valueByte = byte(value);
Serial.print("saving ");
Serial.print(value);
Serial.print(" at ");
Serial.println(address);
//valueArray[address] = value; // Used only for testing code
EEPROM.write(address, valueByte); // EEPROM writing
address++;
EEPROM.write(counterAddress, byte(address));
break;
}
}
//Serial.println("Out of loop"); // Used for testing only
}
 
else if(keyPress == 'r'){
Serial.println("Reading all values in EEPROM array:");
for(int i=1;i<EEPROM.read(counterAddress);i++){
Serial.print(i);
Serial.print(" ");
//Serial.println(valueArray[i]); // Used only for testing
Serial.println(int(EEPROM.read(i)));
}
}
 
 else if(keyPress == 'q'){
 Serial.println("Do you want to reset the addresses? (y/n)");
 while(1){
 if(Serial.available()>0){
 delay(5);
 keyPress = Serial.read();
 if(keyPress == 'y'){
 address = 1;
 EEPROM.write(counterAddress,byte(0));
 break;
 }

if(keyPress == 'n'){
break;
}
}
}
}
 
else{
Serial.println("Unrecognized Key");
}
}
}



...........


And here's Winslow's massive code for the data-logger portion of the lab @_@...........

Build a data-logger.

#include <EEPROM.h>

int sensorPin = 3;
unsigned long time;
unsigned long startTime;
unsigned long endTime;
unsigned long testDuration = 180000;
unsigned long takeSample = 15000;
int counter = 0;
int value = 0;
byte valueByte;
int address = 1;
int counterAddress = 0;
int keyPress = 0;
int valueArray[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Used only for testing code

void setup(){
  Serial.begin(9600);
  Serial.println("Start:");
  Serial.println("Press w to record data or Press r to read data.");
}

void loop(){
  keyPress = 0;
  if(Serial.available()>0){
    delay(5);
    keyPress = Serial.read();
    if(keyPress == 'w'){
      startTime = millis();
      time = startTime;
      endTime = time + testDuration;
      while(time<=endTime){
        time = millis();
        if(time>=(counter*takeSample+startTime)){
          value = RCtime(sensorPin);
          valueByte = byte(value);
          Serial.print("saving "); 
          Serial.print(int(valueByte)); 
          Serial.print(" at "); 
          Serial.println(address);
          valueArray[address] = valueByte; // Used only for testing code
//          EEPROM.write(address, valueByte); // EEPROM writing
          address++;
          counter++;
//          EEPROM.write(counterAddress, byte(address));
          valueArray[0] = address; // Used only for testing code
        }
      }
      Serial.println("Finished Testing!"); // Used for testing only
    }
    else if(keyPress == 'r'){
      Serial.println("Reading all values from previous test:");
      for(int i=1;i<valueArray[0];i++){ // In actual code i<EEPROM.read(counterAddress)
        Serial.print(i);
        Serial.print(" ");
        Serial.println(int(valueArray[i])); // Used only for testing
//        Serial.println(EEPROM.read(i));
      }
    }
    else if(keyPress == 'q'){
      Serial.println("Do you want to reset the data? (y/n)");
      while(1){
        if(Serial.available()>0){
          delay(5);
          keyPress = Serial.read();
          if(keyPress == 'y'){
            address = 1;
            //EEPROM.write(counterAddress,byte(0));
            valueArray[0] = 0; // Used for testing code
            break;
          }
          if(keyPress == 'n'){
            break;
          }
        }
      }
    }
    else{
      Serial.println("Unrecognized Key");
    }
  }
}

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
}

.
.
.
.
.


And then, fun with servos:
.
.
.
.
.


unsigned long time;
unsigned int pulseTime = 1500;
unsigned int currentPulse = 1500;
unsigned long waitTime = 20000;
unsigned long stamp;
int servoPin = 6;
int buffer;
int modSub;

void setup(){
  Serial.begin(9600);
  pinMode(servoPin, OUTPUT);
  digitalWrite(servoPin, LOW);
  Serial.println("Start:");
  Serial.println("Enter pulse time in microseconds:");
}

void loop(){
  if(Serial.available()>0){
    delay(15);
    buffer = 0;
    currentPulse = pulseTime;
    pulseTime= 0;
    while(Serial.available()>0){
      buffer = Serial.read();
      pulseTime = pulseTime*10 + (buffer-'0');
    }
    if(pulseTime%10!=0){
      modSub = pulseTime%10;
      pulseTime -= modSub;
    }
    Serial.print("Pulse time is ");
    Serial.println(pulseTime);
  }
  if(micros() >= stamp+waitTime){
    stamp = micros();
    if(currentPulse!=pulseTime){
      if(currentPulse<pulseTime){
        currentPulse+=10;
      }
      if(currentPulse>pulseTime){
        currentPulse-=10;
      }
    }
    digitalWrite(servoPin, HIGH);
    delayMicroseconds(currentPulse);
    digitalWrite(servoPin, LOW);
    delay(10);
  }





.
.
.
.
.



unsigned int pulseTime = 1000;
unsigned long waitTime = 20000;
unsigned long waitTimeChange = 100;
unsigned long stamp;
unsigned long stampChange;
int servoPin = 6;
int buffer;
int changer;

void setup(){
  Serial.begin(9600);
  pinMode(servoPin, OUTPUT);
  digitalWrite(servoPin, LOW);
  Serial.println("Start:");
  Serial.println("Enter rotation speed in milliseconds:");
}

void loop(){
  if(Serial.available()>0){
    delay(15);
    waitTimeChange= 0;
    while(Serial.available()>0){
      buffer = Serial.read();
      waitTimeChange = waitTimeChange*10 + (buffer-'0');
    }
    if(waitTimeChange < 20){
      waitTimeChange = 20;
    }
    if(waitTimeChange > 1000){
      waitTimeChange = 1000;
    }
    Serial.print("Wait time between rotations is ");
    Serial.println(waitTimeChange);
  }
  if(pulseTime == 1000){
    changer = 0;
  }
  if(pulseTime == 2000){
    changer = 1;
  }
  if(micros() >= stamp+waitTime){
    stamp = micros();
    if(millis() >= stampChange+waitTimeChange){
      stampChange = millis();
      if(changer==0){
        pulseTime++;
      }
      if(changer==1){
        pulseTime--;
      }
    }
    digitalWrite(servoPin, HIGH);
    delayMicroseconds(pulseTime);
    digitalWrite(servoPin, LOW);
    delay(10);
  }
}


Thursday, February 9, 2012

Random fun!

Here's a song to play :)
(You and I... by IU)


int buzzerPin = 4;
int eighth = 150;
int quarter = 300;
int sixteenth = 75;
int e_rest = 188;
int q_rest = 375;
int s_rest = 98;

#include "pitches.h"

void setup(){
  Serial.begin(9600);
  Serial.println("start");
   tone(buzzerPin,NOTE_G5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_C6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_C6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_C6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_C6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_E6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_C6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_G5,quarter);
  delay(q_rest);
  tone(buzzerPin,0,200);
  delay(250);

  // end of first phrase

  tone(buzzerPin,NOTE_AS5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_G5,eighth);
  delay(e_rest);
  tone(buzzerPin,0,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_F5,eighth);
  delay(e_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_E5,quarter);
  delay(q_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);

  // end of first cascade


  tone(buzzerPin,NOTE_G5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_C6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_C6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_C6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_E6, eighth);
  delay(e_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_G6,quarter);
  delay(q_rest);

  //End second phrase

  tone(buzzerPin,NOTE_G5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_C6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_G6,eighth);
  delay(e_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_F6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_AS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_E6,eighth);
  delay(e_rest);
   tone(buzzerPin,NOTE_D6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_C6,eighth);
  delay(e_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);

// End second cascade


  tone(buzzerPin,NOTE_G6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_G6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_F6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_E6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_C6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
   tone(buzzerPin,NOTE_B5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_C6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_D6,quarter+eighth);
  delay(q_rest);


  tone(buzzerPin,0,eighth);
  delay(q_rest);

  tone(buzzerPin,NOTE_G6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_G6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_F6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_E6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_F6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_E6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_D6,eighth);
  delay(e_rest);
   tone(buzzerPin,NOTE_C6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_C6,quarter);
  tone(buzzerPin,NOTE_C6,quarter+quarter+eighth);
  delay(q_rest);
  tone(buzzerPin,0,quarter+eighth);
  delay(q_rest);

  //End third phrase

  tone(buzzerPin,NOTE_G5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_AS5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_G5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_F6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_G5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_F6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_E6,quarter);
  delay(q_rest);

//End third cascade

  tone(buzzerPin,NOTE_D6,quarter);
  delay(q_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_E5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_GS5,quarter+eighth);
  delay(q_rest);
  tone(buzzerPin,NOTE_FS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_FS5,quarter);
  delay(q_rest);

  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_GS5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_FS5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,quarter+eighth);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_E5, quarter+eighth);
  delay(q_rest);
  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_FS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_FS5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_GS5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_E6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_CS6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_CS6,quarter);
  delay(q_rest);
  tone(buzzerPin,0,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,quarter);
  delay(q_rest);


  tone(buzzerPin,NOTE_CS6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,quarter+eighth);
  delay(q_rest+e_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth+quarter);
  delay(q_rest+e_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS6,quarter);
  delay(q_rest);
  tone(buzzerPin,0, quarter);
  delay(q_rest);

  tone(buzzerPin,NOTE_E5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_FS5,quarter+eighth);
  delay(q_rest+e_rest);
  tone(buzzerPin,NOTE_E5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_E5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B4, quarter);
  delay(q_rest);



 tone(buzzerPin,NOTE_E5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_GS5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,eighth+quarter);
  delay(e_rest+q_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_A5,eighth+quarter);
  delay(e_rest+q_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_E6,quarter+quarter+eighth);
  delay(e_rest);

  tone(buzzerPin,NOTE_A5,quarter);
  delay(q_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS6,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,quarter);
  delay(q_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_B5,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_D6,eighth);
  delay(e_rest);
  tone(buzzerPin,NOTE_CS6,eighth+quarter+quarter);
  delay(e_rest+q_rest+q_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_B5,eighth+quarter+quarter);
  delay(e_rest+q_rest+q_rest);
  tone(buzzerPin,0,quarter);
  delay(q_rest);
  tone(buzzerPin,NOTE_A5,quarter+quarter+eighth);
  delay(q_rest+q_rest+e_rest);
}
void loop(){
}


To play this, you also need "pitches.h" which consists of the following:

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Enjoy!

Lab 3 Exercises

Part 1. "Use Serial.read() to control the Arduino."


If you recall, Winslow used the Serial.read() command before in Lab 2 to control our LED brightness...


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++;
}


And he also used this to control the Arduino in the custom sounds part of the last lab. Hooray!


COLORPAL




#include <SoftwareSerial.h>
SoftwareSerial Color90(2, 3);  // rx = 2, tx = 3

int red;
int grn;
int blu;
float red_corrected;
float grn_corrected;
float blu_corrected;

int Kr = 10;
int Kg = 6;
int Kb = 14;
int Wr = 244;
int Wg = 172;
int Wb = 348;

char objectColor;

int gotcolor = 0;
int letter;

void setup()
{
  Serial.begin(9600); // Start communication with serial port read value
  Color90.begin(4800); // Send signal to led to read value

  pinMode(2,INPUT); // serial pin out from color pal
  pinMode(3,INPUT); // from same serial pin, signal pulls up, sends, pulls down, reads
  digitalWrite(2,HIGH); // Enable the pull-up resistor
  digitalWrite(3,HIGH); // Enable the pull-up resistor

  pinMode(2,OUTPUT); // send signal out
  pinMode(3,OUTPUT);
  digitalWrite(2,LOW); // turn pin off so pin 3 can go high
  digitalWrite(3,LOW);

  pinMode(2,INPUT); // Input signal to print
  pinMode(3,INPUT);

  Serial.println("Pass 1");
//  delay(20);

  while( digitalRead(2) != HIGH || digitalRead(3) != HIGH ) {
    Serial.println("In the loop");
    delay(50);
  }

  Serial.println("Pass 2");

  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  delay(100);     // spec is 80, but not all ColorPAL units work with 80

  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
  delay(100);

}

// This oscillates back and forth on one wire to turn off led, send signal,
// turn on led, read signal. very fast strobe read - arduino is not capable of
// one wire signal communication over digital ports, so this is a way around
// that over 2 wires communicating with 1 pin on the sensor.
//---------------------------------

void loop()
{
  readcolor();
  Serial.print("R");
  Serial.print(red);
  Serial.print("   G");
  Serial.print(grn);
  Serial.print("   B");
  Serial.println(blu);
  Serial.print("Rc");
  Serial.print(red_corrected);
  Serial.print("   Gc");
  Serial.print(grn_corrected);
  Serial.print("   Bc");
  Serial.println(blu_corrected);
  Serial.println(objectColor);
  gotcolor = 0;
  delay(100);

}
void readcolor() {  // Reads ColorPAL, putting results in the red,grn,blu variables

  char rByte[9];
  char dummy[4];

  delay(20);
  Color90.begin(4800);
 Color90.print("= (00 $ m) !");  // set up loop to continuously send color data
  pinMode(3,INPUT);
  gotcolor = 0;
  while (gotcolor == 0) {
    rByte[0] = Color90.read();
    if( rByte[0] == '$' ) {
      gotcolor = 1;
      for(int i=0; i<9; i++) {
        rByte[i] = Color90.read();
//        Serial.print(rByte[i]);
      }
      Serial.println("");
      dummy[0] = rByte[0];
      dummy[1] = rByte[1];
      dummy[2] = rByte[2];
      dummy[3] = 0;

      red = strtol(dummy,NULL,16);
      red_corrected = (float)255*(red-Kr)/(Wr-Kr);

      dummy[0] = rByte[3];
      dummy[1] = rByte[4];
      dummy[2] = rByte[5];

      grn = strtol(dummy,NULL,16);
      grn_corrected = (float)255*(grn-Kg)/(Wg-Kg);

      dummy[0] = rByte[6];
      dummy[1] = rByte[7];
      dummy[2] = rByte[8];

      blu = strtol(dummy,NULL,16);
      blu_corrected = (float)255*(blu-Kb)/(Wb-Kb);
   
      if(red_corrected > 25+grn_corrected && red_corrected > 25+blu_corrected){
        objectColor = 'R';
      }
      else if(grn_corrected > 25+red_corrected && grn_corrected > 25+blu_corrected){
        objectColor = 'G';
      }
      else if(blu_corrected > 25+grn_corrected && blu_corrected > 25+red_corrected){
        objectColor = 'B';
      }
      else if(red_corrected > 240 && grn_corrected > 240 && blu_corrected > 240){
        objectColor = 'W';
      }
      else if(red_corrected < 15 && grn_corrected < 15 && blu_corrected < 15){
        objectColor = 'K';
      }
      else{
        objectColor = '?';
      }
    }
  }
}

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!