i’ve managed to make an LED that is blinking for some seconds and stay on for another seconds, but the problem is when i connect a button to control the LED it does not work. here’s my code
const int led = D0;
const int ledState = 0;
const int BUTTON = D1;
volatile int light = LOW;
void click(void);
void setup()
{
pinMode(led, OUTPUT);
attachInterrupt(BUTTON, click, CHANGE);
Serial.begin(9600);
}
void loop()
{
int cntr=5;
do
{
digitalWrite(led, !light);
delay(200);
digitalWrite(led, light);
delay(200);
cntr= cntr-1;
}
while (cntr!=0);
digitalWrite(led, !light);
delay(5000);
}
void click(){
light = !light;
}
I assume you still want the flashing to happen but the STEADY state for 5 seconds should be based on the button press? That is how I interpret your code.
Can you explain what you are seeing with the current software? It’s always a good idea to post what is happening rather than a “it doesn’t work” post.
It could be that the button is generating multiple signals to the interrupt as they bounce. You may need a delay of around 10-50ms where you don’t read any other changes on the pin. A timer is the ideal way to handle this. Don’t put any delay in the interrupt handler itself.
First I’d expect you should pinMode(BUTTON, INPUT_PULLUP); when you connect your button between D1 and GND or INPUT_PULLDOWN for D1 -> 3V3 (I’d go for GND).
And triggering for CHANGE you need to debounce the button, as @v8dave said.
There is a thread that talks about interrupt plus debounce already.
i’ve created a new one, but this time the button (with press and hold) now works but the LED is not blinking. It just turn on all the way and one thing, how can i reduce the brightness of the LED because its too bright.
byte SWITCHPIN= D0; //Set Pin 2 as Switch
byte LEDPIN = A0; //Set Pin 6 as LED
//If you want to go higher than 255 you must change from "byte" to "int"
boolean buttonstate; //Create a Integer variable named buttonstate
void setup()
{
pinMode(SWITCHPIN, INPUT); //Set Pin 2 as Input
pinMode(LEDPIN, OUTPUT); //Set Pin 6 as Output
}
void loop()
{
buttonstate = digitalRead(SWITCHPIN); //Continually look at the switch to see if its pressed
if (buttonstate == HIGH) //If the switch goes HIGH, act on it
{
****
this does not work
int cntr=5;
digitalWrite(LEDPIN, OUTPUT);
delay(200);
digitalWrite(LEDPIN, OUTPUT);
delay(200);
cntr= cntr-1;
while (cntr!=0);
digitalWrite(LEDPIN, OUTPUT);
delay(5000);
****
}
else {
//Once the switch is pressed again we break out of the loop above and then break out of the function completely and go back to our main loop
buttonstate = LOW; //First we tell the micro the switch is now LOW
analogWrite(LEDPIN, 0); //We turn the LED off before leaving our custom function
delay(500);
}
}
Well, I guess I'll have to repeat it a couple more times, but reading the manual definitely helps... http://docs.spark.io/firmware/#libraries-rgb RGB brightness can be found there.
Also, could you properly format your coding by checking this post:
This does not make sense. digitalWrite() takes LOW (0) or HIGH (anything else but 0) and not OUTPUT.
Additionally how should anything blink, when you set the LED to the same value on all three occasions? So I guess the code works as you have written it, you just expect it to do something you have NOT told it to do.
Have you had a look at your own code before you posted this?!?
This does not achieve anything, since the next time round loop() you'll read the actual buttonstate - no matter what story you try to tell the micro.
Please read the docs, do some research, search the forum and apply what you find out properly.