const int buttonPin1 = 2; // the number of the pushbutton pin
const int ledPin1 = 9; // the number of the LED pin
const int ledPin2=10;
const int buttonPin2 =3;
// variables will change:
int buttonState1 = 0;
int buttonState2 =0;// variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2,OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2=digitalRead(buttonPin2);
if (buttonState1 == LOW) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1,LOW);
delay(1000);
digitalWrite(ledPin1,HIGH);
delay(1000);
digitalWrite(ledPin1,LOW);
I am playing with interrupts recently.I am using two buttons and two LED so that both LED blink at different rates. When buttonPin1 is pressed(LOW) then the LED1 will blink at 1s.When buttonPin2 is pressed(LOW), then the LED2 will blink at 5s rate. But I plan to do interrupts which mean when LED1is blinking at 1s, when it is running this program, I press ButtonPin2, so that the LED2 will blink at 5s rate ?
Rarely. When the need arises, you will recognize it.
Can I have example schematics and code ?
Sure. For what?
Need to learn up interrupts
Why? Not to make two LEDs blink at different rates.
any other one simple just related to LED and switch ?
You can't get any simpler than the example.
Think about how YOU would turn the LEDs on and off at different rates. Forget about the switches, for the moment.
You need to record when each LED was turned on or off last, and the state it was set to. You need to decide, based on what time it is now and how long an LED is supposed to be on or off, whether it is time to change its state. If it is, change the state, and record the new time.
When you have that working, you can see that it is easy to add a test for whether the LED should be changing state, based on the switch state. If it is, then you already have code to make that happen. If not, then, simply don't call that code.