Skills Dev II: Working with Inputs and Sensors

Made by Yanling Zhang

practice input and sensor

Created: November 3rd, 2021

0

Exercise1-1:

Using button&potentiometer to control led

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).

0
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
0
iot_SkillDev2_1
zhang yanling - https://youtu.be/RzEmCOz9ACI
0

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.

0
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
0
iot_SkillDev2_2.1
zhang yanling - https://youtu.be/NCLe_wTGGKg
0

Exercise 2-2

Instead of fading your light, why not turn it on or off when the light reaches certain levels.

For example, if the light levels are below 600 turn the light on (and bright), otherwise turn it off.

Modify the program to work in this way.

0
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
0
iot_SkillDev2_2.2
zhang yanling - https://youtu.be/qOPb7wZEklc
0

Practice Exercise: Combining Sensors and Inputs

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).

  • Identify an interesting sensor that you'd like to explore.
  • Develop a working circuit with that component and write code to read and make use of information from that sensor (e.g. share the value online, translate it into an output).
  • Add a second component into the mix. This can either be an input (potentiometer, button, switch) or a sensor. Stream readings from both to the Particle Cloud at the same time.
  • Use conditional logic (if statements) to interpret inputs/sensor readings and trigger actions e.g. if the temperature is less than 50 degrees and the button is pushed...
  • Document your work on the Gallery and submit a link below


0
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
0
iot_SkillDev2_3
zhang yanling - https://youtu.be/R9JZhICuXlU
0

Reflection

I learned how to use the bend sensor!

x