Skills Dev II: Working with Inputs and Sensors
Made by Yanling Zhang
Made by Yanling Zhang
practice input and sensor
Created: November 3rd, 2021
Combine a switch (or button) and potentiometer. The switch should turn on and off the light while the potentiometer will fade up and down the light (but only when its on).
int potPin = A5;
int potReading = 0;
int ledPin = D2;
int ledBrightness = 0;
int buttonPin = D5;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode( buttonPin , INPUT_PULLUP);
Spark.variable("pot", potReading );
}
void loop() {
int buttonState = digitalRead( buttonPin );
if( buttonState == LOW )
{
// digitalWrite( ledPin, HIGH);
potReading = analogRead(potPin);
ledBrightness = map(potReading, 0, 4095, 0, 255);
analogWrite(ledPin, ledBrightness);
delay(100);
}else{
digitalWrite( ledPin, LOW);
}
}
Click to Expand
Exercise 2-1
Currently, the program shows how much light is available. When there is lots of light the LED is bright. When there is little light it’s dim. Modify the program so that the LED shows the opposite. When there is little light, it will be bright and when there’s lots of light, it will be off.
int photoCellPin = A0;
int photoCellReading = 0;
int ledPin = D2;
int ledBrightness = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
Particle.variable("light", &photoCellReading, INT);
}
void loop()
{
photoCellReading = analogRead(photoCellPin);
ledBrightness = map(photoCellReading, 0, 4095, 255, 0);
analogWrite(ledPin, ledBrightness);
delay(100);
}
Click to Expand
int photoCellPin = A0;
// Create a variable to hold the light reading
int photoCellReading = 0;
// Define a pin we'll place an LED on
int ledPin = D2;
// Create a variable to store the LED brightness.
int ledBrightness = 0;
void setup()
{
// Set up the LED for output
pinMode(ledPin, OUTPUT);
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("light", &photoCellReading, INT);
}
void loop()
{
// Use analogRead to read the photo cell reading
// This gives us a value from 0 to 4095
photoCellReading = analogRead(photoCellPin);
// Map this value into the PWM range (0-255)
// and store as the led brightness
if (photoCellReading < 600){
ledBrightness = map(photoCellReading, 0, 4095, 255, 0);
analogWrite(ledPin, ledBrightness);
}
else digitalWrite( ledPin, LOW);
delay(100);
}
Click to Expand
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 BendPin = A3;
int BendReading = 0;
int ledPin = D2;
int ledBrightness = 0;
void setup() {
pinMode(ledPin , OUTPUT);
Particle.variable("resistance", &BendReading, INT);
Particle.variable("brightness", &ledBrightness, INT);
}
void loop() {
BendReading = analogRead(BendPin);
ledBrightness = map(BendReading, 500, 1500, 0, 255);
analogWrite(ledPin, ledBrightness);
//delay(500);
}
Click to Expand