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.
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?
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.