Skills Dev II: Working with Inputs and Sensors

Made by youngjol

Created: November 18th, 2021

0

Assignment

    Prepare a simple 2-in (sensors or inputs) 1-out (LED) device (i.e. your project will use three components (resistors and wire don't count!) and at least one must be a sensor). Present a really simple use case for a connected sensor device (perhaps solve a simple problem in your home).

0

First Program: force-controlled LED 

- This program reads the amount of force applied to a force sensitive resistor and displays an intensity of light proportional to the force. 

0
int forcePin = A0;
int forceReading = 0;
int ledPin = D2;
int ledBrightness = 0;


void setup() {
    pinMode(ledPin, OUTPUT);
    Particle.variable("Force Reading", forceReading);
}

void loop() {
    forceReading = analogRead(forcePin);
    ledBrightness = map(forceReading,0,4095,0,255);
    analogWrite(ledPin, ledBrightness);
    delay(100);
}
Click to Expand
0
dev2 ex1
Youngjoo Lee - https://youtu.be/SXPOvxywsUc
0

Second Program: force-controlled LED with on/off switch

- This program adds on to the first program. It has the same functionalities, with the added feature of an on and off switch. If the switch is turned off, then the light will not respond to any input to the force sensitive resistor. 

0
int forcePin = A0;
int forceReading = 0;
int ledPin = D2;
int ledBrightness = 0;
int switchPin = D4;


void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(switchPin, INPUT_PULLUP);
    Particle.variable("Force Reading", forceReading);
}

void loop() {
    
    int switchState = digitalRead(switchPin);
    
    if (switchState == LOW) {
        digitalWrite(ledPin, LOW);
    }else{
        forceReading = analogRead(forcePin);
        ledBrightness = map(forceReading,0,4095,0,255);
        analogWrite(ledPin, ledBrightness);
    }
    delay(100);
}
Click to Expand
0
dev2 ex2
Youngjoo Lee - https://youtu.be/NaMLj0H2ofI
0

Third Program: force-controlled rainbow lights

- Based on the magnitude of force applied to the force sensitive resistor, different colored LED lights are turned on. If enough pressure is applied, then all of the lights turn off.


0
int forcePin = A0;
int forceReading = 0;

int greenPin = D3;
int yellowPin = D4;
int orangePin = D5;
int redPin = D6;

int greenThresh = 100;
int yellowThresh = 800;
int orangeThresh = 1600;
int redThresh = 2400;
int offThresh = 3200;


void setup() {
    pinMode(greenPin, OUTPUT);
    pinMode(yellowPin, OUTPUT);
    pinMode(orangePin, OUTPUT);
    pinMode(redPin, OUTPUT);
    
    Particle.variable("Force Reading", forceReading);
}

void loop() {
    
    forceReading = analogRead(forcePin);
    
    if (forceReading >= greenThresh and forceReading < yellowThresh) {
        digitalWrite(greenPin, HIGH);
    }
    if (forceReading >= yellowThresh and forceReading < orangeThresh) {
        digitalWrite(yellowPin, HIGH);
    }
    if (forceReading >= orangeThresh and forceReading < redThresh) {
        digitalWrite(orangePin, HIGH);
    }
    if (forceReading >= redThresh and forceReading < offThresh) {
        digitalWrite(redPin, HIGH);
    }
    if (forceReading >= offThresh) {
        digitalWrite(greenPin, LOW);
        digitalWrite(yellowPin, LOW);
        digitalWrite(orangePin, LOW);
        digitalWrite(redPin, LOW);
    }
    delay(250);
}
Click to Expand
0
dev2 ex3
Youngjoo Lee - https://youtu.be/iJ_sjlL_Jto
0

Process

    I began by setting up the breadboard with the circuit we learned about for resistive sensors. I used a force sensitive resistor instead of a photo-resistor. After I checked that this worked, I added a switch to the the circuit: connected one end to GND and the middle to a digital pin (D4).

    I wanted to create a system where the LED brightness could be controlled by how much force I was applying when pressing on the force sensitive resistor. In real life scenarios, we would want a way to turn ‘off’ functionalities, so I added the switch. When it is turned off, accidental (or intentional) force applied to the sensor will not affect the LED.

    This could be used next to one’s bed so that the user can easily control the brightness of the room’s lighting without having to walk to the wall switch.

    Lastly, I wanted to make an aesthetic light display, so I played around with a couple LED’s and used a similar approach as before. I tested the possible force sensitive resistor readings to determine suitable thresholds for triggering the different lights to turn on. 

x