Working with neopixels and buttons HELP!

Basically I'm looking for help with my code, im pretty new to coding and I am trying to create a concept for a colour picking game that 4 people go through a colour wheel and each select a colour then a button is pressed and all the colours merge to create a colour display.

I have each colour selector working but when i put in the button element nothing happens and i cant figure out why. Any help would be great

Wont let me upload the file so this is the best i can do

int val = 0;
int counter = 0;
int val1 =0;
int val2 =0;
int val3 =0;

uint32_t colours[] = { 6553600, 25600, 100, 1234567, 16776705, 16753500, 16724685 }; // colours in order red, green, blue, light blue, yellow, white, pink
#include <Adafruit_NeoPixel.h>
#define PIN 6 // On Trinket or Gemma, suggest changing this to 1
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {

Serial.begin(9600);
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)

}

void loop() {

val = analogRead(A0);
val = map(val, 0, 1023, 0, 7);

val1 = analogRead(A1);
val1 = map(val1, 0, 1023, 0, 7);

val2 = analogRead(A2);
val2 = map(val2, 0, 1023, 0, 7);

val3 = analogRead(A3);
val3 = map(val3, 0, 1023, 0, 7);

if (digitalRead(3 == HIGH)) {
delay(200);
//counter++;

pixels.setPixelColor(4, colours[val]);
pixels.setPixelColor(8, colours[val]);
pixels.setPixelColor(12, colours[val]);

pixels.setPixelColor(5, colours[val1]);

pixels.setPixelColor(9, colours[val1]);
pixels.setPixelColor(13, colours[val1]);

pixels.setPixelColor(6, colours[val2]);

pixels.setPixelColor(10, colours[val2]);
pixels.setPixelColor(14, colours[val2]);

pixels.setPixelColor(7, colours[val3]);

pixels.setPixelColor(11, colours[val3]);
pixels.setPixelColor(15, colours[val3]);

Serial.println(val);
//delay(100);

pixels.clear();

pixels.setPixelColor(0, colours[val]); //colour selection neopixels
pixels.setPixelColor(1, colours[val1]);
pixels.setPixelColor(2, colours[val2]);
pixels.setPixelColor(3, colours[val3]);

pixels.show(); // Send the updated pixel colors to the hardware.
delay(100);
}

AKA

val = analogRead (A0) / 128;
2 Likes

Check those parentheses!

2 Likes

Try this:

#define NUMPIXELS 16
#define NUM_PLAYERS 4
#define NUM_COLORS 7

#include <Adafruit_NeoPixel.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip

Bounce2::Button button = Bounce2::Button();

const byte ledPin = 6;
const byte buttonPin = 3;
const int potPin[NUM_PLAYERS] = {A0, A1, A2, A3};

int colorSelected[NUM_PLAYERS] = {0};

Adafruit_NeoPixel pixels(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);

uint32_t red = pixels.Color(255, 0, 0);
uint32_t green = pixels.Color(0, 255, 0);
uint32_t blue = pixels.Color(0, 0, 255);
uint32_t yellow = pixels.Color(255, 255, 0);
uint32_t white = pixels.Color(255, 255, 255);
uint32_t pink = pixels.Color(255, 0, 100);
uint32_t cyan = pixels.Color(0, 255, 255);

uint32_t colors[NUM_COLORS] = {red, green, blue, yellow, white, pink, cyan};

void setup() {

  Serial.begin(9600);

  button.attach(buttonPin, INPUT_PULLUP);
  button.interval(5);
  button.setPressedState(LOW);

  for (byte i = 0; i < NUM_PLAYERS; i++)
  {
    pinMode(potPin[i], INPUT);
  }

  pixels.begin();
  pixels.setBrightness(255);
  pixels.show();
}

void loop() {
  for (byte i = 0; i < NUM_PLAYERS; i++)
  {
    colorSelected[i] = map(analogRead(potPin[i]), 0, 1023, 0, NUM_COLORS);
  }

  button.update();

  if (button.pressed()) {
    Serial.println("Applying colors!");

    int offset = 0;

    for (byte j = 0; j < NUM_PLAYERS - 1; j++)
    {
      for (byte i = 0; i < NUM_PLAYERS; i++)
      {
        pixels.setPixelColor(i + offset + 4, colors[colorSelected[i]]);
        Serial.print("Pixel ");
        Serial.print(i + offset + 4);
        Serial.print(" = ");
        Serial.println(colors[colorSelected[i]]);
      }
      offset += NUM_PLAYERS;
      Serial.println();
    }
    pixels.show();
    delay(2000);
    pixels.clear();

    for (byte i = 0; i < NUM_PLAYERS; i++)
    {
      pixels.setPixelColor(i, colors[colorSelected[i]]);
      Serial.print("Pixel ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(colors[colorSelected[i]]);
    }
    pixels.show();
    delay(2000);
    pixels.clear();
    pixels.show();
  }
}

Edit: Fixed!

this is almost what i was looking for, i should have been more clearer but i need the first 4 pixels to constantly be on so users can see what colour they are selecting and for the rest to come on after the button is pressed and reset when it is pressed a second time. this is very helpful with how i could work that into the original design thank you :smile:

It's almost what do you need but I don't have time to tune the code now.

#define NUMPIXELS 16
#define NUM_PLAYERS 4
#define NUM_COLORS 7

#include <Adafruit_NeoPixel.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip

Bounce2::Button button = Bounce2::Button();

const byte ledPin = 6;
const byte buttonPin = 3;
const int potPin[NUM_PLAYERS] = {A0, A1, A2, A3};

int colorSelected[NUM_PLAYERS] = {0};

Adafruit_NeoPixel pixels(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);

uint32_t red = pixels.Color(255, 0, 0);
uint32_t green = pixels.Color(0, 255, 0);
uint32_t blue = pixels.Color(0, 0, 255);
uint32_t yellow = pixels.Color(255, 255, 0);
uint32_t white = pixels.Color(255, 255, 255);
uint32_t pink = pixels.Color(255, 0, 100);
uint32_t cyan = pixels.Color(0, 255, 255);

uint32_t colors[NUM_COLORS] = {red, green, blue, yellow, white, pink, cyan};

int potValue[NUM_PLAYERS] = {0};
int previousPotValue[NUM_PLAYERS] = {0};
bool firstTime = true;

void setup() {

  Serial.begin(9600);

  button.attach(buttonPin, INPUT_PULLUP);
  button.interval(5);
  button.setPressedState(LOW);

  for (byte i = 0; i < NUM_PLAYERS; i++)
  {
    pinMode(potPin[i], INPUT);
  }

  pixels.begin();
  pixels.setBrightness(255);
  pixels.show();
}

void loop() {
  bool changed = false;

  for (byte i = 0; i < NUM_PLAYERS; i++)
  {
    potValue[i] = analogRead(potPin[i]);

    if (potValue[i] != previousPotValue[i])
    {
      previousPotValue[i] = potValue[i];
      changed = true;
    }

    colorSelected[i] = map(potValue[i], 0, 1023, 0, NUM_COLORS);
  }

  if (changed == true)
  {
    Serial.println("New colors:");
    for (byte i = 0; i < NUM_PLAYERS; i++)
    {
      pixels.setPixelColor(i, colors[colorSelected[i]]);

      Serial.print("Pixel ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(colors[colorSelected[i]]);
    }
     Serial.println("");
    pixels.show();
    changed = false;
  }

  button.update();

  if (button.pressed()) {

    if (firstTime == true)
    {
      firstTime = false;

      Serial.println("Applying colors!");
      
      pixels.clear();
      pixels.show();

      int offset = 0;

      for (byte j = 0; j < NUM_PLAYERS - 1; j++)
      {
        for (byte i = 0; i < NUM_PLAYERS; i++)
        {
          pixels.setPixelColor(i + offset + 4, colors[colorSelected[i]]);
          Serial.print("Pixel ");
          Serial.print(i + offset + 4);
          Serial.print(" = ");
          Serial.println(colors[colorSelected[i]]);
        }
        offset += NUM_PLAYERS;
        Serial.println();
      }
      pixels.show();
    }
    else
    {
      firstTime = true;
      pixels.clear();
      pixels.show();
    }
  }
}
1 Like

thank you this is great!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.