Void loop works once, then it goes random

Hello there,

i'm facing a problem where the void loop works well 1 time but after it finished the loop it goes into random mode.
i'm using an atmega328p as a standalone, and occupy all 6 the pwm pins
I also have a version where i only use 3 pins and the works perfectly fine. Could it be the hardware side instead of the code itself?

any input would be appreciated as i'm a hobbyist.

int ledPin1 = 11;   // LED connected to digital pin 11
int ledPin2 = 10;   // LED connected to digital pin 10
int ledPin3 = 9;    // LED connected to digital pin 9
int ledPin4 = 6;    // LED connected to digital pin 6
int ledPin5 = 5;    // LED connected to digital pin 5    
int ledPin6 = 3;    // LED connected to digital pin 3
int fadeValue1 = 0;
int fadeValue2 = 0;
int fadeValue3 = 0;
int fadeValue4 = 0;
int fadeValue5 = 0;
int fadeValue6 = 0;

void setup()  
{   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
} 

void loop()
{
    fade1();
    fade2();
    fade3();
    fade4();
    fade5();
    fade6();
}


void fade1() 
{ 
  // fade in from min to max in increments of 1 points:
  for(fadeValue1 = 0 ; fadeValue1 <= 255; fadeValue1 +=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue1);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }

  // fade out from max to min in increments of 1 points:
  for(fadeValue1 = 255 ; fadeValue1 >= 1; fadeValue1 -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue1);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}

void fade2()
{
  // fade in from min to max in increments of 2 points:
  for(fadeValue2 = 0; fadeValue2 <= 255; fadeValue2 +=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin2, fadeValue2);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }  

  // fade out from max to min in increments of 2 points:
  for(fadeValue2 = 255 ; fadeValue2 >= 1; fadeValue2 -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin2, fadeValue2);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}

void fade3()
{
  // fade in from min to max in increments of 3 points:
  for(fadeValue3 = 0; fadeValue3 <= 255; fadeValue3 +=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin3, fadeValue3);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
} 

  // fade out from max to min in increments of 3 points:
  for(fadeValue3 = 255 ; fadeValue3 >= 1; fadeValue3 -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin3, fadeValue3);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}
  void fade4()
{
  // fade in from min to max in increments of 3 points:
  for(fadeValue4 = 0; fadeValue4 <= 255; fadeValue4 +=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin4, fadeValue4);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
} 

  // fade out from max to min in increments of 3 points:
  for(fadeValue4 = 255 ; fadeValue4 >= 1; fadeValue4 -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin4, fadeValue4);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}
  void fade5()
{
  // fade in from min to max in increments of 3 points:
  for(fadeValue5 = 0; fadeValue5 <= 255; fadeValue5 +=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin5, fadeValue5);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
} 

  // fade out from max to min in increments of 3 points:
  for(fadeValue5 = 255 ; fadeValue5 >= 1; fadeValue5 -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin5, fadeValue5);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}
  void fade6()
{
  // fade in from min to max in increments of 3 points:
  for(fadeValue6 = 0; fadeValue6 <= 255; fadeValue6 +=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin6, fadeValue6);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
} 

  // fade out from max to min in increments of 3 points:
  for(fadeValue6 = 255 ; fadeValue6 >= 1; fadeValue6 -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin6, fadeValue6);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}

You for loop index variables should NOT be global. There is no reason to have a different name for the for loop index in each function.

Your comments are either obvious or wrong or both. Get rid of the useless ones. Fix the wrong ones.

What do your serial print statements tell you is actually happening?

