thanks in advance for any help.
I am trying to write a program to pulse LEDs with PWM, but vary the duty cycle to brighten and dim them each independently. I have seen something in an advanced forum that used individal times, and maybe that is necessary, but I wanted to try my hand at this using various techniques from this and other forums.
I am using an Adafruit Metro 4 express (SAMD51) and yes tthe pins are PWM, they work properly with some versions of the code. One, the first function call runs over and over, I was trying to get it to end, and go one to the next function call.
Here is my code.
unsigned long StartMillis;
unsigned long CMillis;
const unsigned long fadePeriod = 100; //fade period
int fadeLedPin[] = {2, 3, 4, 5, 6, 7}; //this LED will fade
int brightness = 0; //initial brightness of LED
int increment = 17; //amount to change PWM value at each change
int ledpin;
int led;
int x;
int count;
void setup()
{
Serial.begin(115200); //start Serial in case we need to print debugging info
StartMillis = millis(); //start time of fading LED
}
void loop()
{
// int count = 0;
CMillis = millis(); //get the current time
// Serial.println(count);
fade(fadeLedPin[1]);
fade(fadeLedPin[0]);
}
void fade(int led) //function to fade an LED
{
if (CMillis - StartMillis >= fadePeriod) // && (count <= 15)) //test whether the period has elapsed
{
analogWrite(led, brightness); //set the brightness of the LED
brightness += increment;
// Serial.println(brightness);
if (brightness >= 254) {
brightness = 0;}
return;
// Serial.println(CMillis);
StartMillis = CMillis; //IMPORTANT to save the start time of the current LED state.
// count = count + 1;
}
}
a couple of things. The first, and kind of unrelated is that I tried to use a counter ( int count;) set it to zero (0), when I try to troubleshoot using Serial.println(count) the thing always starts at 10! I really don’t get that for sure!
I have two LEDs hooked up (pins 2, 3) and would just like the function to exit back to the loop with some kind of test, I tried a timer, a break, a continue, etc. If the function returns it is usually with brightness at max, and both LEDS on max.
Anyway, some help would be great for me, I am in lockdown, and programming is what I have been learning this wither season and it keeps my brain occupied.
Roger