Some LED Trouble

Could someone help me understand what to do? I've verified it; the program works. However, when I begin simulating it only my RGB LEDs work and my other 3 LEDs with an on/off button don't function. I've looked up how to combine different sketches and I still fail to get it to fully work. Any help would be nice.

int counter;
//Begin button on/off
#define LED_1_PIN 4
#define LED_2_PIN 5
#define LED_3_PIN 6
#define BUTTON_PIN 7

#define LED_NUMBER 3

byte LEDPinArray[LED_NUMBER] = { LED_1_PIN,
LED_2_PIN,
LED_3_PIN };

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

byte lastTimeButtonState = HIGH;

byte LEDState = LOW;

void setup() {
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
// begine paste
initAllLEDs();
pinMode(BUTTON_PIN, INPUT_PULLUP);
//end paste
}

void loop() {
for (counter = 0; counter < 2260; ++counter) {
analogWrite(10, 51);
analogWrite(10, 204);
analogWrite(11, 0);
delay(5); // Wait for 5 millisecond(s)
analogWrite(6, 51);
analogWrite(6, 204);
analogWrite(9, 0);
delay(4.99); // Wait for 4.99 millisecond(s)
analogWrite(3, 51);
analogWrite(3, 204);
analogWrite(5, 0);
delay(4.98); // Wait for 4.98 millisecond(s)
}
delay(5000); // Wait for 5000 millisecond(s)
digitalWrite(10, LOW);
digitalWrite(6, LOW);
digitalWrite(3, LOW);
delay(21000); // Wait for 21000 millisecond(s)
analogWrite(10, 51);
analogWrite(10, 204);
analogWrite(11, 0);
delay(5); // Wait for 5 millisecond(s)
analogWrite(6, 51);
analogWrite(6, 204);
analogWrite(9, 0);
delay(4.99); // Wait for 4.99 millisecond(s)
analogWrite(3, 51);
analogWrite(3, 204);
analogWrite(5, 0);
delay(5000); // Wait for 5000 millisecond(s)
analogWrite(5, 255);
analogWrite(3, 0);
analogWrite(3, 0);
delay(5200); // Wait for 5200 millisecond(s)
analogWrite(9, 255);
analogWrite(6, 0);
analogWrite(6, 0);
delay(5800); // Wait for 5800 millisecond(s)
analogWrite(11, 255);
analogWrite(10, 0);
analogWrite(10, 0);
delay(10000); // Wait for 10000 millisecond(s)
//begin paste
unsigned long timeNow = millis();
if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastTimeButtonState) {
lastTimeButtonStateChanged = timeNow;
lastTimeButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
LEDState = (LEDState == LOW) ? HIGH : LOW;
powerOnAllLEDs(LEDState == HIGH);
}
}
}
//end paste
}
void initAllLEDs() {
for (int i = 0; i < LED_NUMBER; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}

void powerOnAllLEDs(bool powerOn) {
for (int i = 0; i < LED_NUMBER; i++) {
if (powerOn) {
digitalWrite(LEDPinArray[i], HIGH);
} else {
digitalWrite(LEDPinArray[i], LOW);
}
}
}

Hello soccerbot-2-0

Welcome to the world's best Arduino forum ever.

Post your sketch, well formated, with well-tempered comments and in so called code tags "< code >" to see how we can help.

I´ve made a small code review:
In addition to some another programming topics, the LedArray does not contain all the PWM pins used.

Have a nice day and enjoy coding in C++.

@soccerbot-2-0, please do not hijack. Thread split and moved.

Hi, @soccerbot-2-0
Welcome to the forum.

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

The code is rather bad. For example:-

What do you expect this to do? The first analogWrite will be totally ignored because it is overwritten by the second. You do this mistake a lot and even worst you do this:-

Which is just silly.

Next you carefully define names for some LEDs and then total ignore those names when you set the pin mode. Then you set some of the pin mode again in the nitAllLEDs function, but not all of them.

We have no idea what this means because you have not identified what pins are RGB and what pins are your other three.

As you are only simulating this then it will be easy enough for you to post a schematic.

As suggested by others:-
You might want to look at this How to get the best out of this forum before you proceed any further.

The data type for the delay() function is unsigned long.
You need to use whole numbers, so your 4.99ms gets truncated to 4ms.
See https://www.arduino.cc/reference/en/language/functions/time/delay/ for details.

You go round that for loop 2260 times and do the same thing each time.
Taking into account what Mike told you in post #5, then nothing effectively changes for approx 30 seconds.

With all the delays that you have, it takes over 80 seconds to go round loop().
You only check the state of the push button once during that time.

This means that you would need to press the button for a long time (up to 80s) before it gets detected as being pushed.

Sorry to everyone for wasting your time.


This is a draw out representation of what it looks like.
I use an Arduino UNO board and Tinkercad to simulate/ program.
As for the code; thank you everybody for helping me understand the computer language. I’ve just gotten interested into this type of work and would happily accept any advice. Thank you again.

I concur with your analysis. I have included colors into the loop function and it performs nominally. However when I introduce the delays it begins to mess with the response of the button. I am trying to figure out how to include such delays without it affecting my button. Is there any recommendations to solve such an issue?

Would it be possible to scale down the delays to only a second or two and for it to still perform nominally?

Thank you for directing me towards what I believe will solve my problem. After reading up throughly on the link you provided I now understand the pros and cons of delay. Now that I know this information, I know that I cannot use the delay function if I want to perform several task at the same time. Thank you.

I got the arduino to running both the RGB and Button simultaneously; Thanks for giving me the directives towards that code. Now, I am trying to work out how to change colors of the RGB without delay. I think it might be a bit too much going on, but any advice would be helpful.

Post the code that you used to get the arduino to run both the RGB and Button simultaneously.

Say a bit about how you want the new colour changing stuff to work.

Too early to say it will just mean more of the same kind of patttern.

a7

Are you sure? The RGB LEDs seem to have only one colour connected on each one and the single LEDs, if wired like you show would be on permanently.

The RGB LEDs should be wired up with an output and resistor on each of the three colour inputs. So you need three outputs per LED.

So clearly this does not represent what you have.
You might want to look at these links to help you.

Reading a schematic

Colin's Lab video on reading a schematic

The problem with your code is that it is too complex, that is you are trying to do too much to soon. Just start simple and build it up as you go along. Forget all the turning on and off in a complex sequence of delays, and just get them to simply blink, until you get the hang of what is going on.

With the help of you guys I've been able to get both functions working through the blink without delay program.

const int LED_PIN_1 = 3;            
const int LED_PIN_2 = 6;
const int LED_PIN_3 = 10;
const int BUTTON_PIN = 7;           

const long BLINK_INTERVAL_1 = 0;  
const long BLINK_INTERVAL_2 = 0;
const long BLINK_INTERVAL_3 = 0;

// Variables will change:
int ledState_1 = LOW;   // ledState used to set the LED 1
int ledState_2 = LOW;
int ledState_3 = LOW;

int previousButtonState = LOW; // will store last time button was updated

unsigned long previousMillis_1 = 0;   // will store last time LED 1 was updated
unsigned long previousMillis_2 = 0;
unsigned long previousMillis_3 = 0;

#define LED_1_PIN 1
#define LED_2_PIN 2
#define LED_3_PIN 4
#define BUTTON_PIN 7

#define LED_NUMBER 3

byte LEDPinArray[LED_NUMBER] = { LED_1_PIN,
                                LED_2_PIN,
                                LED_3_PIN};

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

byte lastTimeButtonState = HIGH;

byte LEDState = LOW; 

void initAllLEDs() 
{
  for (int i = 0; i < LED_NUMBER; i++) {
    pinMode(LEDPinArray[i], OUTPUT);
  }
}

void powerOnAllLEDs (bool powerOn)
{
  for (int i = 0; i < LED_NUMBER; i++) {
    if (powerOn) {
      digitalWrite (LEDPinArray[i], HIGH);
    }
    else {
      digitalWrite (LEDPinArray[i], LOW);
    }
  }
}

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

  // set the digital pin as output:
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  pinMode(LED_PIN_3, OUTPUT);
  
  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT);
  
  initAllLEDs();
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  
  unsigned long currentMillis = millis();

  // check to see if it's time to blink the LED 1
  if (currentMillis - previousMillis_1 >= BLINK_INTERVAL_1) {
    // if the LED is off turn it on and vice-versa:
    ledState_1 = (ledState_1 == LOW) ? HIGH : LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN_1, ledState_1);

    // save the last time you blinked the LED
    previousMillis_1 = currentMillis;
  }

  // check to see if it's time to blink the LED 2
  if (currentMillis - previousMillis_2 >= BLINK_INTERVAL_2) {
    // if the LED is off turn it on and vice-versa:
    ledState_2 = (ledState_2 == LOW) ? HIGH : LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN_2, ledState_2);

    // save the last time you blinked the LED
    previousMillis_2 = currentMillis;
  }
  
  // check to see if its time to blink the LED 3
  if (currentMillis - previousMillis_3 >= BLINK_INTERVAL_3) {
    // if the LED is off turn it on and vice-versa:
    ledState_3 = (ledState_3 == LOW) ? HIGH : LOW;
    
    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN_3, ledState_3);
    
    // save the last time you blinked the LED
    previousMillis_3 = currentMillis;
  }
  
  // check button state's change
  int currentButtonState = digitalRead(BUTTON_PIN);

  if(currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);

    // save the last state of button
    previousButtonState = currentButtonState;
  }

  unsigned long timeNow = millis();
  if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
    byte buttonState = digitalRead(BUTTON_PIN);
    if (buttonState != lastTimeButtonState) {
      lastTimeButtonStateChanged = timeNow;
      lastTimeButtonState = buttonState;
      if (buttonState == HIGH) { // button has been released
        LEDState = (LEDState == LOW) ? HIGH : LOW;
        powerOnAllLEDs(LEDState == HIGH);
      }
    }
  }
}

