volatile byte half_revolutions;
unsigned int rpm;
unsigned long timeold;
int led = D2;
int led2 = D7;
int sen1 = D4;
int bluebutton = D6;
int duration = 6000; // Contact Lens needs to be cleaned for min of 6 hours; for the sake of display, 6 secs = 6 hrs
int tstarted =-1;
volatile int state = 0;
bool detected = FALSE;
int lastState = 0;
int TS_DAY_MILLIS (1000 * 26);
// Actual code below; Contact lens need to be changed after 26 days; for the sake of display, 1 sec = 1 day
// #define TS_DAY_MILLIS (24 * 60 * 60 * 1000 * 26)
unsigned long lastSync = millis();
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(led2,OUTPUT);
pinMode(sen1, INPUT_PULLUP);
attachInterrupt(sen1, magnet_detect, CHANGE);
// attachInterrupt(D4, magnet_detect, CHANGE);//Initialize the intterrupt pin
half_revolutions = 0;
rpm = 0;
timeold = 0;
Particle.variable("x",detected);
digitalWrite (led2, LOW);
pinMode( bluebutton , INPUT_PULLUP); // sets blue button pin as input
}
void loop()
{
delay(100);
// if( lastState != state){
// Serial.println("detect change");
// Serial.println(tstarted);
// //detected = state;
// }
if(detected==true&&tstarted==-1)
{
tstarted=millis();
Serial.println("CLEANING STARTED");
Serial.println(tstarted);
}
if((tstarted !=-1) && (tstarted+duration < millis()) && state ==0) {
Serial.println("YOUR CONTACTS ARE CLEANED!");
Serial.println(tstarted);
lastState = state;
// detected = int(state);
digitalWrite(led, HIGH);
state = 1;
// if (half_revolutions >= 20) {
// rpm = 30*1000/(millis() - timeold)*half_revolution;s
// timeold = millis();
// half_revolutions = 0;
// //Serial.println(rpm,DEC);
// }
delay( 500 );
}
if (millis() - tstarted > TS_DAY_MILLIS) {
// Request time synchronization from the Particle Device Cloud
Particle.syncTime();
// lastSync = millis();
Serial.println("TIME FOR A NEW PAIR OF CONTACTS");
digitalWrite(led2, HIGH);
delay( 500 );
}
int bbuttonState = digitalRead( bluebutton );
if( bbuttonState == LOW && state == 1)
{
// tun the LED Off
digitalWrite (led, LOW);
}
}
void magnet_detect()//This function is called whenever a magnet/interrupt is detected
{
detected = TRUE;
// state = !state;
// half_revolutions++;
// Serial.println("detected");
// Serial.println(state);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .