• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

interrupt code not being complied in Arduino IDE

Sashvat

Member
hi guys, I was learning how to use interrupts on Arduino and saw this tutorial -
I wrote the code and when I compiled it the IDE didn't and had an error message saying "buttonPressed was not declared in this scope". What have I done and what is wrong with this code? I have attached the file I have written here, please let me know.

thank you
 

Attachments

Nigel Goodwin

Super Moderator
Most Helpful Member
You can't put your routine inside the main loop - move it outside and it compiles OK.

C++:
volatile boolean ledOn = false;


void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(2, INPUT);
attachInterrupt(0,buttonPressed,RISING);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void buttonPressed(){
  if(ledOn){
    ledOn = false;
    digitalWrite(13, LOW);
  }else{
    ledOn = true;
    digitalWrite(13, HIGH);
  }
}
 

Sashvat

Member
You can't put your routine inside the main loop - move it outside and it compiles OK.

C++:
volatile boolean ledOn = false;


void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(2, INPUT);
attachInterrupt(0,buttonPressed,RISING);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void buttonPressed(){
  if(ledOn){
    ledOn = false;
    digitalWrite(13, LOW);
  }else{
    ledOn = true;
    digitalWrite(13, HIGH);
  }
}
Thank you very much finally got it
 

Latest threads

EE World Online Articles

Loading

 
Top