EE47 Lab 3
Tom Cohlmia
4/19/2013
A.a. I would change the duration numbers from 4 to 8 and 8 to 16.
4, 8, 8, 4,4,4,4,4 to 8, 16, 16, 8,8,8,8,8
A.b. The Star Wars theme!
B.a. 5V
B.b. I initally set the potentiometer too high (or low) and couldn't see anything. I had to re-upload and adjust the dial. I also needed to turn on the backlight because it was really hard to see.
B.c. I changed lcd.print("hello, world!"); to lcd.print("Tom Cohlmia");
C.1.a. LED dimming code using A0:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, 0, 1024, 5, 250);
// turn the ledPin on
analogWrite(ledPin, sensorValue);
// stop the program for <sensorValue> milliseconds:
Serial.println(sensorValue);
delay(30);
}
C.2.a. Flex sensor range: 22.5-58 kOhms
C.2.b.
R1 = 22.5-58 kOhms
R2 = 24 kOhms
Vi = 3.3V
Vo = Vi*R2/(R1+R2)
Vo = 0.96-1.70V
C.2.c. The output ranges I am capable of doing are very different from the inputs I'd like to use for the LED, so I need to use map() to convert them into something more useful.
C.2.d.
Code using a flex sensor to adjust an LED's brightness, using the LCD to output the V0 measured at Analog ipnut A0.
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
lcd.print(sensorValue);
Serial.println(sensorValue);
sensorValue = map(sensorValue, 85, 220, 5, 250);
// turn the ledPin on
analogWrite(ledPin, sensorValue);
// stop the program for <sensorValue> milliseconds:
delay(100);
lcd.clear();
}
C.3.a. 0.25-INF kOhms
C.3.b. It seems to be reverse exponential; at first it's infinite resistance
C.3.c. Copy of thumb wrestling code:
// include the library code:
#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 the potentiometer
int sensorPin2 = A1; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue1 = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
lcd.println(sensorValue1);
lcd.print(sensorValue2);
Serial.println(sensorValue1);
sensorValue1 = map(sensorValue1, 0, 700, 5, 250);
// turn the ledPin on
analogWrite(ledPin, sensorValue1);
// stop the program for 100 milliseconds:
delay(100);
lcd.clear();
}
D. Timer code:
// include the library code:
#include <LiquidCrystal.h>
#include "pitches.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 the potentiometer
int sensorPin2 = A1; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue1 = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0; // variable to store the value coming from the sensor
int startcheck = 0;
long timerset = 0;
int melody[] = {
NOTE_C6, NOTE_D6,NOTE_C6, NOTE_D6, NOTE_C6, NOTE_D6, NOTE_C6, NOTE_D6};
int noteDurations[] = {
16,16,16,16,16,16,16,16 };
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("<- Squeeze 1 to");
lcd.setCursor(0, 1);
lcd.print("set timer");
}
void loop() {
// read the value from the sensor:
while (startcheck==0) {
if (analogRead(sensorPin1) > 300) {
startcheck = 1;
lcd.display();
}
}
lcd.clear();
while (analogRead(sensorPin1) > 300) {
timerset = timerset + 5000;
lcd.setCursor(0, 0);
lcd.print(timerset/1000);
lcd.setCursor(0, 1);
lcd.print(" seconds");
delay(500);
lcd.clear();
Serial.println(timerset);
}
lcd.setCursor(0, 0);
lcd.print(timerset/1000);
lcd.print(" seconds");
lcd.setCursor(0, 1);
lcd.print("press 2 to GO!");
while (analogRead(sensorPin2) < 300) {
}
while (timerset > 0) {
timerset = timerset - 1000;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown:");
lcd.setCursor(0, 1);
lcd.print(timerset/1000);
lcd.print(" seconds");
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time's Up!");
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 = 1000/noteDurations[thisNote];
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;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time's Up!");
lcd.setCursor(0, 1);
lcd.print("Press 2 to Reset");
while (analogRead(sensorPin2) < 300) {
lcd.display();
}
lcd.clear();
startcheck = 0;
timerset = 0;
lcd.setCursor(0, 0);
lcd.print("<- Squeeze 1 to");
lcd.setCursor(0, 1);
lcd.print("set timer");
}
Comments (0)
You don't have permission to comment on this page.