Skills Dev II: Working with Inputs & Sensors

Made by Justin Slusher ·

In this project, my goal was to build a system using a flex sensor and LEDs, where the flex sensor's bend degree controls the lighting behavior. I then added a push button switch to alter the interaction between the flex sensor and the LEDs -- allowing the LEDs to respond differently when the button is pressed or not.

Created: November 8th, 2023

0

Skill Dev II: Working with Inputs & Sensors

Project Summary:

In this project, my goal was to build a system using a flex sensor and LEDs, where the flex sensor's bend degree controls the lighting behavior. I then added a push button switch to alter the interaction between the flex sensor and the LEDs -- allowing the LEDs to respond differently when the button is pressed or not. 

In each below exercise, the process and outcome are explained. 

0

Flex Sensor & Two LEDs (Video Below):

In this setup, the flex sensor is connected to the analog pin A0 on the breadboard to measure bend pressure, while two LEDs are connected to digital pins Red (D3) and Orange (D2), acting as visual indicators. The code reads the flex sensor's value, mapping its bend to defined thresholds to determine LED behavior: a medium bend triggers the red LED, and a high bend activates both LEDs.   

0
0

Flex Sensor, Push Button Switch, & Two LEDs (Video/Code Below):

In this setup, I added a push button switch to the breadboard, connected to digital pin D4. This is alongside the existing flex sensor and two LEDs. With adding the push button, the code now can operate in more than one method. For example, when the button is not pressed, the flex sensor's bend dictates LED behavior, lighting up the red LED for medium bends and both LEDs for high bends (as shown in the previous example). However, when the button is pressed – the LEDs blink quickly ten times for high bends, and blink slowly three times in an alternating fashion for medium bends. 

0
1
// Using a push button and a flex sensor at different bend thresholds
// Assigning pins between the two led lights, button switch, and the flex sensor
int ledPin1 = D3; // First LED (Red)
int ledPin2 = D2; // Second LED (Orange)
int buttonPin = D4; // Push button switch
int flexPin = A0;  // Flex sensor

// Thresholds for flex sensor readings
const int mediumBend = 700; // Threshold for medium bend
const int highBend = 1000;   // Threshold for high bend

void setup() {
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(flexPin, INPUT);

    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
}

void loop() {
    int flexValue = analogRead(flexPin);

    if (digitalRead(buttonPin) == LOW) {
        // Button is pressed
        if (flexValue >= highBend) {
            // High bend - Blink lights quickly 10 times
            for (int i = 0; i < 10; i++) {
                digitalWrite(ledPin1, HIGH);
                digitalWrite(ledPin2, HIGH);
                delay(50); // Short delay for quick blink
                digitalWrite(ledPin1, LOW);
                digitalWrite(ledPin2, LOW);
                delay(50); 
            }
            delay(1000); // Pause for a second after blinking
        } else if (flexValue >= mediumBend) {
            // Medium bend - Alternate blinking three times
            for (int i = 0; i < 3; i++) {
                digitalWrite(ledPin1, HIGH);
                digitalWrite(ledPin2, LOW);
                delay(500); 
                digitalWrite(ledPin1, LOW);
                digitalWrite(ledPin2, HIGH);
                delay(500); 
            }
            digitalWrite(ledPin2, LOW); // Both LEDs are off after blinking
            delay(1000); // Pause for a second after blinking
        } else {
            // Below medium bend - both LEDs off
            digitalWrite(ledPin1, LOW);
            digitalWrite(ledPin2, LOW);
        }
    } else {
        // Button is not pressed
        if (flexValue >= mediumBend && flexValue < highBend) {
            // Medium bend - only RED LED on
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, LOW);
        } else if (flexValue >= highBend) {
            // High bend - both LEDs on
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
        } else {
            // Below medium bend - both LEDs off
            digitalWrite(ledPin1, LOW);
            digitalWrite(ledPin2, LOW);
        }
    }
}
Click to Expand
0

Next Steps:

One of the items that I would like to try that I did not get to was trying to see how I could potentially get two different sensors to work with one another. I would have liked to see if I could create a multi-sensor criteria where after more than one parameter was met then that would create a different output, maybe not on a LED light this time. 

Reflection:

One of the primary obstacles I encountered was understanding why I needed create the flexValue variable as that seemed like a gap in my understanding until I figured it out. After I figured this out, it became easier to understand how I would make this variable <>=, to the various flex sensor bend thresholds that I was creating. To add, the bend thresholds were a little tricky to dial in. To little or to much would allow it to be to easily triggered or not at all. If the interval between the two bend pressure measurements were not set wide enough either then you couldn't make it distinct enough between to two readings that I was trying to create. 

x
Share this Project

This project is only accessible by signed in users. Be considerate and think twice before sharing.


Courses

48-675 Designing for the Internet of Things 24

· 11 members

A hands-on introductory course exploring the Internet of Things and connected product experiences.


About

In this project, my goal was to build a system using a flex sensor and LEDs, where the flex sensor's bend degree controls the lighting behavior. I then added a push button switch to alter the interaction between the flex sensor and the LEDs -- allowing the LEDs to respond differently when the button is pressed or not.