| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Get control of your email attachments. Connect all your Gmail accounts and in less than 2 minutes, Dokkio will automatically organize your file attachments. You can also connect Dokkio to Drive, Dropbox, and Slack. Sign up for free.

View
 

harrymj Lab 3

Page history last edited by Harry Johnson 7 years, 2 months ago

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

I would modify line 35 to:

 int noteDuration = 500/noteDurations[thisNote];

By dividing 500 by the duration instead of 1000 by the duration, the time each note is played (and by virtue of lines 40&41 the time between the notes) and will be reduced by half.

 

b. What song is playing? ;-)

The Star Wars main theme is played.

 

 

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

When I first placed the LCD, I didn't leave enough room to put the potentiometer in the place I wanted. (I have a strong sense of wiring feng shui). So I had to move it over, and move the two power/ground wires that I already placed. 

 

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

  lcd.print("hello, world!"); needs to be replaced with lcd.print("Harry"); Note that when I wired the LCD, I used a different set of wiring than was included in the original schematic. So I also had to change the initialization line as well.

 

 

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


 

void loop() {

  // read the value from the sensor:

  sensorValue = analogRead(sensorPin);    

  analogWrite(13, sensorValue>>2); //analogWrite uses 0-255, analogRead uses 0-1023. Need to divide by four, it's faster to shift right by two.

  delay(50);                 

}


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

Flat: 24K

Bent: 50K

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

Flat: 2.5V

Bent: (24/(24+50))*5V =  1.62V

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

Drastically reduced range.


#include <LiquidCrystal.h>

 

int oldSensorVal = 0;

int sensorVal = 0;

int ledPin = 13; 

 

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

LiquidCrystal lcd(8, 7, 12, 11, 10, 9);

 

void setup() {

  pinMode(A0, INPUT);

  pinMode(ledPin, OUTPUT);

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

  lcd.begin(16, 2);

  // Print a message to the LCD.

  lcd.print("hello, world!");

}

 

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);

  oldSensorVal = sensorVal; //for comparison

  sensorVal = analogRead(0); //new sensor value.

  if(oldSensorVal != sensorVal) { //only print if there's a new value. 

    lcd.print("    "); //clear the appropriate section (faster than having to clear the entire screen)

  lcd.setCursor(0, 1); 

  lcd.print(sensorVal);

  }

  analogWrite(13, map(sensorVal, 350, 550, 0, 255)); //non-flexed to flexed seems to range 350 - 550. Map this to LED being off-on. 

  delay(50);

}


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

0.3K-70K

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

After the initial drop from infinite impedance, it is an inversely proportional linear relationship. 

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


#include <LiquidCrystal.h>

 

int oldSensorVal = 0;

int sensorVal = 0;

int ledPin = 13; 

 

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

LiquidCrystal lcd(8, 7, 12, 11, 10, 9);

 

void setup() {

  pinMode(A0, INPUT);

  pinMode(ledPin, OUTPUT);

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

  lcd.begin(16, 2);

  // Print a message to the LCD.

  lcd.print("Thumb Wrestling!");

}

 

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);

  sensorVal = analogRead(0); //new sensor value.

  lcd.setCursor(map(sensorVal, 0, 1023, 15, 0 ), 1); //Print a # at the appropriate point based on who is winning. (0 = right team winning, 1023 = left team winning).

  lcd.print("#");

  delay(50);

  lcd.setCursor(map(sensorVal, 0, 1023, 15, 0 ), 1); //Clear the character. 

  lcd.print(" ");

}


 

 

Thumb Wrestling/Timer!

 

/*

ThumbWrestling.ino

Harry Johnson

Stanford University EE47, Spring 2012-2013

Implements a thumb wrestling game with two force sensors, a button, an LCD, and a speaker.

 

The game pits two players against each other to see who can squeeze the flex sensor harder.

 

It waits for a button press to start the game. During the game's operation, the LCD displays the current prospective winner, 

how long they need to hold on for to win, and a graphical representation of the current balance. 

 

The two force gauges are used as a voltage divider.

 

5V -> 4.7K (current limit) -> Gauge 1 -> Gauge 2 -> 4.7K -> GND.

*/

#include <LiquidCrystal.h>

 

 

const int soundPin = 6; //speaker on pin 6

const int buttonPin = 2; //button on pin 2.

 

const int timeout = 5; //5 seconds in one state = win. 

long int startTime;  //startTime is refreshed with current millis() value on each "turnover" between one team winning and another. 

long int curTime; //curTime is how many seconds are left until the current team wins. 

 

int sensorVal = 0;

int gameState = 0; // State of game. 

//0: not started

//1: Player 1 winning

//2: Player 2 winning

//3: Celebrating player 1!

//4: Celebrating player 2!

 

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

LiquidCrystal lcd(8, 7, 12, 11, 10, 9);

 

void setup() {

  pinMode(A0, INPUT);

  pinMode(soundPin, OUTPUT);

  pinMode(buttonPin, INPUT);

  digitalWrite(buttonPin, HIGH); //enable internal pullups. 

 

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

  lcd.begin(16, 2);

  // Print a message to the LCD.

}

 

void loop() {

  sensorVal = analogRead(0); //new sensor value.

 lcd.setCursor(0, 0);

 lcd.print("                "); //clear first line of display

 lcd.setCursor(0, 0);

  switch(gameState) {

     case 0: gameState = state_zero(); break; //next state generated by return of current state. 

     case 1: gameState = state_one(); break;

     case 2: gameState = state_two(); break;

     case 3: gameState = state_three(); break;

     case 4: gameState = state_four(); break; 

  }

 lcd.setCursor(0, 0);

 delay(50); 

}

 

int state_zero() {

  lcd.print("Button To Start"); 

  lcd.setCursor(0,1);

  lcd.print("                "); //clear second line of display

  if(digitalRead(buttonPin) == LOW) return 1;

  return 0;

}

 

int state_one() { //Player one is winning.

  curTime = timeout - (millis() - startTime)/1000; //curTime represents how long the person has to hold on for by subtracting the delta-T since a turnover from the time required to win. 

  lcd.print("Team 1 wins in "); 

  lcd.print(curTime);

  if(curTime == 0) return 3; //KO, team one has won!

  displayMeter(sensorVal); //graphical display on line two. 

 

  if(sensorVal < 511) { //if team two has beaten team one, switch to the state of team two winning. 

  startTime = millis();

  return 2;

 }

 

 return 1;

}

 

int state_two() { //player two is winning. 

  curTime = timeout - (millis() - startTime)/1000;

  lcd.print("Team 2 wins in "); 

  lcd.print(curTime);

  if(curTime == 0) return 4; //KO, team two has won!

 

  displayMeter(sensorVal); 

 

  if(sensorVal > 511) { //if team one has beaten team two, switch. 

  startTime = millis();

  return 1;

 }

 return 2;

}

 

int state_three() {  //player one has won.

  lcd.print("Team One Wins!");

  tone(soundPin, 147, 1000);

  delay(4000);

  return 0;

}

 

int state_four() { //player two has won. 

   lcd.print("Team Two Wins!");

  tone(soundPin, 147, 1000);

  delay(4000);

  return 0;

}

 

void displayMeter(int val) { //takes a val from 0-1023 and 

    lcd.setCursor(map(val, 0, 1023, 15, 0 ), 1); //Print a # at the appropriate point based on who is winning. (0 = right team winning, 1023 = left team winning).

  lcd.print("#");

  delay(50);

  lcd.setCursor(map(val, 0, 1023, 15, 0 ), 1); //Clear the character. 

  lcd.print(" "); 

}

 


 

Comments (0)

You don't have permission to comment on this page.