Eunice_Sensors+Input
Made by yuany4
Made by yuany4
Set up a push bottom and sensor for the circuit!
Created: October 31st, 2024
Set up a circuit that have input, sensor and output on it.
This time, I used two breadboards to accommodate my three circuit components: an input (push button), a sensor (force-sensitive resistor), and an output (green LED). The push button serves as the switch, while the force-sensitive resistor controls the LED's brightness. I wanted to create a more interactive experience for the user, so instead of using a traditional potentiometer, I opted for a sensor that introduces an element of unpredictability. This requires users to apply force slowly, encouraging them to explore and interact with the device multiple times. While this approach might seem less user-centered, it adds a playful and surprising dimension, as users discover how their touch influences the light.
Initially, I tried using a single breadboard, but I soon realized it wasn’t large enough to accommodate all the components. So, I switched to two boards. Following the circuit design provided in class, I combined the code for both the sensor and the input. Debugging proved to be the most challenging part, especially the output code. I had to map the sensor’s analog values correctly and integrate them with the switch logic, which was tricky and occasionally confusing. To make the LED brightness more responsive to the sensor, I not only changed the LED pin from D2 to D1 but also reduced the sensor’s range from 4095 to 2500 for finer control.
I plan to make two improvements. First, I’ll switch from a push button to a maintained switch. This way, I won’t need to keep pressing the button to maintain the connection—simply flipping the switch will keep the circuit on. Second, I’d like to enhance the interaction by having the force sensor not only control the brightness but also change the LED color.
//Eunice's Lab2: Sensors+Inputs
int buttonPin = D3; // Pin for button
int fsrPin = A0; // Pin for force sensor
int fsrReading = 0; // Variable to store force sensor reading
int ledGreen = D1; // Pin for green LED (D1 performance is better than D2)
int ledBrightness = 0; // Variable for LED brightness
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up
pinMode(ledGreen, OUTPUT); // Set LED pin as output
Particle.variable("force", &fsrReading, INT); // Register force reading to the cloud
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read button state
// Read force sensor value
fsrReading = analogRead(fsrPin);
Serial.println(fsrReading); // Allow the Terminal on Mac to test it
// The sensor’s metric goes up to 2500 (0-2500) to LED brightness (0-255)
ledBrightness = map(fsrReading, 50, 2500, 0, 255);
// Set the LED brightness using PWM
if (buttonState == HIGH) {
// When the switch is off (or the button isn’t pressed), the LED will be turned off
digitalWrite(ledGreen, LOW);
}
else {
// When the button is pressed, then the LED will be turned on
analogWrite(ledGreen, ledBrightness);
}
// Wait 100 milliseconds before looping
delay(100);
}
Click to Expand
Set up a push bottom and sensor for the circuit!