look this over -- use arrays

const byte PinLed []  = { 3, 6, 10 };
const int  BUTTON_PIN = 7;

const int  Nled = sizeof (PinLed);
      byte butState;

unsigned long msecInterval [Nled] = { 500, 800, 1300 };
unsigned long msecPrevious [Nled];

const int Nmode = 3;
      int mode;

// -----------------------------------------------------------------------------
void powerAllLeds (
    int  state )            // HIGH/LOW
{
    for (int i = 0; i < Nled; i++)
        digitalWrite (PinLed [i], state);
}

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long currentMillis = millis ();

    switch (mode)  {
    case 0:
        for (int n = 0; n < Nled; n++)  {
            if (currentMillis - msecPrevious [n] > msecInterval [n])  {
                msecPrevious [n] = currentMillis;

                digitalWrite (PinLed [n], ! digitalRead (PinLed [n]));
            }
        }
        break;

    case 1:
        powerAllLeds (HIGH);
        break;

    default:
        powerAllLeds (LOW);
        break;
    }

    byte but = digitalRead (BUTTON_PIN);
    if (butState != but) {
        butState = but;
        delay (20);                 // debounce

        if (but == HIGH)   // button released
            if (Nmode <= ++mode)
                mode = 0;
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    for (int n = 0; n < Nled; n++)
        pinMode (PinLed [n], OUTPUT);

    pinMode (BUTTON_PIN, INPUT_PULLUP);
    butState = digitalRead (BUTTON_PIN);
}

