lab: analog input

First attempt at soldering.

It’s not pretty and the right side is a little charred. But what matters for now is that it still conducts.

This week’s lab introduced us to Analog Input. This first round utilized the Analog In code provided in the Arduino Examples. What I was hoping to accomplish was an LED that would turn on when the FSR (force sensitive resistor) was pressed and completely off when not pressed. However, the LED seemed to stay on dimly even when the FSR was not in use. When the FSR was pressed slightly, the LED would increase in brightness. When more pressure was applied to the FSR, the LED would flicker.

Code:
int sensorPin = 0;
int ledPin = 9;
int sensorValue = 0;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}

In this second round, I incorporated IF and ELSE statements into the code, which seemed to fix the problem of the flickering lights. Although I’m still not entirely sure why the previous code did not work.

Code:
int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 9; // 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);

if(sensorValue > 90) { //maximum value at about 112 (when pressed hardest)
digitalWrite(ledPin,HIGH);
}
else {
digitalWrite(ledPin,LOW);
}
Serial.println(sensorValue);
}

Finally, I also explored using the FSR in controlling the brightness of the LED based on how much pressure was applied to the sensor.

Code:
int fsrPin = 0;
int sensorValue = 0;
int led = 9; //PWM: Pulse Width Modulation (Basically a digital pin that can provide analog results.)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(led, OUTPUT);
}

void loop() {
sensorValue = analogRead(fsrPin); // read the FSR value
int brightness = map(sensorValue, 10, 112, 0, 255); // FSR values max at 112

analogWrite(led, brightness); // set the LED brightness with the result
Serial.println(sensorValue);
delay(10);
}

And finally, 1 FSR and 3 LEDs.

Leave a comment