interrupt on a analogPin (A0)

193 views
Skip to first unread message

Marcel Dunk

unread,
Apr 22, 2013, 3:35:45 PM4/22/13
to arduino-pincha...@googlegroups.com
Hi,

I have issues with an interupt on a analog pin (A0).
I using a knocksensor (piezo disc) connected to A0.

When continue reading the value this will show on the serial monitor:
no knock - 0 decimal value
light knock - 6 decimal value
hard knock - 20 decimal value

My problem is that i don't know when the state is falling/rising or change. Because it is analog. In digital term i get it.
But is >0 in analog values a change??

Can you give me a hand?
thanks.



my code:

#include <PinChangeInt.h>

// These two pins are connected for interrupts.
// Add more Pins at your leisure.
// For the Analog Input pins, you can use 14, 15, 16, etc.
// or you can use A0, A1, A2, etc.  The Arduino code comes with #define's
// for the Analog Input pins.
#define PIN1 A0

uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20]={0}; // 20 possible arduino pins
void quicfunc() {
  latest_interrupted_pin=PCintPort::arduinoPin;
  interrupt_count[latest_interrupted_pin]++;
      Serial.print(" Ja... interrupt ");
};

void setup() {
  pinMode(PIN1, INPUT); analogWrite(PIN1, LOW);
  PCintPort::attachInterrupt(PIN1, &quicfunc, FALLING);  // add more attachInterrupt code as required
  Serial.begin(9600);
  Serial.println("---------------------------------------");
}

uint8_t i;
void loop(){
    uint8_t count;
  Serial.print(".");
  delay(1000);
  for (i=0; i < 20; i++) {
    if (interrupt_count[i] != 0) {
      count=interrupt_count[i];
      interrupt_count[i]=0;
      Serial.print("Count for pin ");
      if (i < 14) {
        Serial.print("D");
        Serial.print(i, DEC);
      } else {
        Serial.print("A");
        Serial.print(i-14, DEC);
      }
      Serial.print(" is ");
      Serial.println(count, DEC);
    }
  }
}
  

Michael Schwager

unread,
Apr 22, 2013, 10:05:46 PM4/22/13
to arduino-pincha...@googlegroups.com
The Arduino's pins are multi-purpose. You can set up any pin to be input, output, or a pwm output. Some of them are used for SPI communications, others for I2C. The Atmel processor is very versatile that way.

The analog pins are called that because you can connect an internal Analog-to-Digital coverter to them, and read analog signals.  But they don't have to be analog- they can be digital pins as well.

That said, the PinChange Interrupts are useful for digital signals only. They won't work with analog signals. So you can't do what you're trying to do. You need a Schmitt trigger, or a number of them, to take inputs and trigger once your signal reaches a certain value. 

Either that, or continually scan an analog input.

I hope this helps. I have not tried this sort of thing, but basically know that it's what you have to do. Good luck.
Reply all
Reply to author
Forward
0 new messages