How to use Interrupts for analog pins? Help needed.

Hi all,

I would like to know how can i use interrupt when a data is being fed into the AnalogRead?
I have written some code but it doesnt even go to the void loop function.
Instead, it went straight to the interrupt function. I have attached the result from the Serial monitor below.

Following is my code:

//set the baudrate
int baudRate = 115200;

//set the LEDs
int greenLED = 3;
int blueLED = 4;

//defining variables
int pdlimit = 1020; //1032 is the value of 5V
int interruptSignal = A1;

//defining variables used by the interrupt function
volatile int flag; //to see if the PD receiver '1' or '0'
volatile int i;
volatile int delayTime = 500; //delay time for LED to blink
volatile int j = 0; //receiver counter for the bits
volatile int pd = A0; //setting the input pin for analog signal
volatile int value = 0; //initiate the store variable of PD to be zero
volatile char outChar; //output data back

void setup() {
// put your setup code here, to run once:
pinMode(greenLED,OUTPUT);
pinMode(pd,INPUT); //set the pin to input to collect data from photodiode
Serial.begin(baudRate); //set the rate for serial monitor
startLED();

//Attach the interrupt
attachInterrupt(interruptSignal, startreceiving, RISING);
//Falling edge because Photodiode is 5v at 'off state', thus
//when PD detects light, voltage will drop.
}

void loop() {
// put your main code here, to run repeatedly:
j = 0; //reset the counter back to 0
clearchar(); //clear the storage variable
Serial.println("Program Start");

int connectionEstablish = analogRead(interruptSignal);
}

//end of main program
//sub function

void startreceiving() //the interrupt function
{
Serial.println("interrupt start");

for (int i =0; i<8; i++)
{
Serial.println("for loop start");
pdvalue(); //get value from photodiode
receiver(); //decode the data and output
}

Serial.print("\n");
Serial.print("The character entered is ");
Serial.println(outChar);
}

test.png

Why is 5 V 1032?

And you should not put serial communications in an interrupt handler.

Interesting trick

int baudRate = 115200;

EDIT: Had a better look and several other problems...
Your trying to use A1 as external (hardware) interrupt pin.
Your attempting to print stuff while in the interrupt code and it's calling two other functions that are not included so cannot be checked for errors.

There's reference on attachInterrupt, saying which pins are valid for which processors.
A1 is not in that table.

There are only certain pins you can use attachInterrupt with.

Not only that, the interrupt number is what you specify, not the pin number.

Hi Riva,

Im printing stuff to see which part of the code the program in going into.

Riva:
Interesting trick

int baudRate = 115200;

EDIT: Had a better look and several other problems...
Your trying to use A1 as external (hardware) interrupt pin.
Your attempting to print stuff while in the interrupt code and it's calling two other functions that are not included so cannot be checked for errors.

Hi Delta_G,

I understand that A1 is not in the pins i can use. but i am kinda confuse about how the interrupt knows which pins i want to monitor when i didnt even specify?

I have look at the reference you provided in the link above:

In this example, the attachInterrupt is 0. but which pin is that? How does it monitor?

Example

int pin = 13;
volatile int state = LOW;

void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);
}

void loop()
{
digitalWrite(pin, state);
}

void blink()
{
state = !state;
}

Delta_G:

int interruptSignal = A1;
attachInterrupt(interruptSignal, startreceiving, RISING);

attachInterrupt() - Arduino Reference

Try again. There are only certain pins you can use attachInterrupt with.

What are you trying to accomplish?

Hi Delta_G,

I am trying to send a signal via the use of LED light to signified the start of a transmission. With a photodiode to receive the indication(which is being read by the analog input pin A1), then i would start receiving data using a interrupt to check for that indication.

Hi KeithRB,

Because im am supplying a 5V to the Photodiode, thus if it detect light, current will be able to pass therefore resulting in a voltage drop on a resistor. Else the analog input from the photodiode will be 1023, which indicating a 5V. Its a analog signal.

In this example, the attachInterrupt is 0. but which pin is that? How does it monitor?

Reference

Dyllantzy:
Hi KeithRB,

