Isabel - Inputs + Sensors
Made by Isabel Fleck
Made by Isabel Fleck
Combining sensors and inputs in a little warning device that once on, blinks when pressure is applied.
Created: November 18th, 2024
Using inputs and sensors, I wanted to create a device that worked as a warning, creating a blinking response when pressure is applied, and if possible have a series of leds turn on as the pressure increases. Thinking about pressure dials and how the dial turns to different colors as the pressure increases until eventually turning red, I thought that this could be a simple interaction translated to light to signals when pressure is applied, and if possible how much pressure.
After completing the practice exercises on inputs and sensors, I started by taking the base setup and code for the light sensor, replacing it with the pressure sensor. Looking back at the pushbuttons, I connected this to the board as well, and restructured my code, so that the button had controlled states, connected to an if else statement. Within this statement here I applied the conditions for the led and included the photoCellReading here, so that there would be two states for the led. Once I had this working, I then progressed to add a second led to my board, but quickly realized that my board was too small to fit multiple leds, and had to quickly drop this idea. But maybe in the future with more time, I can look into using the individually addressable led to signal different stages of pressure.
// Practice Exercise
// Creating a circuit that warns when pressure is applied. When the button is pressed, the led turns on, and when pressure is applied above 600, the led begins to blink.
int photoCellPin = A0; // Pin for the photo cell
int buttonPin = D4; // Pin for the pushbutton
int ledPin = D1; // Pin for the LED
// Variable to hold the light reading
int photoCellReading = 0;
// Variable to track the LED state
bool ledOn = false;
// Variable to track the button press state
bool buttonPressed = false;
void setup()
{
// Set up pins for input/output
pinMode(photoCellPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(ledPin, OUTPUT);
// Create a cloud variable of type integer called 'light' mapped to photoCellReading
Particle.variable("light", &photoCellReading, INT);
}
void loop()
{
// Read the photo cell value (0-4095)
photoCellReading = analogRead(photoCellPin);
// Check the pushbutton state
if (digitalRead(buttonPin) == LOW && !buttonPressed) { // Button pressed
ledOn = !ledOn; // Toggle the LED state
buttonPressed = true; // Prevent multiple toggles
}
else if (digitalRead(buttonPin) == HIGH) { // Button released
buttonPressed = false; // Reset button press tracking
}
// Control the LED
if (ledOn) {
if (photoCellReading > 600) {
// Blink the LED
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
else {
// Turn the LED on steadily
digitalWrite(ledPin, HIGH);
}
}
else {
// Turn the LED off
digitalWrite(ledPin, LOW);
}
}
Click to Expand
Overall, I would like to find a way to make the response a little more appealing, and figure out how to make a second and third led work that will each turn on at a different range of pressure. So when the most pressure is applied, all three leds will be on. I did enjoy making this little circuit, and even though I thought that this interaction would be simple, it was interesting to have to think about the way in which this applied in my code, and knowing where and how to apply the else if statements.
Combining sensors and inputs in a little warning device that once on, blinks when pressure is applied.