Continue to Site

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.

  • 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

Status
Not open for further replies.

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

  • interrupts_2.ino
    391 bytes · Views: 204
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);
  }
}
 
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
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top