Skills Dev II: Working with Inputs and Sensors
Made by youngjol
Made by youngjol
Created: November 18th, 2021
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).
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
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
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
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.