initialisations in the wrong loop - good point. I tried to have them inside void Setup() {...} but I ended up with errors that the variables did not exist in that scope. just some rusty and lack of C knowledge. (my bad... how do you initialise a global variable?) [did I get it right? - see below]
+= and -= are compound addition and subtraction. is it good practice to use "x = x + y" not "x += y" I used to know a healthy amount of basic and never got any C education, so I figured I would use the += functions when available
the trouble is that I am using fadeValue as both the delay time and the counter in the for-loop. should I not?
I guess I do know that my increment is 1/50th of total power, so I could just make fadeValue into a counter and make the delay look like
for (counter = 1 to 50; counter ++) {
digitalWrite(ledPin, HIGH);
delay(fadeValue*2) ;
digitalWrite(ledPin, LOW);
delay (TimeofFade - fadeValue*2);
}
etc etc.
which way is neater//better coding practice?
I didnt realise that delay takes an unsigned long... I was working in DelayMicroseconds() but I thought I just had too many zeros. guess I will move back to that. save me a decimal point... oh also I was getting worried about overflow from the (I think) 32000 or so limit of a normal interger. working with 25000us... (and now I am using long)
here is what we got: didn't get a chance to test it just yet. but the last one was working and this one compiles fine..
int ledPin=13; // uses arduino D13 LED
long DelayValue = 500; // increment of power
long TimeofFade = 25000; // width of one pulse
void setup() {
Serial.begin(9600); //I like to talk to my arduino and have it report back to me
Serial.println("start");
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop(){
// total length of time from 0 to 100% is (TimeofFade/DelayValue)*TimeofFade (TOF/DV is the number of times the loop is completed, * length of time in the loop)
// in this case 25000/500 = 50/1 = 50*25000 = 1250000us = 1250ms
// total loop time is 2500 + the two delays between increase and decrease the middle to add smoothness
//this is going from 0->100%
for(long FadeValue=0; FadeValue <= TimeofFade; FadeValue += DelayValue) {
digitalWrite(ledPin, HIGH);
delayMicroseconds(FadeValue); // ON for the incrementing FadeValue
digitalWrite(ledPin, LOW);
delayMicroseconds(TimeofFade-FadeValue); // OFF for the decrementing remaining "pulse width"
// Serial.println(FadeValue);
}
digitalWrite(ledPin, HIGH); delayMicroseconds (TimeofFade);
//this is going from 100->0%
for(long FadeValue=TimeofFade; FadeValue >=0; FadeValue -=DelayValue){
digitalWrite(ledPin, HIGH);
delayMicroseconds(FadeValue); // ON for the decrementing FadeValue
digitalWrite(ledPin, LOW);
delayMicroseconds(TimeofFade-FadeValue); // OFF for the incrementing remaining "pulse width"
// Serial.println(FadeValue);
}
digitalWrite(ledPin, LOW); delayMicroseconds (TimeofFade);
}