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?
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.
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);
}
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 : 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