Untitled
unknown
c_cpp
16 days ago
2.8 kB
2
Indexable
Never
#include <Adafruit_NeoPixel.h> #define PIN 13 #define NUMPIXELS 8 #define BUTTON_PIN 12 Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); const int sensorPin = A0; const float gain = 0.548; const float minInputVoltage = 6; const float maxInputVoltage = 8.4; const float stepVoltage = (float)(maxInputVoltage - minInputVoltage) / NUMPIXELS; float previousButtonState; unsigned long previousMillis = 0; unsigned long debounceInterval = 30; unsigned long ledOnMillis = 0; const unsigned long ledDuration = 3000; bool ledsOn = false; bool buttonPressed = false; void setup() { pixels.begin(); pinMode(BUTTON_PIN, INPUT_PULLUP); previousButtonState = digitalRead(BUTTON_PIN); } void loop() { unsigned long currentMillis = millis(); //debouncing if (currentMillis - previousMillis >= debounceInterval) { float currentButtonState = digitalRead(BUTTON_PIN); if (currentButtonState != previousButtonState) { previousMillis = currentMillis; previousButtonState = currentButtonState; if (currentButtonState == LOW) { ledsOn = true; buttonPressed = true; ledOnMillis = currentMillis; } } } if (ledsOn) { if (currentMillis - ledOnMillis <= ledDuration) { float sensorValue = analogRead(sensorPin); float outputVoltage = (5 / 1023.0) * sensorValue; float inputVoltage = (float)outputVoltage / gain; int ledOn = 0; if (inputVoltage >= minInputVoltage) { ledOn = (float)((inputVoltage - minInputVoltage) / stepVoltage); if (ledOn > NUMPIXELS) { ledOn = NUMPIXELS; } } for (int i = 0; i < NUMPIXELS; i++) { if (i < ledOn) { if (i == 0) { pixels.setPixelColor(i, pixels.Color(35, 0, 0)); } else if (i == 1) { pixels.setPixelColor(i, pixels.Color(30, 5, 0)); } else if (i == 2) { pixels.setPixelColor(i, pixels.Color(25, 10, 0)); } else if (i == 3) { pixels.setPixelColor(i, pixels.Color(20, 15, 0)); } else if (i == 4) { pixels.setPixelColor(i, pixels.Color(15, 20, 0)); } else if (i == 5) { pixels.setPixelColor(i, pixels.Color(10, 25, 0)); } else if (i == 6) { pixels.setPixelColor(i, pixels.Color(5, 30, 0)); } else if (i == 7) { pixels.setPixelColor(i, pixels.Color(0, 35, 0)); } } else { pixels.setPixelColor(i, pixels.Color(0, 0, 0)); } } pixels.show(); } else { ledsOn = false; buttonPressed = false; for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(0, 0, 0)); } pixels.show(); } } }
Leave a Comment