NUMPixel LEDs using 2D array and Potentiometer

 avatar
subratasarkar20
c_cpp
4 months ago
945 B
5
Indexable
#include <stdio.h>
#include <Adafruit_NeoPixel.h>
#define PIN 13
#define NUMPIXELS 8
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

const int potPin = A0;
const int maxVoltage = 5;
const int totalStep = NUMPIXELS;
const float stepVoltage = (float)maxVoltage / totalStep;

int a[NUMPIXELS][3] = {
  { 35, 0, 0 },
  { 30, 5, 0 },
  { 25, 10, 0 },
  { 20, 15, 0 },
  { 15, 20, 0 },
  { 10, 25, 0 },
  { 5, 30, 0 },
  { 0, 35, 0 },
};

void setup() {
  pixels.begin();
}

void loop() {
  int potValue = analogRead(potPin);
  float voltage = (potValue / 1023.0) * maxVoltage;
  float ledOn = (float)voltage / stepVoltage;

  for (int i = 0; i < totalStep; i++) {
    if (i < ledOn) {
      if (i == 0) {
        pixels.setPixelColor(i, pixels.Color(a[i][0], a[i][1], a[i][2]));
      } else {
        pixels.setPixelColor(i, pixels.Color(0, 0, 0));
      }
      pixels.show();
    }
  }
}
Leave a Comment