Inputs and Sensors

Made by Shih-Hsueh Wang · UNLISTED (SHOWN IN POOLS)

Created: November 9th, 2022

0

Outcome

    • Dev 2 photoresistor ex 1: LED gradually turns bright when is under a shadow. Video
    • Dev 2 photoresistor ex 2: LED immediately turns on when is under a shadow. Video
    • Two-in-one-out device: With a photocell sensor and a button, when there is a bright environment, the button won't activate the LED; when there is a dark environment, pushing the button turns on the LED. Video 1. While the LED is on and shifting the environment from dark to bright, the LED will turn off. Then shifting from bright to dark, the LED will turn on again. Video 2

0

Process

 I wanted to make a lighting device that is meant to save energy. In that regard, the LED can only be activated when the environment is below certain brightness. To achieve that, I used a photocell sensor to detect the brightness and a button to turn the LED on and off. When the room dims, we can manually push the button to turn on the LED. If we activate the button beforehand, when the environment dims, the LED will automatically turn on. Conversely, while the button is activated, when the environment is getting bright, the LED will automatically turn off.    

0
int ledPin = D3;
int phoPin = A5;
int buttonRead = D2;

int phoReading = 0;
int lastButtonState = LOW;

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(buttonRead, INPUT_PULLUP);
    //analog pin always defaults as input
    
    //A cloud variable called photoCellReading and says it is an integer (or number)
    Particle.variable("Photocell", &phoReading, INT);
}

void loop() {
    
    int buttonState = digitalRead(buttonRead);
    int lastButtonState = LOW;
    phoReading = analogRead(phoPin);
    
    // if (buttonState != lastButtonState){
        
        
        if ((buttonState == LOW) && (phoReading < 1600)){
        
            digitalWrite(ledPin, HIGH);

        }else{
        
            digitalWrite(ledPin, LOW);

        }
    
        if (phoReading >= 1600){
            digitalWrite(ledPin, LOW);
        }
        
    // }
    
    // lastButtonState == buttonState;
    
    
    delay(100);
}
Click to Expand
0

Reflection

My initial intention is to push the button and the LED will keep being bright. So I added another integer to indicate the previous state of the button. I thought when the buttonState is LOW (button pushed), the lastButtonState equates to LOW, which failed to meet the first if statement so it won't change the LED state anymore until I pushed the button again. Yet, it turned out the code was not working. I still need to figure out the problem.  

x
Share this Project

This project is only listed in this pool. Be considerate and think twice before sharing.


Courses

About

~