Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- // Define the number of LEDs and the pin connected to the LED strip
- #define NUM_LEDS 3
- #define LED_PIN 4
- // Define sensor pins
- #define SENSOR_PIN_LOW A1
- #define SENSOR_PIN_MEDIUM A2
- #define SENSOR_PIN_HIGH A3
- // Create an array to store the LED objects
- CRGB leds[NUM_LEDS];
- void setup() {
- Serial.begin(9600);
- // Initialize the FastLED library
- FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
- // Set the sensor pins as input
- pinMode(SENSOR_PIN_LOW, INPUT);
- pinMode(SENSOR_PIN_MEDIUM, INPUT);
- pinMode(SENSOR_PIN_HIGH, INPUT);
- // Turn off all LEDs initially
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB::Black;
- }
- FastLED.show();
- }
- void loop() {
- // Read the water level sensor inputs
- bool isLow = digitalRead(SENSOR_PIN_LOW);
- bool isMedium = digitalRead(SENSOR_PIN_MEDIUM);
- bool isHigh = digitalRead(SENSOR_PIN_HIGH);
- // Determine the number of LEDs to light up
- int numLedsToLight = 0;
- if (isHigh) {
- Serial.println("HIGH");
- numLedsToLight = 3; // High level - Turn on all 3 LEDs
- } else if (isMedium) {
- Serial.println("MED");
- numLedsToLight = 2; // Medium level - Turn on 2 LEDs
- } else if (isLow) {
- Serial.println("LOW");
- numLedsToLight = 1; // Low level - Turn on 1 LED
- }
- // Update the LEDs based on the water level
- for (int i = 0; i < NUM_LEDS; i++) {
- if (i < numLedsToLight) {
- leds[i] = CRGB::Blue; // Use Blue color for all levels
- } else {
- leds[i] = CRGB::Black; // Turn off unused LEDs
- }
- }
- // Update the LED strip
- FastLED.show();
- // Small delay for stability
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment