Hello, I cannot understand something here:
http://www.arduino.cc/en/Reference/AttachInterrupt
If I want to enable the pin 20 for interrupt. What I put on the "ISR" position?
Hello, I cannot understand something here:
http://www.arduino.cc/en/Reference/AttachInterrupt
If I want to enable the pin 20 for interrupt. What I put on the "ISR" position?
ISR (the second argument) is the function to call to handle the interrupt. It must return nothing (ie, void) and take no arguments. It works the same way regardless of which pin it's on.
The call for pin 20 would be something like this
attachInterrupt(digitalPinToInterrupt(20), functionToCallOnInterrupt, CHANGE);
You would have to define something like
void functionToCallOnInterrupt() {
//put the stuff you want to do here
}
This is the code! It does not work!
const int intPin = 20;
void play()
{
Serial.println("Hello World");
delay(1000);
}
void setup()
{
pinMode(intPin, INPUT);
Serial.begin(38400);
}
void loop()
{
attachInterrupt(digitalPinToInterrupt(intPin),play,CHANGE);
}
const int intPin = 20;
void play()
{
Serial.println("Hello World"); //you shouldn't print to serial in an ISR either...
//delay(1000); //Please read the page you linked above. delay() doesn't work inside the ISR - since the interrupt that it uses to keep time is disabled, delay() will block forever.
}
void setup()
{
pinMode(intPin, INPUT);
attachInterrupt(digitalPinToInterrupt(intPin),play,CHANGE); //You want to attach the interrupt only once. You use detachInterrupt to remove it if you want to get rid of it, it doesn't get removed when it fires.
Serial.begin(38400);
}
void loop()
{
}
Better way - avoiding the serial.print();
const int intPin = 20;
byte isrFired=0;
void play()
{
isrFired=1;
}
void setup()
{
pinMode(intPin, INPUT);
attachInterrupt(digitalPinToInterrupt(intPin),play,CHANGE); //You want to attach the interrupt only once. You use detachInterrupt to remove it if you want to get rid of it, it doesn't get removed when it fires.
Serial.begin(38400);
}
void loop()
{
if (isrFired) {
isrFired=0;
Serial.println("Hello Whirled");
}
}
How are you generating a signal on pin 20?
If you're using a button, do you have a pullup/pulldown in place? If not, it can transition randomly, and fail to transition when you press the button (google up info on using buttons/switches with Arduino - you either need to set the pin as INPUT_PULLUP and have the button ground it, or use an external pull-up or pull-down (with the button connecting to ground or Vcc respectively)
Thank you!
On pin 20 I have this attached:
Are you sure that the voltage it's putting out is high enough to count as a 1? Check it with digitalRead()
That sensor puts out an analog voltage, and under the conditions you're using, that may not be high enough to be considered a 1.
See page 5 of the datasheet you linked for a suggestion on extending the output range all the way to the supply voltage with an external 100k resistor (I'll bet you could set the pin INPUT_PULLUP to get much the same effect) - or you can use it with a comparator like a 393 and a resistor divider or other source of reference voltage to set the threshold (look what they do on the second half of page 5).
if u are stuck using an analog output detector, for whatever reason, then why not use
analogRead function
Vin_sense1=analogRead(20);
and u can set any 'threshold value u want-- between 0 and 1023..to make a '1' or 'true'
make sure u set up the pin(20) in setup for analog input..