safwan092

FastLED_3xLevel_Water_Level_Simple_Final_Code

Jan 7th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. // Define the number of LEDs and the pin connected to the LED strip
  4. #define NUM_LEDS 3
  5. #define LED_PIN 4
  6.  
  7. // Define sensor pins
  8. #define SENSOR_PIN_LOW A1
  9. #define SENSOR_PIN_MEDIUM A2
  10. #define SENSOR_PIN_HIGH A3
  11.  
  12. // Create an array to store the LED objects
  13. CRGB leds[NUM_LEDS];
  14.  
  15. void setup() {
  16. Serial.begin(9600);
  17. // Initialize the FastLED library
  18. FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  19.  
  20. // Set the sensor pins as input
  21. pinMode(SENSOR_PIN_LOW, INPUT);
  22. pinMode(SENSOR_PIN_MEDIUM, INPUT);
  23. pinMode(SENSOR_PIN_HIGH, INPUT);
  24.  
  25. // Turn off all LEDs initially
  26. for (int i = 0; i < NUM_LEDS; i++) {
  27. leds[i] = CRGB::Black;
  28. }
  29. FastLED.show();
  30. }
  31.  
  32. void loop() {
  33. // Read the water level sensor inputs
  34. bool isLow = digitalRead(SENSOR_PIN_LOW);
  35. bool isMedium = digitalRead(SENSOR_PIN_MEDIUM);
  36. bool isHigh = digitalRead(SENSOR_PIN_HIGH);
  37.  
  38. // Determine the number of LEDs to light up
  39. int numLedsToLight = 0;
  40.  
  41.  
  42. if (isHigh) {
  43. Serial.println("HIGH");
  44. numLedsToLight = 3; // High level - Turn on all 3 LEDs
  45. } else if (isMedium) {
  46. Serial.println("MED");
  47. numLedsToLight = 2; // Medium level - Turn on 2 LEDs
  48. } else if (isLow) {
  49. Serial.println("LOW");
  50. numLedsToLight = 1; // Low level - Turn on 1 LED
  51. }
  52.  
  53. // Update the LEDs based on the water level
  54. for (int i = 0; i < NUM_LEDS; i++) {
  55. if (i < numLedsToLight) {
  56. leds[i] = CRGB::Blue; // Use Blue color for all levels
  57. } else {
  58. leds[i] = CRGB::Black; // Turn off unused LEDs
  59. }
  60. }
  61.  
  62. // Update the LED strip
  63. FastLED.show();
  64.  
  65. // Small delay for stability
  66. delay(100);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment