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 ![]()
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