I need to write a simple program, in which a LED has to blink at a variable speed. And I can control that speed with a button.
So far, I've just been trying to get the LED to blink at that variable speed, but it always seems to blink at the same speed.
Here's my code:
//Constants
const int ledPin = 0//Number of the LED pin
//Variables
boolean ledState = LOW; //State the LED is in
int intervals[] = {200, 500, 1000, 2000, 10000}; //Intervals of time
int timeStep = 4; //Step of time in intervals
void setup() {
//Set the LED pin as output
pinMode(ledPin, OUTPUT);
}
void loop() {
delay(intervals[timeStep]); //Waits for a certain amount of time based on timeStep
if (ledState == LOW) //Inverts the ledState
ledState = HIGH; //Cant I do ledState = !ledState ?
else
ledState = LOW;
digitalWrite(ledPin, ledState); //Sets the LED to the ledState
}
I should be able to change the speed with timeStep, but changing it does nothing for me.
My setup is just a cable going from pin 0 to a 220 ohm resistor, to a led, back to ground.
hi
you need to change the timestep value by somthing like this:
timestep = 2;
and somewhere else in your code, change it again.
for example see this blink 10 times for each speed and then changes the speed
int led = 13;
int intervals[] = {200, 500, 1000, 2000, 5000}; //Intervals of time
int timestep = 0;
char i;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
for(i=0;i<10;i++){
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(intervals[timestep]);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(intervals[timestep]);
}
timestep++;
if (timestep==5)
timestep=0;
}