using sensor interrupt in arduino

Hello Guys!

I am new to this forum and subject, however, I would like to solve a problem with regard to reading and using the interrupts!

I am using the Arduino IDE and UNO to read the VNCL sensor, namely to turn on the LED. So far so good, I can read the proximity via i2C, and turn on the LED, but I am having problems to turn it off the :confused:

The VCNL library (GitHub - Zanduino/VCNL4010: Library to support the VCNL4010 proximity and ambient light sensor) and proximity reading works well, however, I am not sure, if I am doing the ISR ok, because the led wont turn off when I reach certain (high) threshold.

I guess I am not reading/using the interrupts from the sensor properly...

The code:

#include <Wire.h> // I2C
#include <VCNL4010.h> // the library! (not master) https://github.com/SV-Zanshin/VCNL4010


VCNL4010 Sensor; // This is my sensor

const byte ledPinB = 11; // Blue LED is connected

const uint32_t serial_speed = 9600; //serial

uint16_t intPin = 2; //the Arduino interrupt input pin, either 2 or 3
uint8_t interruptFlags;

// variables that are used across main and ISR should be volatile
volatile uint16_t offSet;
volatile uint16_t lowT;
volatile uint16_t hiT;

// Interrupt Service Routine (ISR)
void pin_ISR() {
 if (digitalRead(intPin) == HIGH) {  // while or if
   digitalWrite(ledPinB, HIGH);
   //int t = (offSet * 7) / 10;
   //setInterrupt(2, t, 65535);
 }
 else {
   digitalWrite(ledPinB, LOW);
   cli();
   //Sensor.clearInterrupt(B0011);
 }
}

void setup() {
 //Start serial
 Serial.begin(serial_speed);
 delay(20);

 // update the led on its pin
 pinMode(ledPinB, OUTPUT);
 
 //Interrupt service routine (ISR)
 // initialize "the interrupt" pin as an input:
 pinMode(intPin, INPUT);
 // start the sensor and initialise its values
 startVCNL();
 // clear all interrupts, apparently it is important to clear the interrupts, however, I am not sure, how to make this going...
 //clearInterrupts();
 // SET INTERRUPT
 // Get the proximity reading
 offSet = Sensor.getProximity();
 //lowT = Sensor.getProximity() *  9 / 10; // Low value is 90% of first reading
 hiT = Sensor.getProximity() * 19 / 10; // High value is 190% of first value

 setInterrupt(2, lowT, hiT);
 
 attachInterrupt (0, pin_ISR, CHANGE);  // attach interrupt handler

}

void loop() {//nothing}

//startVCNL//

void startVCNL(){
 
 Serial.println(F("Starting VCNL4010 "));                                      
 while (!Sensor.begin()) {                                                   // Loop until sensor found          //
   Serial.println(F("Error, unable to find or identify VCNL4010."));         // Show error message               //
   delay(2000);                                                              // Wait 2 seconds before retrying   //
 } // of if-then we can't initialize or find the device                      //                                  //

 //initialize the sensor Proximity power and sampling
 Sensor.setLEDmA(200);                                                       // Boost power to Proximity sensor  //
 Sensor.setProximityHz(128);                                                 // Sample 128x per second           //
 Sensor.setProximityFreq(2);                                                 // Set frequency to 1.5625MHz, also 3 other available 
 
 Serial.println(F("VCNL4010 initialized.\n\n"));                             
}

// Trigger an interrupt when the proximity reads below or above ceratin threshold value
void setInterrupt(int count, int lowT, int hiT){
 Sensor.setInterrupt(
 count,      //Count  (default 1) The number of events needed to trigger an interrupt
 false,      //Proximity Ready (default false) Whether or not to trigger an interrupt when a proximity reading is ready
 false,      //ALS Ready (default false) Whether or not to trigger an interrupt when an ambient light sensor reading is ready
 true,       //Proximity Threshold (default false) 
 false,      //ALS Threshold (default false) 
 lowT,       //Low Threshold value (default 0) 
 hiT        //High Threshold value  (default 65536)
 ); 
}

Any help is much appreciated!

MU

You can help us to help you if

a) Read the stickies at the top of the page.
b) Post ALL your code between the code tags </>
c) Post ALL of the error message, again using code tags

Fof

Thank you!

I guess the post is now in order?

The code compiles ok, however, it does not work, namely the led does not turn back ON.

The code starts with the led ON, then the proximity (sensor) is used and turns OFF the led, which is alright, because by default the interrupt is HIGH, I guess this can be easily turned around with ! ... anyway, the ISR does not turn back ON the led when the high threshold is reached...

Hope this helps.

MU

IamFof:
You can help us to help you if

a) Read the stickies at the top of the page.
b) Post ALL your code between the code tags </>
c) Post ALL of the error message, again using code tags

Fof

What is the purpose of the cli()? Interrupt flags are cleared automatically on return from an ISR.

aarg:
What is the purpose of the cli()? Interrupt flags are cleared automatically on return from an ISR.

Thank you, I guess I do not have to worry then about

"The interrupt settings remain until cleared using the clearInterrupt() function."

?

MU

mu234:
Thank you, I guess I do not have to worry then about

"The interrupt settings remain until cleared using the clearInterrupt() function."

?

MU

Who are you quoting? What is the context?

I think cli() will prevent future execution of the ISR, since it disables interrupts. Is that what you wanted?

The quote is from the sensor library ( getInterrupt() · Zanduino/VCNL4010 Wiki · GitHub ).

Thanks for explaining the cli(), no, I did not want the ISR to stop, actually it has to be continuous loop, once the led is ON, it is turned OFF and vice versa. The sensor is used as a toggle switch...

MU

(Replying to a dead thread - but I should clarify the last comment)

That quote above refers to the interrupt MFP pin of the VCNL4010 and not Arduino interrupts.