skillDev2_sensorInput

Made by Ziru Wei

control a led with a sensor and a switch

Created: November 3rd, 2024

0

Product/Intention

This is a gadget used to check if movements are standard during the stretching phase of a workout. A flexure sensor is attached to the joints, and if it detects any bending, it indicates that the movement is loose, and an LED will alert the user to this issue.

0

Round 1: 

In this round, I designed the gradual change in the brightness of the LED to reflect the flexure detected by the sensor.

0
// test1: gradual change in brightness

int sensorPin = A0;
int sensorReading = 0;
int ledPin = D1;
int ledBrightness = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  Particle.variable("Flexure", &sensorReading, INT);
  Particle.variable("Brightness", &ledBrightness, INT);
}


void loop()
{
  sensorReading = analogRead(sensorPin);
  ledBrightness = map(sensorReading, 3350, 4095, 255, 0);
  analogWrite(ledPin, ledBrightness);
  delay(100);
}
Click to Expand
0

I try to map the value in different domains, but unfortunately, none of them appears good. I don't think the display of the brightness could provide a clear and intuitive clue about the flexure. Anyway here is the different trials I made in this process:

0

`ledBrightness = map(sensorReading, 3350, 4095, 0, 225);`

0

`ledBrightness = map(sensorReading, 3350, 4000, 0, 225);`

0

`ledBrightness = map(sensorReading, 3650, 4095, 0, 225);`

0

`ledBrightness = map(sensorReading, 3450, 4000, 0, 225);`

0

Round 2:

In this round, I utilized a simple on and off indication to determine if bending exceeds the threshold value. I believe this approach is an improvement over the previous version because users might be significantly distracted from their workout tasks if they have to pay extra attention to the LED brightness.

0
int sensorPin = A0;
int sensorReading = 0;
int ledPin = D1;
int ledBrightness = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  Particle.variable("light", &sensorReading, INT);
  Particle.variable("brightness", &ledBrightness, INT);
}

void loop() {
  sensorReading = analogRead(sensorPin);
  Serial.println(sensorReading); 
  if (sensorReading < 3700) {
    ledBrightness = 255;
  } else {
    ledBrightness = 0;
  }
  
  analogWrite(ledPin, ledBrightness);
  delay(100);
}
Click to Expand
0

Round 3:

In this round, I added a new mechanism where the system monitors the duration of a bending action at a joint. If the bending at the joint persists for more than 1 second, this is recognized as an indication of improper posture or movement during stretching exercises. To alert the user immediately about this issue, the system activates an LED to shine brightly for 1 second. 

0
int sensorPin = A0;
int sensorReading = 0;
int ledPin = D1;
int ledBrightness = 0;

unsigned long bendStartTime = 0;
bool isBending = false;
bool ledOn = false;
unsigned long ledOnTime = 0;

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

void loop() {
  sensorReading = analogRead(sensorPin);

  if (sensorReading < 3800) {
    if (!isBending) {
      isBending = true;
      bendStartTime = millis();
    }
    if (millis() - bendStartTime > 1000 && !ledOn) {
      ledBrightness = 255;  // Turn the LED on full brightness
      analogWrite(ledPin, ledBrightness);
      ledOn = true;
      ledOnTime = millis();
    }
  } else {
    isBending = false;
    bendStartTime = 0;
  }

  if (ledOn && (millis() - ledOnTime > 1000)) {
    ledBrightness = 0;  // Turn the LED off
    analogWrite(ledPin, ledBrightness);
    ledOn = false;
  }

  delay(100);  // Small delay to stabilize readings
}
Click to Expand
0

Round 4:

Finally add a switch.

0
int sensorPin = A0;
int sensorReading = 0;
int ledPin = D1;
int ledBrightness = 0;
int switchPin = D5;

unsigned long bendStartTime = 0;
bool isBending = false;
bool ledOn = false;
unsigned long ledOnTime = 0;

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

void loop() {
  int buttonState = digitalRead(switchPin);

  if (buttonState == LOW) {  // If the switch is on (LOW when using INPUT_PULLUP and switch is pressed)
    sensorReading = analogRead(sensorPin);

    if (sensorReading < 3800) {
      if (!isBending) {
        isBending = true;
        bendStartTime = millis();
      }
      if (millis() - bendStartTime > 1000 && !ledOn) {
        ledBrightness = 255;  // Turn the LED on full brightness
        analogWrite(ledPin, ledBrightness);
        ledOn = true;
        ledOnTime = millis();
      }
    } else {
      isBending = false;
      bendStartTime = 0;
    }

    if (ledOn && (millis() - ledOnTime > 1000)) {
      ledBrightness = 0;  // Turn the LED off
      analogWrite(ledPin, ledBrightness);
      ledOn = false;
    }
  } else {
    // Turn the LED off if the switch is off
    if (ledOn) {
      ledBrightness = 0; 
      analogWrite(ledPin, ledBrightness);
      ledOn = false;
    }
    isBending = false;  // Reset bending state
  }

  delay(100);  // Small delay to stabilize readings
}
Click to Expand
0
x