Good point! I just set count to "0" in my condition "if". It works well, when I attach smartphone to NFC tag, Arduino is triggered - sensor messures and led is lighted up.
But another problem occured. When I leave smartphone on the tag for longer time (Pin 2 stays LOW even after condition is met - Arduino is triggered) and then I take it from the NFC tag and return it back. Arduino is not triggered again. I don't know why shouldn't be triggered.
My code is following:
#include <Wire.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "SparkFunISL29125.h"
int ledPin = 13; // LED connected to digital pin 13
int count = 0;
void wakeUpNow() // here the interrupt is handled after wakeup
{
sleep_disable();
detachInterrupt (0);
}
SFE_ISL29125 RGB_sensor;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(2,INPUT_PULLUP);
Serial.begin(9600);
Wire.begin();
// Inicialization of sensor
if (RGB_sensor.init())
{
Serial.println("Sensor Initialization Successful\n\r");
}
}
void sleepNow()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable (); // enables the sleep bit in the mcucr register
attachInterrupt (0,wakeUpNow, LOW); // wake up on low level on D2
sleep_mode (); // here the device is actually put to sleep!!
} // end of sleepNow
void loop()
{
// Light up led diode and read from RGB sensor
detachInterrupt (0);
digitalWrite(ledPin, HIGH);
delay (500);
unsigned int red = RGB_sensor.readRed();
Serial.print("Red: "); Serial.println(red, DEC);
Serial.println();
// Write to NFC tag EEPROM memory
Wire.beginTransmission(25);
Wire.write(0x40);
Wire.write(0x55);
Wire.write(0x00);
Wire.write(red);
Wire.write(red>>8);
Wire.write(0x00);
Wire.write(0x00);
Wire.endTransmission();
count++;
delay(2000);
if (count==2){
digitalWrite(ledPin, LOW);
digitalWrite(2, HIGH);
count = 0;
delay(100);
sleepNow();
}
}