Flex Sensor with Button ON/OFF Control

Made by Adwoa Asare

This is a flex sensor that can display low, medium, high flex on 3 LEDs (green, yellow, red), but only when a button is pressed. When the button is not pressed. all LEDs will be OFF. It is connected to Wi-Fi using a Photon 2 microcontroller.

Created: November 7th, 2024

0

Intention

I wanted to explore having 2 inputs to 1 output in my Internet of Things (IoT) device design. I decided to use a flex sensor because of the interesting applications it could have in structural monitoring or testing. A button was used as my other input because it is common to have a switch or a button to turn devices on or off. I used 3 different color light emitting diodes (LEDs)  to show low, medium, and high flex.

0

Process

I started with wiring and testing each component. To test them to make sure my wiring was working I would make a simple code to turn them ON if it was an output and to display the value if it was an input. Once I had all my component working, I first made it so that my flex sensor would always map to the different LEDs and display without taking the button push into account. Once that worked, I added functionality around that to only display to the LEDs when the button was pushed. I had an issue where the range of inputs I got from my flex sensor was smaller than expected so I had to adjust my value map. 

0

Product

I used a Photon 2 microcontroller for this project. The Photon 2 allowed me to publish output values from my flex sensor to the Particle Console that was connected to the microcontroller over Wi-Fi.  I used conditional statements to control which LED turned on which you can see in my code. I have also included a video of me testing it out where I start with the button pressed and you can see the flex output change, then I release the button, and the LEDs turn off until I press the button again.

0
// Include Particle Device OS APIs
#include "Particle.h"

// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

//PINS
int flexPin = A0;
int redLED = D2;
int yellowLED = D3;
int greenLED = D4;
int buttonPin = D5;

//Va;irables

int flexRead = 0;
int ledOn = 0;
//bool buttonState = FALSE;

// setup() runs once, when the device is first turned on
void setup() {
    
    pinMode( buttonPin , INPUT_PULLUP);
  
    pinMode(redLED, OUTPUT);
    pinMode(yellowLED, OUTPUT);
    pinMode(greenLED, OUTPUT);
  
    Particle.variable("flex", flexRead);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  flexRead = analogRead(flexPin);
  ledOn = map(flexRead, 0, 10, 1, 3);
 /*
  if(flexRead < 2) ledOn = 1;
  else if (flexRead < 3) ledOn = 2;
  else ledOn = 3;
  */
  Particle.publish("flex", String(flexRead));
  int buttonState = digitalRead( buttonPin );
    if( buttonState == LOW )
    {
      if (ledOn == 3){
          digitalWrite(redLED, HIGH);
          digitalWrite(yellowLED, LOW);
          digitalWrite(greenLED, LOW);
      }//red ON
      else if (ledOn == 2){
          digitalWrite(redLED, LOW);
          digitalWrite(yellowLED, HIGH);
          digitalWrite(greenLED, LOW);
      }//yellow ON
      else if (ledOn == 1){
          digitalWrite(redLED, LOW);
          digitalWrite(yellowLED, LOW);
          digitalWrite(greenLED, HIGH);
      }//green ON
    }else{
        digitalWrite(redLED, LOW);
        digitalWrite(yellowLED, LOW);
        digitalWrite(greenLED, LOW); 
        
    }
  
    delay(100);
}
Click to Expand
0
0

Reflection

I learned about live publishing values to the Particle Console in a way that does not require you to manually keep refreshing a variable. I also learned about using a flex sensor. In the future I would like to try and figure out how to get a larger value range from my flex sensor.

x
Share this Project


Skills
Tools
About

This is a flex sensor that can display low, medium, high flex on 3 LEDs (green, yellow, red), but only when a button is pressed. When the button is not pressed. all LEDs will be OFF. It is connected to Wi-Fi using a Photon 2 microcontroller.