Loebner Keith Lab 3


Part A:

a. How would you change the code to make the song play twice as fast?

Decrease the duration of the notes as well as the pause between notes by 50%, such as in the following code:

void setup() {

  // iterate over the notes of the melody:

  for (int thisNote = 0; thisNote < 8; thisNote++) {

 

    // to calculate the note duration, take one second 

    // divided by the note type.

    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

    int noteDuration = 0.5*1000/noteDurations[thisNote]; // EDIT HERE

    tone(8, melody[thisNote],noteDuration);

 

    // to distinguish the notes, set a minimum time between them.

    // the note's duration + 30% seems to work well:

    int pauseBetweenNotes = noteDuration * 1.30 * 0.5; // EDIT HERE

    delay(pauseBetweenNotes);

    // stop the tone playing:

    noTone(8);

  }

}

b. What song is playing? ;-)

The STAR WARS theme

Part B

a. What voltage level do you need to power your display?

It appears to accept a range, between approximately 3.0 V and 5.5 V (one spec quotes up to 11 V, unclear if this is necessary / wise)

b. What was one mistake you made when wiring up the display? How did you fix it?

I was cutting wires too short, so I left the Flatlander camp for a minute to get it wired up. I didn't make any pinout mistakes, though.

c. What line of code do you need to change to make it flash your name instead of "Hello World"?

the lcd.print() line, from lcd.print("hello, world!"); to lcd.print("KEITH");

Part C.

1.

a. Post a copy of your new code in your lab writeup.

void loop() {

  lcd.setCursor(0, 0);

  // read the value from the sensor:

  sensorValue = analogRead(sensorPin);

  //display the sensor value on the LCD screen

  lcd.print(sensorValue);

  // turn the ledPin on

  analogWrite(ledPin, sensorValue / 4);   

}

2.

a. What resistance do you see with a Multimeter when the sensor is flat? When it is bent?

When flat, we see ~10.8 kOhms. When bent, ~30 kOhms.

b. What kind of voltages should we expect for the Arduino analog pin based on the sensor resistance?

This is a voltage divider circuit, so the ranges we expect to see with a variable resistor such as that outlined above and a constant 27 kOhm resistor are from ~1.56 to 2.36 volts, with an input voltage of 3.3 V. 

c. How does the range of the LED's brightness change compared to the potentiometer?

Very little, almost imperceptibly.

d. Include a copy of your Lowly Multimeter code in your lab write-up.

void loop() {

  lcd.setCursor(0, 0);

  // read the value from the sensor:

  sensorValue = analogRead(sensorPin);

  //display the sensor value on the LCD screen

  lcd.print(sensorValue);

  // the read value goes from 333 to 513 or so

  int fadeValue = (515 - sensorValue) * (255.0/181.0);

  lcd.print(' ');

  lcd.print(fadeValue);

  // turn the ledPin on

  analogWrite(ledPin, fadeValue);             

}

3.

a. What resistance values do you see from your force sensor?

When pressing very hard, I see something like ~400-450 Ohms, but while barely touching it there is something on the order of 0.5-1 MOhm, but the precision is very low.

b. What kind of relationship does the resistance have as a function of force applied? (e.g., linear?)

The variation does not appear to be linear, although I have a relatively poor intuitive feeling as to what it feels like to apply linear force. It does fall off something "like" exponentially as more force is applied, however. 

c. Include a copy of your FSR thumb wrestling code in your lab write-up.

/*

  ThumbWar

Reads the output of two voltage divider circuits, each with a different FSR

governing the signal voltage. The LCD screen displays the player (1 or 2) that

is winning the thumb war.

 

 The circuit:

 * FSR attached to analog input 0

 * FSR attached to analog input 1

 * LCD wired to appropriate pins

 

 

 

 Created by Keith Loebner

 15 July 2014

 */

 

#include <LiquidCrystal.h>

 

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

int sensorPin1 = A0;    // select the input pin for player 1

int sensorPin2 = A1;    // select the input pin for player 2

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

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

 

void setup() {  

  lcd.begin(16,2);

}

 