I had to mess with a bit. I also moved one of your LEDs off pin 1. Pins 0 and 1 are used for serial input and output and should be avoided until you just cannot.

//... works better with non-zero blink rates!
const long BLINK_INTERVAL_1 = 500;  
const long BLINK_INTERVAL_2 = 700;
const long BLINK_INTERVAL_3 = 900;

and

//... in real life, LEDs need to be on pins that are in OUTPUT mode
  // set the digital pin as output:
  pinMode(LED_1_PIN, OUTPUT);
  pinMode(LED_2_PIN, OUTPUT);
  pinMode(LED_3_PIN, OUTPUT);

after that, sure enough, three blinking lights and you can turn on and off all three of the other LEDs with the debounced push button.

Theres code in there that serves no purpose. Harmless, maybe, but in general it is a good idea to get rid of anything that isn't needed, just to make reading and tinkering with the code easier.

This and the declaration of previousButtonState can be deleted:

  // check button state's change
  int currentButtonState = digitalRead(BUTTON_PIN);

  if(currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);

    // save the last state of button
    previousButtonState = currentButtonState;
  }

Although I guess it was nice to see the switch changing states whilst you were getting this to work.

Here

  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT);
  
  initAllLEDs();
  pinMode(BUTTON_PIN, INPUT_PULLUP);

You call pinMode() again BUTTON_PIN, which overrides the first setting.

@gcjr's code does not seem to follow your schematic and handle the two tasks each of which is controlling three LEDs.

a7

i posted code that corrected a # of problems and demonstrated how a button press can change the mode to use powerOnAllLEDs() to either turn the LEDs on or off, or use the timers to flash the LEDs

  • seems there are redundant/conflicting definitions for LED/buttons
  • BLINK_INTERVAL_ ...

  • collapses 3 timers into one block of code using arrays

  • purpose-less button code ...

  • what's the purpose of calling powerOnAllLEDs() without stopping the LED timers above

Thank you for helping me understand how to concisely code a program/ function. I have learned a lot over the past 5 days about C++, how to write it, what some of the functions/ coding does, and how to use them properly such as void setup(), void loop(), delay(), analogWrite(,), and most importantly the blink without delay program. Thank you for helping a beginner like me understand at first a complex task and make it so much simpler.

Thank you for helping me create more precise coding that effectively conveys what I want to happen. You have helped me understand the world of coding and how to use it. Thank you.

Thank you for helping me understand the basics of coding and the true meaning/ uses of it. You have helped me speak the computer language immensely. Thank you.