Lighting Effects Project Help Needed

All,

I am trying to create a light object as follows. Using an Arduino (nano likely) connected to a Neopixel 8 LEDs strip (NeoPixel Stick - 8 x 5050 RGBW LEDs - Cool White - ~6000K : ID 2869 : $7.95 : Adafruit Industries, Unique & fun DIY electronics and kits) connected to pin 6, and a button to ground on pin A0, when booted, all LEDs should light white. Then, upon pressing the button, I would like it to cycle through some different lighting effects, basically, all LEDs red, all LEDs blue, all LEDs green, then a random fading in and out of various colors, then back to all LEDs white.

I am no programmer, so I have been trying to generate code with ChatGPT, and using Tinkercad to run simulations, but it is not working. The final code so far is:

#include <Adafruit_NeoPixel.h>

#define PIN            6    // Define the pin to which the data input of the NeoPixel strip is connected
#define NUMPIXELS      8    // Define the number of NeoPixels in your strip
#define BUTTON_PIN     A0   // Define the pin to which the analog button is connected

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int buttonState = 0;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int currentMode = 0;

void setup() {
  strip.begin();
  strip.show();  // Initialize all pixels to 'off'

  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  int reading = analogRead(BUTTON_PIN);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState > 900) {  // Adjust this threshold based on your button characteristics
        // Button is pressed, change lighting mode
        currentMode = (currentMode + 1) % 5;

        switch (currentMode) {
          case 0:
            setAllPixelsColor(255, 255, 255, 0);  // White
            break;
          case 1:
            setAllPixelsColor(255, 0, 0, 0);  // Red
            break;
          case 2:
            setAllPixelsColor(0, 0, 255, 0);  // Blue
            break;
          case 3:
            randomFade();
            break;
          case 4:
            setAllPixelsColor(0, 0, 0, 0);  // Turn off all LEDs
            break;
        }
      }
    }
  }

  lastButtonState = reading;
}

void setAllPixelsColor(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  for (int i = 0; i < NUMPIXELS; i++) {
    strip.setPixelColor(i, strip.Color(r, g, b, w));
  }
  strip.show();
}

void randomFade() {
  for (int i = 0; i < 256; i++) {
    for (int j = 0; j < NUMPIXELS; j++) {
      uint8_t color = random(256);
      strip.setPixelColor(j, strip.Color(color, color, color, 0));
    }
    strip.show();
    delay(20);
  }
}

When I simulate the code in Tinkercad, absolutely nothing happens. The LEDs don't light upon boot up, and pressing the button does nothing.

Any help is greatly appreciated.
Thanks,
Michael

What does ChatGPT have to say about why it does not work ? You might start by asking why analogRead() is being used to read a digital signal from a pushbutton

Frankly, I, and others here, are fed up being asked to fix code generated by ChatGPT

1 Like

Sorry to hear that you aren't happy to be asked to fix ChatGPT code, but the ease of creation makes it a go to for those who aren't programmers or are just setting out learning.

I have tried it with both digital and analog button settings, and it doesn't make a difference.

ChatGPT keeps taking the criticism, but then eventually tells me it doesn't know why it isn't working, and that I should read more about the Neopixels and how to program them.

As you have discovered, creating code is easy but creating code that both works and does what you want is much more difficult.

You should also not discount the possibility that the problem is with your hardware or how you have configured it. For instance, how is the input pin wired ?

Yes, I have learned that.

I have wired the switch to the appropriate input pin, usually A0. I have tried simply connecting one pin of the switch to ground, so when pushed, it grounds. I have also tried with the switch floating high, with a press to ground.

Also, just want to make sure you are aware, I have been testing in Tinkercad running a simulation.

From an educational point of view, i am disappointed. Still if you understand what is going on, you are on your way to becoming a programmer, which is what life is all about.

So the advantage of ChatGPt code is that it is fairly readable.

pinMode(BUTTON_PIN, INPUT);

This should probably be

pinMode(BUTTON_PIN, INPUT_PULLUP);

And you might want to add a small delay to the loop.

adafruit neopixel uses a 50us break by defaiult and ws2812b's actually need 300us.(or at least that is the spec)
This usually doesn't show up because the MCU takes more time to get to the next show();

