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

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
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…