ANALOG_COMP_vect

I am having a laser sensor, read by the arduino. If the value from the sensor is within a specific range, I want the system to indicate.

I'm not sure that I understand how I should configure the ANALOG_COMP_vect and how it is working.

Here is the code I'm using.

const int analogInPin = A0;  // Analog input pin that the sensor is attached to
const int analogOutPin = 9; // Analog output pin that the indicator is attached to

int sensorValue = 0;        // value read from the sensor
int outputValue = 0;        // value output to the PWM (analog out)

int rangemin = 200 //Minimum value the system will start indicate
int rangemax = 400 //Maximum value the system will start indicate
void setup() 
{

ACSR = 
 (0<<ACD) |   // Analog Comparator: Enabled
 (0<<ACBG) |   // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
 (0<<ACO) |   // Analog Comparator Output: Off
 (1<<ACI) |   // Analog Comparator Interrupt Flag: Clear Pending Interrupt
 (1<<ACIE) |   // Analog Comparator Interrupt: Enabled
 (0<<ACIC) |   // Analog Comparator Input Capture: Disabled
 (1<<ACIS1) | (1<ACIS0);   // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge
}

void loop() 
{

    if(analogRead(analogInPin)>rangemin) && (analogRead(analogInPin)<rangemax){  // the terms for when to light the indicatior 
    outputValue = 255;  //on
  }
  else
  {
    outputValue = 0; //off
  }
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

}
 
 
 ISR(ANALOG_COMP_vect )
{
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            

}

I know that some of the code is just ad-hoc made. But most important, how does the ANALOG_COMP_vect works here, e.g. when will it read the sensor value?

Thankful for every help I can get.

The whole sketch makes no sense to me.

You have an ISR that reads analogInPin and attempts to save it in sensorValue.

I say "attempts" because you haven't declared sensorValue as volatile, so no other part of your sketch will be able to see the result of this. But this issue is irrelevant as no other part of your sketch actually looks at sensorValue, so therefore the whole ISR is totally redundant.

You are right. I guess the program just read the analogue value continuously without using the ISR.

Is it possible to put a interrupt if the value for the sensor is within the wanted range?

Is it possible to put a interrupt if the value for the sensor is within the wanted range?

Are you asking if it is possible for an interrupt to be triggered only when the value returned by the sensor is in a certain range ?

Why do you want to use an interrupt anyway ?

Yeah i know it's stupid and impossible. I am bit bad of expressing what I mean i've notice.

I just don't want the arduino to continuously read the input value. What would be the best way to solve this? The input value is between 0-1023.

This is more correct i guess for just reading:

const int analogInPin = A0;  // Analog input pin that the sensor is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the sensor
int outputValue = 0;        // value output to the PWM (analog out)

int trainrangemin = 200 //Minimum value the system will start indicate
int trainrangemax = 400 //Maximum value the system will start indicate
void setup() 
{
  // initialize serial communications at 9600 bpsm for serial monitor:
  Serial.begin(9600); 

void loop() 
{

    if(analogRead(analogInPin)>trainrangemin) && (analogRead(analogInPin)<trainrangemax){  // the terms for when to light the indicator 
    outputValue = 255;  //on
  }
  else
  {
    outputValue = 0; //off
  }
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

}

I just don't want the arduino to continuously read the input value

Why not ?

If i want to add function. Guess I could always add an extra arduino.

The 'art' of efficient programming would be to check once in the loop() the analog value and if applicable process it in a function. If in the valid range you would have all the time-time-of-the world to do other things. Unless the analog read is so crittical you might ask yourself if you are using the right solution ::slight_smile: And if you really want to have it generate an interrupt, use a small processor (i.e. an attiny25/45/85) have it constantly do the analogRead and validate it. If applicable, generate an interrupt pulse and connect this pin to either int0 or int1. Then you would have a dual core solution with real parallel processing :grin:

Raun:
If i want to add function. Guess I could always add an extra arduino.

WHAT?

But what you have so far is using such a tiny part of the arduino's full potential.

I know. But the arduino can't be occupied with another task and miss the output from the sensor. Maybe I am overdoing it.

the arduino can't be occupied with another task and miss the output from the sensor

How long do you anticipate the sensor being in the range of values that you are interested in ?