Breathing green [Tried Almost everything]


#1

Hey guys im having a bit of an issue here,
Im trying to read a fingerprint sensor and have it activate a servo thats attached to it after the finger is verified. I can call all the other functions fine without an issue but whenever i call the "unlock command, the device hums green and stays there until i force a reset. Ive tried the Particle.process() sprinkle from here [ https://docs.particle.io/tutorials/device-os/led/photon/] but that dosen’t work. The SYSTEM_THREAD(ENABLED) only makes it stay cyan but i cant call any function. any help please.

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_Fingerprint.h>


#include <Adafruit_DHT_Particle.h>


#define DHTPIN D2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 22 (AM2302)

int motorPin = D1; //engine pin
const int indicator = D7;  //indicator  pin

DHT dht(DHTPIN, DHTTYPE);


const int frontlight = D4; //front light
const int backlight = A2; //back light

//fingerprint 
Servo myservo;  // create servo object to control a servo
int angle = 180;    // variable to store the servo position
int getFingerprintIDez(); 

int i=0; 

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial1);



void setup() {
  Serial.begin(9600);
  dht.begin();
  delay(2000);
  finger.begin(57600);
  
  Particle.function("Doorlock", open_close);
  Particle.function("Engine-temp", Engine_temp); //car engine temp
  Particle.function("Engine-ON-OFF", Engine_ONOFF); //turn engine on
  Particle.function("Front-Lights", FrontLights);
  Particle.function("Back-Lights", Backlights);
  
  pinMode(motorPin, OUTPUT);
  pinMode(indicator, OUTPUT);
  pinMode(frontlight, OUTPUT);
  pinMode(backlight, OUTPUT);



     //servo
  myservo.attach(D0);   // attach the servo on the D0 pin to the servo object
  


}


void loop() {

    getFingerprintIDez();

  // Read temperature as Celsius
  float t = dht.getTempCelcius();
  // Read temperature as Farenheit
  float f = dht.getTempFarenheit();

  // Check if any reads failed and exit early (to try again).
  if (isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  //publish temp just to be sure
  Particle.publish("readings", String::format("{ \"Temp(°F)\": %4.2f, \"Temp(°C)\": %4.2f}", f, t ));
  delay(60000); // read every 1 min




}
//temp  reader
int Engine_temp(String state) {
  if (state == "temp" || "") {
    float f = dht.getTempFarenheit();
    return (f);
  }

  else {
    return (-1);
  }
}
// engine controls
int Engine_ONOFF(String state) {
  if (state == "Car On") {
    digitalWrite(indicator, HIGH);
    analogWrite(motorPin, 150);

    return (1);
  }
  else if (state == "Car Off") {
    analogWrite(motorPin, 0);
    digitalWrite(indicator, LOW);
    return (0);
  }
  else {
    return (-1);
  }
}
//front lights
int  FrontLights(String ledCommand) {
  String convertedCommand = ledCommand.toUpperCase();

  if (convertedCommand == "ON FRONT") {
    digitalWrite(frontlight, HIGH);
    return (1);
  } else if (convertedCommand == "OFF FRONT") {
    digitalWrite(frontlight, LOW);
    return (0);
  }
  else {
    return (-1);
  }
}
//back ights
int  Backlights(String ledCommands) {
  String convertedCommand = ledCommands.toUpperCase();

  if (convertedCommand == "ON BACK") {
    digitalWrite(backlight, HIGH);
    return (1);
  } else if (convertedCommand == "OFF BACK") {
    digitalWrite(backlight, LOW);
    return (0);
  }
  else {
    return (-1);
  }

}

void open_close_door()
{
  Serial.print("Finger verified, Door open");
  Serial.println();
  myservo.attach(D0);
  for (int i = 0; i < angle; i++)
  {
    myservo.write(i);
    delay(5);
  }

  delay(4000);

  for (int i = (angle - 1); i >= 0; i--)
  {
    myservo.write(i);
    delay(5);
  }
  myservo.detach();


}



// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez()
{
 Particle.process();
    if (!finger.verifyPassword())
    {
        Serial.println("Did not find fingerprint sensor :(");
        return -1;
    }

    uint8_t p = finger.getImage();
    if (p != FINGERPRINT_OK)
    {
        return -1;
    }

    p = finger.image2Tz();
    if (p != FINGERPRINT_OK)
    {
        return -1;
    }

    p = finger.fingerFastSearch();
    if (p != FINGERPRINT_OK)
    {
        open_close_door();
        return 1;
    }

#if __Debug
    Serial.print("Found ID #");
    Serial.print(finger.fingerID);
    Serial.print(" with confidence of ");
    Serial.println(finger.confidence);
#endif

    return finger.fingerID;

}

int open_close(String command)
{
    Particle.process();
 if  (command == "unlock")
   
    while(i<=7){
	if(getFingerprintIDez()>=0)
    {
        return(1);
    }
    else if (command == "") {
        return (-1);
    }
   i--; 
}

}


#2

I fixed formatting on your open_close function so hopefully you can follow it a little more easily. I didn’t add/remove any braces or move anything around. Just fixed the spacing.

The real problem is that you have a global int i=0 that is being used in this function. So if your fingerprint isn’t OK it will loop a couple billion times as it goes negative (<= 7) and eventually underflows back to a really big positive number. I expect that feels a lot like a lockup.

int open_close(String command)
{
    Particle.process();
	if(command == "unlock")
		while(i<=7)
		{
			if(getFingerprintIDez()>=0)
			{
				return(1);
			}
			else if (command == "")
			{
				return (-1);
			}
			i--; 
		}
}