Impossible to know what go wrong with the code ![]()
Hours to check and search didn't help.
Have someone an idea ??
volatile byte pcint_pins = B00000000; // variable for current port J bits
volatile byte bitsToCheck = B00000000; // variable for isolate bits to check
volatile byte pcint_pinsLast = B00000000; // variable for previous port J bits
// Variables utilisées pour les capteurs de lumières
volatile unsigned long cnt1 = 0;
volatile unsigned long cnt2 = 0;
unsigned long oldcnt1 = 0;
unsigned long oldcnt2 = 0;
unsigned long t1 = 0;
unsigned long t2 = 0;
unsigned long last;
void setup()
{
Serial.begin(115200);
Serial.println("test 2 TSL235R on pin A8 and A9");
InitialiseIO();
InitialiseInterrupt();
}
void InitialiseIO(){
pinMode(A8,INPUT_PULLUP); // Pin A8 is input to which a switch is connected
pinMode(A9,INPUT_PULLUP); // Pin A9 is input to which a switch is connected
}
void InitialiseInterrupt(){
cli(); // switch interrupts off while messing with their settings
PCICR |= (1 << PCIE2); // enable PCINT2
PCMSK2 |= (1 << PCINT16); // mask for bit0 of port K (A8)
PCMSK2 |= (1 << PCINT17); // mask for bit1 of port K (A9)
sei(); // turn interrupts back on
}
ISR(PCINT2_vect) { // Interrupt service routine
bitsToCheck = PINK & B00000011; // To check A8 & A9 bits
pcint_pins = bitsToCheck ^ (pcint_pinsLast & B00000011); // XOR to check what bit had changed since last past
pcint_pinsLast = pcint_pins; // Save for next time
if (pcint_pins == B00000001) cnt1++; // Interrupt on sensor pin A8
if (pcint_pins == B00000010) cnt2++; // Interruut on sensor pin A9
if (pcint_pins == B00000011) { cnt1++; cnt2++;} // Interrupt on both sensor pins
}
void loop()
{
if (millis() - last >= 1000) // Calculate FREQ every seconds
{
last = millis();
t1 = cnt1;
unsigned long hz1 = t1 - oldcnt1;
Serial.print("FREQ1: ");
Serial.print(hz1);
Serial.print("\t = ");
Serial.print((hz1+50)/100); // +50 == rounding last digit
Serial.println(" mW/m2");
oldcnt1 = t1;
t2 = cnt2;
unsigned hz2 = t2 - oldcnt2;
Serial.print("FREQ2: ");
Serial.print(hz2);
Serial.print("\t = ");
Serial.print((hz2+50)/100); // +50 == rounding last digit
Serial.println(" mW/m2");
oldcnt2 = t2;
}
}