Consistent placement of { is a good thing. Either put them all on lines by themselves, where they belong, or put them all on the end of the statement that they go with. Do not use both styles in one program.

Thank you for your input,

i cleaned up the code a bit.
It seems to work when i use it with my arduino plugged in the pc, now i will go test if it works on the standalone.

int ledPin1 = 11;   // LED connected to digital pin 11
int ledPin2 = 10;   // LED connected to digital pin 10
int ledPin3 = 9;    // LED connected to digital pin 9
int ledPin4 = 6;    // LED connected to digital pin 6
int ledPin5 = 5;    // LED connected to digital pin 5    
int ledPin6 = 3;    // LED connected to digital pin 3
int fadeValue1 = 0;
int fadeValue2 = 0;
int fadeValue3 = 0;
int fadeValue4 = 0;
int fadeValue5 = 0;
int fadeValue6 = 0;

void setup()  
{   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
} 

void loop()
{
    fade1();
}


void fade1() 
{ 
 
  for(fadeValue1 = 0 ; fadeValue1 <= 255; fadeValue1 +=1)
  { 
    analogWrite(ledPin1, fadeValue1);             
    delay(10);                            
  }
   for(fadeValue1 = 255 ; fadeValue1 >= 1; fadeValue1 -=1) 
  { 
    analogWrite(ledPin1, fadeValue1);            
    delay(10);                            
  }
  for(fadeValue2 = 0; fadeValue2 <= 255; fadeValue2 +=1) 
  { 
    analogWrite(ledPin2, fadeValue2);           
    delay(10);                            
  }  
  for(fadeValue2 = 255 ; fadeValue2 >= 1; fadeValue2 -=1) 
  {   
    analogWrite(ledPin2, fadeValue2);              
    delay(10);                            
  }
  for(fadeValue3 = 0; fadeValue3 <= 255; fadeValue3 +=1)
  {    
    analogWrite(ledPin3, fadeValue3);         
    delay(10);                            
  } 
  for(fadeValue3 = 255 ; fadeValue3 >= 1; fadeValue3 -=1)
  { 
   analogWrite(ledPin3, fadeValue3);        
   delay(10);                            
  }
  for(fadeValue4 = 0; fadeValue4 <= 255; fadeValue4 +=1) 
  { 
   analogWrite(ledPin4, fadeValue4);         
   delay(10);                            
  } 
  for(fadeValue4 = 255 ; fadeValue4 >= 1; fadeValue4 -=1) 
  {  
  analogWrite(ledPin4, fadeValue4);                 
  delay(10);                            
  }  
  for(fadeValue5 = 0; fadeValue5 <= 255; fadeValue5 +=1) 
  { 
   analogWrite(ledPin5, fadeValue5);         
   delay(10);                            
  } 
  for(fadeValue5 = 255 ; fadeValue5 >= 1; fadeValue5 -=1) 
  {  
   analogWrite(ledPin5, fadeValue5);         
   delay(10);                            
  }
  for(fadeValue6 = 0; fadeValue6 <= 255; fadeValue6 +=1)
  { 
  analogWrite(ledPin6, fadeValue6);         
  delay(10);                            
  } 
  for(fadeValue6 = 255 ; fadeValue6 >= 1; fadeValue6 -=1) 
  { 
  analogWrite(ledPin6, fadeValue6);          
  delay(10);                            
  }
}

The standalone keeps having the same issue,
one loop will work then halfway through the second one it starts doing random on offs.

ryukenden:
i cleaned up the code a bit.

hmmm...

you should be passing pin numbers to a much simpler function...

ryukenden:
It seems to work when i use it with my arduino plugged in the pc, now i will go test if it works on the standalone.

what is "the standalone" power supply?

The standalone power supply is a 5 volt 3 amp adapter.

ryukenden:
The standalone power supply is a 5 volt 3 amp adapter.

How is it connected to the Arduino ?

this is really akward:

for(fadeValue1 = 0 ; fadeValue1 <= 255; fadeValue1 +=1)
  { 
    analogWrite(ledPin1, fadeValue1);             
    delay(10);                            
  }
   for(fadeValue1 = 255 ; fadeValue1 >= 1; fadeValue1 -=1) 
  { 
    analogWrite(ledPin1, fadeValue1);            
    delay(10);                            
  }

why do you using global variables there?

try something like this (untested) which just uses one function and iterates over an array of pins:

#define FADE_INTERVAL 10 // milliseconds
#define FADE_INCREMENT 1

byte pwmPins[] = {3,5,6,9,10,11};

void setup() 
{
  for(auto pin : pwmPins)
    pinMode(pin, OUTPUT);
}

void loop() 
{
  static byte activeLed = 0;
  if (fadeLed(activeLed))
  {
    activeLed++;
    activeLed %= sizeof(pwmPins);
  }
}

bool fadeLed(byte pin)
{
  static unsigned long lastChangeMillis = 0;
  static int pwmValue = 0;
  int fadeDirection = 1;
  if (millis() - lastChangeMillis > FADE_INTERVAL)
  {
    pwmValue += fadeDirection * FADE_INCREMENT;
    if (pwmValue >= 255)
    {
      pwmValue = 255;
      fadeDirection *= -1;
    }
    else if (pwmValue <= 0)
    {
      pwmValue = 0;
      fadeDirection *= -1;
      return true;
    }
    analogWrite(pin, pwmValue);
    lastChangeMillis = millis();
  }
  return false;
}

Its connected via a 3.mm jack socket UKHeliBob,

and i will try the code you wrote BulldogLowell,

the code from BulldogLowell doesn't seem to work

It seems to work when i use it with my arduino plugged in the pc,

Show us your hardware.

Its connected via a 3.mm jack socket UKHeliBob,

How is it connected to the Arduino itself ? Which pins, connectors or PC pads ?
Which Arduino board are you using ?

This is the standalone layout without the 3.5jack on this one.

I remember that i had this problem before when i was using the 3 pin code, it also went random after 1 loop. That one i could fx by placing a capacitor in the circuit.

However this circuit is the same and yet it doesn't work.
The standalone is driving 100 leds, maybe its to much to handle?

its a arduino uno.

Will you please answer my question
Which pins, connectors or PC pads is the 5V and GND connected to ?

ryukenden:
Its connected via a 3.mm jack socket UKHeliBob,

Hmmm UNO documentation

Power The Arduino Uno board can be powered via the USB connection or with an external power supply. The power source is selected automatically. External (non-USB) power can come either from an AC-to-DC adapter (wall-wart) or battery. The adapter can be connected by plugging a 2.1mm center-positive plug into the board's power jack. Leads from a battery can be inserted in the GND and Vin pin headers of the POWER connector. The board can operate on an external supply from 6 to 20 volts. If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may become unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.

Sorry for the late reply, here is a pic of the standalone,
The ground is connected to pins 8 - 22 of the atmega
the 5v is connected to pins 7 -20

ryukenden:
the code from BulldogLowell doesn't seem to work

yep...

bool fadeLed(byte pin)
{
  static unsigned long lastChangeMillis = 0;
  static int pwmValue = 0;
  int fadeDirection = 1;  <<<<< BUG here!!

should be static!

revised code:

#define FADE_INTERVAL 10 // milliseconds
#define FADE_INCREMENT 1

byte pwmPins[] = {3,5,6,9,10,11};

void setup() 
{
  Serial.begin(9600);
  for(auto pin : pwmPins)
    pinMode(pin, OUTPUT);
}

void loop() 
{
  static byte activeLed = 0;
  if (fadeLed(activeLed))
  {
    activeLed++;
    activeLed %= sizeof(pwmPins);
    Serial.print(F("New Led:\t"));
    Serial.println(activeLed);
    //delay(1000);
  }
}

bool fadeLed(byte pin)
{
  static unsigned long lastChangeMillis = 0;
  static int pwmValue = 0;
  static int fadeDirection = 1;
  if (millis() - lastChangeMillis > FADE_INTERVAL)
  {
    pwmValue += (fadeDirection * FADE_INCREMENT);
    if (pwmValue >= 255)
    {
      pwmValue = 255;
      fadeDirection = -1;
    }
    else if (pwmValue <= 0)
    {
      pwmValue = 0;
      fadeDirection = 1;
      return true;
    }
    //Serial.println(pwmValue);
    analogWrite(pin, pwmValue);
    lastChangeMillis = millis();
  }
  return false;
}

I tried the revised code,it pulses one pin only and doesn't shift to the other ones.

ryukenden:
I tried the revised code,it pulses one pin only and doesn't shift to the other ones.

what happens in the Serial monitor?

the serial output below.