2D array using pointers
subratasarkar20
c_cpp
10 days ago
919 B
6
Indexable
Never
#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) { 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