Because im am supplying a 5V to the Photodiode, thus if it detect light, current will be able to pass therefore resulting in a voltage drop on a resistor. Else the analog input from the photodiode will be 1023, which indicating a 5V. Its a analog signal.

Exactly, the comment says 1032.

Sorry about that, I overlooked that part. However it state for digital pin. I have tried using pin 2 or 3. Set the attachInterrupt to 0. But it still doesnt work.

How exactly can i use interrupt for a analog input signal? I think i am doing it wrong here. How do i monitor that analog signal when there is a change?

Hi Delta_G,

Dyllantzy:
I am trying to send a signal via the use of LED light to signified the start of a transmission. With a photodiode to receive the indication(which is being read by the analog input pin A1), then i would start receiving data using a interrupt to check for that indication.

Thanks for taking your time to read and helping. Really appreciate it. I would not want to keep polling for that pin as i want the microprocessor to be doing other stuff.

I am curious about that interrupt you mention that i can get that trigger when a conversion on the ADC completes. Can i know how to access that interrupt? It might just solve all my problems!

Delta_G:
Even if you used the interrupt I mentioned, you'd be taking the time somewhere to read the pin. It wouldn't make things any faster.

Not necessarily true, since it takes about 100uSec to do a conversion, and using analogRead the processor will be sitting doing nothing during that time.

That said, I agree is it unlikely this will cause any performance issues in a well-written application. I do lots of analog reads in an application that is doing real-time control of multiple DC servos with high-count encoders, and it works fine using analogRead(). There are few applications that will saturate these processors if the code is well-written.

Regards,
Ray L.

Delta_G:
I would give an example, but I can't think of one. But if there were a case where the reads had to come at exactly some specific interval, then you might use an interrupt. Otherwise it is just headache over nothing.

Sorry guys for the late reply, had a really busy few days. Thanks for the reply!

Hi Delta_G,

Yes, i am require to read the data at a specific point of time. Which is why i am having headaches over this. If only i can trigger an interrupt when the AnalogRead reads a data, then my problem is all solved!
Like i mention that i actually need to decode a data only when the analogRead detect a data that is coming in, i do not need it go make it faster.

Hi Ray L,

Maybe you could provide me with some advice?

Like i mention that i actually need to decode a data only when the analogRead detect a data that is coming in,

This doesn't make any sense - analogue data is always "coming in".

If only i can trigger an interrupt when the AnalogRead reads a data, then my problem is all solved

Have you read about the EOC (end-of-conversion) interrupt?

AWOL:
This doesn't make any sense - analogue data is always "coming in".
Have you read about the EOC (end-of-conversion) interrupt?

Hi AWOL,

Oh! That is an interrupt when a ADC completes its conversion right? I am using an Arduino due ATSAM3X8C. The datasheet states that when a conversion is completed, the resulting 12-bit digital value is stored in the channel data register(ADC_CDRx) and ADC Last Converted Data Register.

The channel EOC bit in the Status Register (ADC_SR) is set and the DRDY is set. In the case of
a connected PDC channel, DRDY rising triggers a data transfer request. In any case, either
EOC and DRDY can trigger an interrupt.

But how can i access this Register in arduino? Is the the interrupt register? Do i need to download any library to use? like the AVR etc?

I am using an Arduino due ATSAM3X8C

It's taken 18 responses to prise that gem out of you?

AWOL:
It's taken 18 responses to prise that gem out of you?

Sorry about that, it slipped my mind as i was too fixated on how i could get it work. And when i was reading about the interrupts, it occur to me that different board would have different configurations. I apologize.

I am also looking at the possibility of using interrupts on the Analog pins.
This is because I am going to use the MUX Shield to read 48 Rotary Encoders and that shield uses A0 - A2.
The documentation for the Due says ALL DIGITAL PINS are available for interrupts.
Since the Analog pins can be set to Digital Pins using pinMode....
Thus, pinMode(A0, INPUT);
Now its a digital pin with interrupts no?

As far as I know only 2 pins have dedicated interrupts, all others share the same interrupt
https://playground.arduino.cc/Code/Interrupts

zoomx:
As far as I know only 2 pins have dedicated interrupts, all others share the same interrupt
Arduino Playground - HomePage

That article might not be applicable to a Due.