Robin2:
In my opinion (and it is no more than that) your starting point is on very poor foundations.If someone has a piece of code that uses analogWrite() in several (never mind 30) different locations to set the PWM for a single pin they have already got serious problems that you just don't want to get involved with.
And, again, the user with such complex code would be much better helped by explaining how to organize it. And the need to have the PWM info in a single easily accessed variable might be just the stimulus they need to mend their ways.
agreed ![]()
lets look at it in a different way.
with my new function ! could wright something like:
void loop(){
if (digitalRead(2)){ // slow down or trun off PWM
analogWrite(3,0);
analogWrite(5,max(0,analogOutputReadPWM(5)-10));
analogWrite(6,max(0,(analogOutputReadPWM(6)*.5)-1)); // lets ramp Down the PWM to 0 with some decelleration
} else { // Speed up PWM
analogWrite(3,min(255,analogOutputReadPWM(3)+1)); // lets ramp up the PWM to 255 by 1
analogWrite(5,min(255,analogOutputReadPWM(5)+5)); // lets ramp up the PWM to 255 by 5
analogWrite(6,min(255,(analogOutputReadPWM(6)*1.5)+1)); // lets ramp up the PWM to 255 with some acceleration
}
if(analogRead(4)<=500){
analogWrite(5,max(0,analogOutputReadPWM(5)-5)); //this would cancel the Speed up PWM if analogRead shows a value less than 500
}
delay(100);
}
I am having fun with this new function analogOutputReadPWM that I created and it has already simplified several things I have done int the past that once took multiple lines of code and lots of variables to store stuff in.