void setAllPixelsColor(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  for (int i = 0; i < NUMPIXELS; i++) {
    strip.setPixelColor(i, strip.Color(r, g, b, w));
  }
  strip.show();
  delay(1);
}

So eh, you got a program written by a computer and then simulated by another program, and nothing happened.
This is perfect, because nothing really happened, or well you didn't really do anything, you didn't write anything, you didn't connect anything, you didn't try anything, and it was free.

If you want something to happen you should actually do the hardware thing, or the computers may cheat you into thinking something does or does not work. Don't trust them devilish things.

As an aside: I've tried using ChatGPT to generate a few arduino programs and I've been surprised at how good it is. That said, I am very careful to specify all the relevant conditions, but damn, I'm impressed.

Not out of a job just yet, tho :slight_smile:

Imagine you work in any profession. Someone grabs something out of a dumpster and brings it to you to fix. You could make a product better, faster and with less effort if you started with a bucket of ingredients. ChatGPT is that dumpster find... and everyone here is you, dismantling the junk to its fundamental parts, then build a new one. The "ease" on one end is moved to someone else's end. Imagine you skipping customers who have put real effort into their project because most of your customers take the ChatGPT way. ChatGPT is a cute idea, but it produces dumpster code. Teach yourself coding (and fixing ChatGPT "code"). You will be happier for it.

2 Likes

But this code was readable and wasn't to bad.
Still if the user doesn't understand it, it doesn't serve much purpose, but it looked better than most stuff found online. Bur what is the point of then using a simulator. It's one or the other. Either write your own code and use a simulator for testing, or use something like ChatGPT and use actual hardware. But not both.

Hi,

Or write your code in stages, with relative hardware connected as you go.
Debug as you go, forget about simulators, hands on experiment you learn a lot quicker about programming and electronics hardware.

Tom... :grinning: :+1: :coffee: :australia:

Ease of not creating but rather generating garbage.
And get this.. it's not even YOUR garbage!
You won't learn much about code by practicing "not coding" any more than those who play the "do it for me" game.

I can read a pin digitally and react in less than a microsecond.
A default analog read takes over 100 microseconds.
OTOH if I have several buttons with different resistors, an analog pin can tell them apart and ...
analog pins don't bounce but changing pins requires settling time to get a good read. A fast 8 bit read can be done in only 50 micros or so.

See if ChatGPT can read up and learn for you since it's so good. Until you put in the practice, you won't get long term memory connections in your own head to do this for yourself.
can't code, get ChatGPT. Can't tie your shoes, wear sandals or loafers!

2 Likes

BTW, I paid $15 ea for WS2811 50 RGB led bulb strings pre-2020.
They are very cut & splice friendly and have 12mm diffuser bulbs better for display than little bright dots close in line, which have their own better-at's too,

Have you tried the FastLed library? It lets you make an array of RGB values to set and run the show function. It has tons of effect functions as well to dive into but diddling with the array and calling show doesn't take dealing with the rather massive rest.

But has no error checking on writing beyond the size of the array.
I prefer Makuna Neopixelbus. Also loads of effects and support for matrices etc.

Not a problem for me. I don't color outside the lines in code.
I write my own effects.

1 Like

But newbies tend to.

So the problem in newbies, not in code

Funny thing is, if you'd spent an even amount of time reading examples here and running them in wokwi, you'd learn something and probably have finished your code by now.

The problem is no error checking within the library.

I think there are two main problems -

  1. ChatGTP can't read schematics. Is that true? Probably soon it will learn. Normally, a schematic won't show every-single NeoPixel so it would have to understand the notes too.

  2. You have to tell ChatGTP EXACTLY what you want to do. That's pretty-much what a program is... It tells the processor EXACTLY what to do, in a very-precise programming language that the processor understands. (Actually, the processor is running machine code translated/compiled from C++).

1 Like

need to LEARN which when done by making mistakes teaches what the book did not say.

What to save beginners from is not getting sharper at writing code.

And if the bounds check is dynamic then the code will be SLOWER.

I don't just color inside the lines. Often I shape my code so the colors keep themselves inside but I had to see WHY to do that and a Mommy System would not let me see that.