system
April 23, 2011, 2:25am
1
Looking for some help with my simple program. Just getting started and trying to have a while loop increment and the delay to decrease while blinking an LED. My first counter works fine but I cannot get the led delays to get smaller as it increments. Anyone with any thoughts?
int counter = 0;
int increment_delay = 1000;
int speed_increase = 100;
void setup()
{
pinMode(13, OUTPUT);
}
void loop ()
{
while (counter < 15)
{
digitalWrite(13, HIGH);
delay(increment_delay - speed_increase);
digitalWrite(13, LOW);
delay(increment_delay - speed_increase);
counter++;
speed_increase + 100;
}
}
system
April 23, 2011, 2:52am
2
The variable "speed_increase" is never modified.
You have:
speed_increase + 100;
Which actually does nothing.
You want
speed_increase = speed_increase + 100;
or
speed_increase += 100;
The problem THEN will be that once speed_increase is over 1000 the subtraction will result in a negative value. You're going to want to rethink that.
system
April 23, 2011, 3:16am
3
You got me on the right track,
ended up with something like:
int counter = 0;
//int delay = 1000;
int speed_increase = 1000;
void setup()
{
pinMode(13, OUTPUT);
}
void loop ()
{
while (counter < 30)
{
digitalWrite(13, HIGH);
delay(speed_increase);
digitalWrite(13, LOW);
delay(speed_increase);
counter++;
speed_increase = speed_increase - 33;
}
}
system
April 23, 2011, 5:09am
4
Nailed it!
I like the way you calculated the minimum possible delay to be 10 ms. Good math skills.
system
April 23, 2011, 1:19pm
5
speed_increase = speed_increase - 33;
Strange name for a variable that only decreases in value...