LAB #4 - lannieme/interactiveDevicesDesign GitHub Wiki
Part A. Revisiting Blink
1. Blinking LEDs with Arduino
a. What line(s) of code do you need to change to make the LED blink (like, at all)? Need to change the reference to the pin number to 9 which LED connects to in the following 3 lines.
digitalWrite(9, HIGH);
digitalWrite(9, LOW);
b. What line(s) of code do you need to change to change the rate of blinking?
delay(1000);
We can change the number here to change the rate of blinking.
c. What circuit element would you want to add to protect the board and LED?
Resistance.
2. Digitally toggle LEDs on and off using Arduino
a. Which lines do you need to modify to correspond with your button and LED pins?
const int ledPin = 9; // the number of the LED pin
b. Modify the code or the circuit so that the LED lights only while the button is depressed. Include your code in your lab write-up.
Used the design, so no need to change the high/low state to make sure lights only show up when button is pressed.
// read the state of the pushbutton value: buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH); } else {
// turn LED off:
digitalWrite(ledPin, LOW); }
}
3. Fading LEDs on and off using Arduino
a) Which line(s) of code do you need to modify to correspond with your LED pin?
int ledPin = 9; // LED connected to digital pin 9
b) How would you change the rate of fading?
We can change the fadeValue increment here to change the rate of fading.
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 50) {
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 50) {
c) (Extra) Since the human eye doesn't see increases in brightness linearly and the diode brightness is also nonlinear with voltage, how could you change the code to make the light appear to fade linearly?
We can change the increment on fadeValue to a non-linear function, e.g. log function, exponential function
Part B. Advanced Inputs
1. Potentiometer
2. Force Sensitive Sensor
a. What resistance values do you see from your force sensor?
anything range from 0 to 1023
b. What kind of relationship does the resistance have as a function of the force applied? (e.g., linear?)
It’s seems linear but based on reference here, it's actually a log relationship - the higher the force, the lower the frequency the LED light blinks, also higher the resistance.
c. Can you change the LED fading code values so that you get the full range of output voltages from using your FSR?
Yes, it’s doable. The FSR value ranges from 0 to 1023, while the value for LED ranges from 0 to 255(based on Arduino reference here. We could be change the value to LED with sensorValue/4(approximately) in analogWrite(ledPin, sensorValue/4)
function.
Part C. LCD
a. What voltage level do you need to power your display? b. What voltage level do you need to power the display backlight?
5V is needed to power the display. 3V is needed to power the display backlight.
b. What was one mistake you made when wiring up the display? How did you fix it?
I reversed the order of the pin on LCD due to the graph here - thought pin 1 is pin16, pin 16 is the pin 1 etc, which confused me for a long time. The backlight showed up and something like cursor showed up on the LCD, but wasn’t function as expected.. I examined all the wires and the LCD. And then I noticed that there is PIN number indicator in the back of the LCD and realized that I set it up in the reverse order. Fixing this issue solved my problem.
c. What line of code do you need to change to make it flash your name instead of "Hello World”?
d. Include a copy of your Lowly Multimeter code in your lab write-up.
e. Include a copy of your FSR thumb wrestling code in your lab write-up.
It will print the largest sensor value and which one of the FSR won on the LCD screen.
Part D. Timer - Nyan Cat Timer
Video:
Timer Video
Explanation:
After press the FSR, it will automatically start a countdown of 10 seconds. During the countdown, the remaining time will show up on the LCD screen. Once the time is up, it will show on the LCD screen that “Time’s up!” and then the LED light will light orange. And then the speaker will start to sing Nyan cat.
Reference
For creating the Nyan cat sound through speaker, I utilize this Creative Common licensed code from Github - Nyan_Cat.ino
Code:
Part E. Tone
a. How would you change the code to make the song play twice as fast?
Need to change the noteDuration to double of its original duration. Like following:
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 16, 16, 8, 8, 8, 8, 8
};
b. What song is playing?
It's the opening theme song from Star Wars.