Arduino Freezes With LED Strip Light Control

Attached is my code. I am trying to control RGB Strip lighting with 3 MOSFETs and 1 potentiometer with a button. The color changing works perfectly and the 4th mode of random color works well. The issue that I am getting is after the code runs for about 3 minutes, the Arduino freezes and in order to reset it, I need to unplug the Arduino for about 20 seconds to reset it. Any help would be greatly appreciated.

LED_Control.ino (4.64 KB)

Why do you suspect your code is the problem? It is obviously working properly. And your wiring is probably correct. The one thing not given is what is powering the system? How much current is being drawn by the LEDs?

To run that long and then fail, time after time makes the power supply suspect.

Paul

Paul_KD7HB could be right concerning the power supply. A good indicator is that you said you have to turn it off for 20 seconds. A programming error would respond even if the power was turned on for a lot shorter period. However, 20 seconds isn't really enough time for a power supply to "cool down" either (though it might be Long enough for a capacitor to discharge).

Or, the program might not really be "freezing" at all. Perhaps there is some obscure logic that makes it do the same Thing over and over which would appear to the eye that it has "frozen".

If you are using an Arduino Uno for example, you have a built in LED on pin 13. You can use that to easily/quickly create what is called a "heartbeat" to help in Debugging your code. If the heartbeat stops, the program has truly frozen. If the heartbeat continues, then the program is still running but not optically doing what you expect.

To create the heartbeat, add a variable before the Setup. For example, you could use bool flipFlop;

Then, at the very beginning of loop, before the line which has "buttonState = digitalRead(PotSwitch);" add the following lines of code.

digitalWrite(13, flipFlop); // Turn the LED on or off
flipFlop = ! flipFlop; // Reverse the on/off variable
delay(500); // Wait long enough for the eye to see something

    if (buttonState == HIGH) {
      count++;
      count++;

You know that there are other operators you could use, right?

    if (buttonState == HIGH) {
      count += 2;

Way easier to do that than to copy and paste code if you want to increment by 10 or 100.

Of course, count is a lousy name. There is no clue what is being counted.

  //    currentColorValueRed = random(0,255);
  //    currentColorValueGreen = 0;
  //    currentColorValueBlue = 0;
  //    analogWrite(redPin, currentColorValueRed);
  //    analogWrite(bluePin, currentColorValueBlue);
  //    analogWrite(greenPin, currentColorValueGreen);
  //     Serial.print(currentColorValueRed);
  //    Serial.print(currentColorValueGreen);
  //    Serial.println(currentColorValueBlue);
  //    delay(1000);
  //    currentColorValueRed = 0;
  //    currentColorValueGreen = random(0,255);
  //    currentColorValueBlue = 0;
  //    analogWrite(redPin, currentColorValueRed);
  //    analogWrite(bluePin, currentColorValueBlue);
  //    analogWrite(greenPin, currentColorValueGreen);

Do you suppose that this is what might be causing your problem? If not, DELETE THIS CRAP BEFORE POSTING CODE!

    currentColorValueRed = 0;
    currentColorValueGreen = 0;
    currentColorValueBlue = 0;

Wouldn't currentRed, currentGreen, and currentBlue convey just as much information?

Do you press the switch to cause the apparent freezing? Or, is count still 3 when the Arduino appears to freeze?

If it is, how do you know that the Arduino has apparently frozen, since you do nothing when count IS 3.

If count is not 3, what value DOES it have when the Arduino appears to freeze?

I did the count like that because I had to change the code and adding it again allowed me to change needs quicker. That code was supposed to be "deleted". That code was not causing the problem, just a test for serial and random RGB color idea. The freezing happens after 3 button presses when it is in the last loop that gives a random RGB color. It changes the color just fine, but then the Arduino freezes and the LEDs get stuck on the random color that was shown last. The circuit is being powered by a 30A 12v PSU.

The circuit is being powered by a 30A 12v PSU.

That tells us almost nothing. How does that power get to the LED strip? Through the Arduino? Or around it?

The freezing happens after 3 button presses

When count is what? I'm not going to figure that out for you. Serial.print() is your friend.

when it is in the last loop that gives a random RGB color.

I really don't understand this statement. You only have ONE loop, and it is the loop() function. There is nothing in loop() that causes looping.

The RGB strip has the positive going to the strips and the negative of each color being sent through the mosfets which are being controlled via the Arduino PWM. When I said loop, I was talking about the if statement loop which is giving a random RGB color.

When I said loop, I was talking about the if statement loop which is giving a random RGB color.

An if statement does not loop, so I still don't know what you are talking about.

You haven't answered the question about the value in count when the system exhibits unwanted behavior.

By the way, your code will never set the PWM pins to full, since the value returned by random will always be less than 255.