// Practice Exercise
// Creating a circuit that warns when pressure is applied. When the button is pressed, the led turns on, and when pressure is applied above 600, the led begins to blink.
int photoCellPin = A0; // Pin for the photo cell
int buttonPin = D4; // Pin for the pushbutton
int ledPin = D1; // Pin for the LED
// Variable to hold the light reading
int photoCellReading = 0;
// Variable to track the LED state
bool ledOn = false;
// Variable to track the button press state
bool buttonPressed = false;
void setup()
{
// Set up pins for input/output
pinMode(photoCellPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(ledPin, OUTPUT);
// Create a cloud variable of type integer called 'light' mapped to photoCellReading
Particle.variable("light", &photoCellReading, INT);
}
void loop()
{
// Read the photo cell value (0-4095)
photoCellReading = analogRead(photoCellPin);
// Check the pushbutton state
if (digitalRead(buttonPin) == LOW && !buttonPressed) { // Button pressed
ledOn = !ledOn; // Toggle the LED state
buttonPressed = true; // Prevent multiple toggles
}
else if (digitalRead(buttonPin) == HIGH) { // Button released
buttonPressed = false; // Reset button press tracking
}
// Control the LED
if (ledOn) {
if (photoCellReading > 600) {
// Blink the LED
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
else {
// Turn the LED on steadily
digitalWrite(ledPin, HIGH);
}
}
else {
// Turn the LED off
digitalWrite(ledPin, LOW);
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .