Go Down

Topic: Interrupts (Read 377 times) previous topic - next topic

vick5821

Quote

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);
   
  } 
  
  else if(buttonState2 ==LOW){
    

    digitalWrite(ledPin2, HIGH);
    delay(5000);
    digitalWrite(ledPin2, LOW);
   delay(5000);
  digitalWrite(ledPin2, HIGH);
 delay(5000);
digitalWrite(ledPin2, 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 ?

How can I do that ? What should I add ?

Thank you

kenny_-_

I think that maybe a better option is to look at http://arduino.cc/en/Tutorial/BlinkWithoutDelay
you don't need an interrupt then  :)

vick5821

When we use interrupts ? Can I have example schematics and code ? Need to learn up interrupts

kenny_-_

What you need to look at then is http://arduino.cc/playground/Main/ReadingRPM

here is a good reason for using interupts and a working code for it  :)

vick5821

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

But this is not related to switch..It is just blinking the LED without using delay function..isnt that ?

vick5821

any other one simple just related to LED and switch ?

Thank you

PaulS

Quote
When we use interrupts ?

Rarely. When the need arises, you will recognize it.

Quote
Can I have example schematics and code ?

Sure. For what?

Quote
Need to learn up interrupts

Why? Not to make two LEDs blink at different rates.

Quote
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.

Go Up