void loop() {

  lcd.setCursor(0, 0);

  // read the values from the sensors:

  sensorValue1 = analogRead(sensorPin1);

  sensorValue2 = analogRead(sensorPin2);

 

  if(sensorValue1 > sensorValue2){

    lcd.print("Player 1 winning");

  }

  else if(sensorValue2 > sensorValue1){

    lcd.print("Player 2 winning");

  }

  else{

    lcd.print("It's a tie!");

  }

  // clear screen for the next loop:

  lcd.clear();

 

}

Part D.

a. Make a short video showing how your timer works, and what happens when time is up!

Here is the code:

/*

 Timer!

 

 This timer uses two FSR's to enter the time in seconds until the alarm rings, 

 playing the star wars theme song!

 */

 

// include the library code:

#include <LiquidCrystal.h>

#include "pitches.h"

 

// notes in the alarm melody

int melody[] = {

  NOTE_D3,NOTE_D3,NOTE_D3,NOTE_G3,NOTE_D4,NOTE_C4,NOTE_B3,NOTE_A3,NOTE_G4,NOTE_D4, \

  NOTE_C4,NOTE_B3,NOTE_A3,NOTE_G4,NOTE_D4,NOTE_C4,NOTE_B3,NOTE_C4,NOTE_A3,0};

 

// notes durations

int noteDurations[] = {

  10,10,10,2,2,10,10,10,2,4, \

  10,10,10,2,4,10,10,10,2,4};

 

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

int upPin = A0; // the pin attached to the increment sensor

int downPin = A1; // the pin attached to the decrement sensor

int upValue = 0; //

int downValue = 0; //

int timerSec = 0;

int alarmFlag = 0;

 

void setup() {

  // set up the LCD's number of columns and rows: 

  lcd.begin(16, 2);

  // Print a message to the LCD.

  lcd.print("Enter Time:");

}

 

void loop() {

  // set the cursor to column 0, line 1

  // (note: line 1 is the second row, since counting begins with 0):

  lcd.setCursor(0,1);

 

  // determine current state, and set timerSec appropriately

  if(alarmFlag == 0){

    lcd.print(timerSec);

  }

  else{

    lcd.clear();

    alarmFlag = 0;

    lcd.setCursor(0,0);

    lcd.print("Enter Time:");

    timerSec = 0;

    lcd.setCursor(0,1);

    lcd.print(timerSec);

  }

 

  upValue = analogRead(upPin);

  downValue = analogRead(downPin);

 

  if(upValue > 100 && downValue < 100){ // max reading is around 680

    delay(200); // make sure it's a real button press

    upValue = analogRead(upPin);

    if(upValue > 100 && downValue < 100){

      timerSec++;

    }

    //delay(1000); // wait to avoid double counting single presses

  }

 

  if(downValue > 100 && upValue < 100){

    delay(200);

    downValue = analogRead(downPin);

    if(downValue > 100 && upValue < 100 && timerSec > 0){ // don't set negative times!

      timerSec--;

    }

    //delay(1000);

  }

 

  // initiate the timer by squeezing both at the same time

  if(upValue > 100 && downValue > 100){

    lcd.clear();

    lcd.setCursor(0,0);

    lcd.print("Tick, tock!");

    int currTime = millis()/1000; // current time in seconds

    while(currTime + timerSec >= millis()/1000){

      int secsRemain = currTime + timerSec - millis()/1000;

      lcd.setCursor(0,1);

      if(secsRemain/10 == 0){ // number less than 10

      lcd.print('0');

      lcd.print(currTime + timerSec - millis()/1000);

      }

      else{

      lcd.print(currTime + timerSec - millis()/1000); // print the remaining secs

      }

    }

    lcd.setCursor(0,1);

    lcd.print('0');

    lcd.print('0');

    //play the alarm!

  for (int thisNote = 0; thisNote < 20; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];

    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;

    delay(pauseBetweenNotes);

    noTone(8);

  }

  alarmFlag = 1;

 }

}

 

 

b. Post a link to the Lab 3 Timers Hall of Fame.

 

done.