skillDev2_sensorInput
Made by Ziru Wei
Made by Ziru Wei
control a led with a sensor and a switch
Created: November 3rd, 2024
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.
// 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
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.
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
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.
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
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
control a led with a sensor and a switch