Due as light controller causes LEDs to flicker/wrong color

So I am coding a light strip to create a LED controller and I am fleshing out the UI controls before creating the pattern. My code is just a simple button to change the color of the WS2813 strip.

The code works for what it is intended, though the LED I have on to test will flicker and sometimes even be the wrong color. Other simpler codes work fine so I cannot find the issue. My button pin (8) is connected to ground, and ground has a 1Kohm resistor on it. 5V from the board goes to the other side of the button. The board is connected to the ground on my benchtop PSU. The LED strip is a 5V strip powered by aforementioned PSU. Signal to the strip is on pin 2 and I have tried putting 0, 0.25K, 1K, 2K, 3K ohm resistors on the signal wire (I just kept adding them hoping it would help, I lost hope after 1K).

The Arduino Due also hums a very high pitched tone once the flickering occurs. The board is powered using the programming USB. I have ran this code on an Uno (before I bricked it, oops) and I did not have this issue.

Any ideas, code below,

#include <FastLED.h>

#define NUM_LEDS 1
#define DATA_PIN 2
#define DEBOUNCE 5
#define BUTTONS 2
#define COLOR_BUTTON 8
#define AUX_BUTTON 8

#define LOOP_SPEED 50
int timer = 0;

CRGB leds[NUM_LEDS];

int color = 0;
unsigned long loop_timer = 0;
int counter = 0;

void setup() {
 Serial.begin(9600);
 Serial.println("setup");

pinMode(2, OUTPUT);
pinMode(8, INPUT);

     
FastLED.addLeds<WS2813, DATA_PIN, GRB>(leds, NUM_LEDS);
  
  
int pattern = 0;


}

bool button_pressed(unsigned int num)
{
  static bool pressed[BUTTONS] = {false};
  static unsigned long last_pressed[BUTTONS] = {0};
  static const int port[] = {COLOR_BUTTON,AUX_BUTTON};
  bool retval = false;
  
  if (num<BUTTONS)
  {
    if (!pressed[num])
    {
      if (digitalRead(port[num]) == HIGH)
      {
        pressed[num] = true;
        last_pressed[num] = millis();
      }
    }
    else
    {
      if (digitalRead(port[num]) == LOW)
      {
        pressed[num] = false;
        Serial.println("released");
      }
      else if ((last_pressed[num] + DEBOUNCE) < millis())
      {    
        retval = true;
        Serial.println("pressed");
      }
    }
  }
  return(retval);
}


void loop() {
unsigned long current_time = millis();
while (loop_timer >= current_time){
  current_time = millis();
}
loop_timer = current_time + LOOP_SPEED;


static bool waiting_for_color = true;

  if (waiting_for_color)
  {
      Serial.println("waiting");
    if (button_pressed(0))
    {
      color++;
      waiting_for_color = false;
    }
  }
  else
  {
    if (!button_pressed(0))
    {
      waiting_for_color = true;
    }
  }

  if (color == 0)
   
    leds[0] = CRGB::Red;
    
  else if(color == 1)
    
    leds[0] = CRGB::Blue;
    
  else if (color == 2)
  
    leds[0] = CRGB::Green;
    
  else if (color > 2)
    color = 0;

if (digitalRead(8) == HIGH)
  Serial.println("yes");
else if (digitalRead(8) == LOW)
  Serial.println ("no");
  
  counter ++;
  Serial.print ("counter: ");
  Serial.println(counter);

FastLED.show();
}

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