Recognize Arduino conneccted to the PC

Hi everyone, I have implemented a system to move a servo using a fingerprint. I have saved the fingers in to the database using this software:
https://startingelectronics.org/articles/GT-511C3-SDK-demo-arduino-uno/

and writing in to the arduino this code:

#include <SoftwareSerial.h>
// need a serial port to communicate with the GT-511C3
SoftwareSerial gtSerial(8, 7); // Arduino RX (GT TX), Arduino TX (GT RX)
// the Arduino TX pin needs a voltage divider, see wiring diagram at:
// http://startingelectronics.com/articles/GT-511C3-fingerprint-scanner-hardware/
void setup() {
  Serial.begin(9600);    // serial connection to processing app.
  gtSerial.begin(9600);  // for communicating with the GT-511C3
}
byte rx_byte = 0;        // stores received byte
void loop() {
  if (Serial.available()) {
    // get byte from processing app. and send to FPS
    rx_byte = Serial.read();
    gtSerial.write(rx_byte);
  }
  // check for a byte from the GT-511C3
  if (gtSerial.available()) {
    // get a byte from the FPS and send it to the processing app.
    rx_byte = gtSerial.read();
    Serial.write(rx_byte);
  }
}

And I have used this code to move the servo after fingerprint recognize the finger:

#include "FPS_GT511C3.h"
#include "SoftwareSerial.h" // usata dalla FPS_GT511C3.h
#include "Arduino.h"
#include "Servo.h"

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
FPS_GT511C3 fps(8, 7);

void setup() {
  // put your setup code here, to run once:

    Serial.begin(9600);
  delay(100);
  fps.Open();
  fps.SetLED(true);
    myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() 
{
  // put your main code here, to run repeatedly:
  if((fps.IsPressFinger()) == true)
  {

    fps.CaptureFinger (true);         // cattura l’impronta del dito sul sensore e la memorizza nella memoria del sensore stesso. Andrà poi confrontata con il DB per individuare l’id. Restituisce true se la cattura ha successo, false se il sensore è vuoto.
      if (fps.Verify1_1(0) == 0 || fps.Verify1_1(1) == 0 || fps.Verify1_1(2)== 0 || fps.Verify1_1(3)== 0 || fps.Verify1_1(4)== 0 || fps.Verify1_1(5)== 0 || fps.Verify1_1(6)== 0 || fps.Verify1_1(7)== 0 || fps.Verify1_1(8)== 0 || fps.Verify1_1(9)== 0 || fps.Verify1_1(10)== 0 || fps.Verify1_1(11)== 0 || fps.Verify1_1(12)== 0 || fps.Verify1_1(13)== 0 || fps.Verify1_1(14)== 0 || fps.Verify1_1(15)== 0 || fps.Verify1_1(16)== 0 || fps.Verify1_1(17)== 0 || fps.Verify1_1(18)== 0 || fps.Verify1_1(19)== 0)
      // int Verify1_1(int id): verifica se l’impronta catturata con la funzione CaptureFinger corrisponde all’impronta memorizzata nello slot identificato da id (da 0 a 199). Ritorna: 0 se corrispondono, 1 se la posizione non è valida (id < 0 o id > 199), 2 se l’id non in uso (vuoto), 3 se l’impronta non corrisponde a quella nello slot id
      {

        pos =0;
        myservo.write(pos);              // motore in posizione 0 gradi
        delay (2000);             
        }
   
             pos =75;
              myservo.write(pos);              // motore in posizione 75 gradi
              delay (500);         
  }
          
}

Now I want to use a single arduino software to do bough things. How can I recognize if the arduino is connected with USB or not? Are there some functions? Could I have some conflict?

Thanks in advice.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason just make for more scrolling when we're trying to read your code.

Which Arduino board are you using?

I have wrote this code but it is not working:

#include "FPS_GT511C3.h"
#include "SoftwareSerial.h" // usata dalla FPS_GT511C3.h
#include "Arduino.h"
#include "Servo.h"

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
SoftwareSerial gtSerial(8, 7); // Arduino RX (GT TX), Arduino TX (GT RX)
FPS_GT511C3 fps(8, 7);
byte rx_byte = 0;        // stores received byte

void setup() {
  // put your setup code here, to run once:


  Serial.begin(9600);    // serial connection to processing app.
  gtSerial.begin(9600);  // for communicating with the GT-511C3
  delay(100);
  fps.Open();
  fps.SetLED(true);
    myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() 
{

  if (Serial.available()) {
    // get byte from processing app. and send to FPS
    rx_byte = Serial.read();
    gtSerial.write(rx_byte);
  }
  // check for a byte from the GT-511C3
  if (gtSerial.available()) {
    // get a byte from the FPS and send it to the processing app.
    rx_byte = gtSerial.read();
    Serial.write(rx_byte);
  }
  else {
  
    delay (700);

  // put your main code here, to run repeatedly:
  if((fps.IsPressFinger()) == true)
  {

    fps.CaptureFinger (true);         // cattura l’impronta del dito sul sensore e la memorizza nella memoria del sensore stesso. Andrà poi confrontata con il DB per individuare l’id. Restituisce true se la cattura ha successo, false se il sensore è vuoto.
      if (fps.Verify1_1(0) == 0 || fps.Verify1_1(1) == 0 || fps.Verify1_1(2)== 0 || fps.Verify1_1(3)== 0 || fps.Verify1_1(4)== 0 || fps.Verify1_1(5)== 0 || fps.Verify1_1(6)== 0 || fps.Verify1_1(7)== 0 || fps.Verify1_1(8)== 0 || fps.Verify1_1(9)== 0 || fps.Verify1_1(10)== 0 || fps.Verify1_1(11)== 0 || fps.Verify1_1(12)== 0 || fps.Verify1_1(13)== 0 || fps.Verify1_1(14)== 0 || fps.Verify1_1(15)== 0 || fps.Verify1_1(16)== 0 || fps.Verify1_1(17)== 0 || fps.Verify1_1(18)== 0 || fps.Verify1_1(19)== 0)
      // int Verify1_1(int id): verifica se l’impronta catturata con la funzione CaptureFinger corrisponde all’impronta memorizzata nello slot identificato da id (da 0 a 199). Ritorna: 0 se corrispondono, 1 se la posizione non è valida (id < 0 o id > 199), 2 se l’id non in uso (vuoto), 3 se l’impronta non corrisponde a quella nello slot id
      {

        pos =0;
        myservo.write(pos);              // motore in posizione 0 gradi
        delay (2500);             
        }
   
             pos =75;
              myservo.write(pos);              // motore in posizione 75 gradi
              delay (800);         
  }
  }
          
}

What doesn’t work?

If I use the first part of the code, save a finger in to the fingerprint, it works, but when I try to use the fingerptint to move the servo, the fingerprint doesn't work. it is like it lost the power.

andrea080690: I have wrote this code but it is not working:

I suspect you have not got your IFs and your ELSEs organized properly. If you use the AutoFormat tool it will indent your program consistently and make it easier to see which parts depend on which.

Because you have not described how the code is intended to work I can't suggest any changes to the organization.

...R

Robin2:
I suspect you have not got your IFs and your ELSEs organized properly. If you use the AutoFormat tool it will indent your program consistently and make it easier to see which parts depend on which.

Because you have not described how the code is intended to work I can’t suggest any changes to the organization.

…R

Sorry, I have used now the AutoFormat:

#include "FPS_GT511C3.h"
#include "SoftwareSerial.h" // usata dalla FPS_GT511C3.h
#include "Arduino.h"
#include "Servo.h"

SoftwareSerial gtSerial(8, 7); // Arduino RX (GT TX), Arduino TX (GT RX)
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
byte rx_byte = 0;        // stores received byte
FPS_GT511C3 fps(8, 7);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  gtSerial.begin(9600);  // for communicating with the GT-511C3
  delay(100);
  fps.Open();
  fps.SetLED(true);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  //delay (700);
  while (!Serial) {

    if ((fps.IsPressFinger()) == true)
    {

      fps.CaptureFinger (true);         // cattura la impronta del dito sul sensore e la memorizza nella memoria del sensore stesso. Andrà poi confrontata con il DB per individuare l’id. Restituisce true se la cattura ha successo, false se il sensore è vuoto.
      if (fps.Verify1_1(0) == 0 || fps.Verify1_1(1) == 0 || fps.Verify1_1(2) == 0 || fps.Verify1_1(3) == 0 || fps.Verify1_1(4) == 0 || fps.Verify1_1(5) == 0 || fps.Verify1_1(6) == 0 || fps.Verify1_1(7) == 0 || fps.Verify1_1(8) == 0 || fps.Verify1_1(9) == 0 || fps.Verify1_1(10) == 0 || fps.Verify1_1(11) == 0 || fps.Verify1_1(12) == 0 || fps.Verify1_1(13) == 0 || fps.Verify1_1(14) == 0 || fps.Verify1_1(15) == 0 || fps.Verify1_1(16) == 0 || fps.Verify1_1(17) == 0 || fps.Verify1_1(18) == 0 || fps.Verify1_1(19) == 0)
        // int Verify1_1(int id): verifica se l’impronta catturata con la funzione CaptureFinger corrisponde all’impronta memorizzata nello slot identificato da id (da 0 a 199). Ritorna: 0 se corrispondono, 1 se la posizione non è valida (id < 0 o id > 199), 2 se l’id non in uso (vuoto), 3 se l’impronta non corrisponde a quella nello slot id
      {

        pos = 20;
        myservo.write(pos);              // motore in posizione 0 gradi
        delay (2000);
      }

      pos = 95;
      myservo.write(pos);              // motore in posizione 75 gradi
      delay (500);
    }
  }
  if (Serial.available()) {
    // get byte from processing app. and send to FPS
    rx_byte = Serial.read();
    gtSerial.write(rx_byte);
  }
  // check for a byte from the GT-511C3
  if (gtSerial.available()) {
    // get a byte from the FPS and send it to the processing app.
    rx_byte = gtSerial.read();
    Serial.write(rx_byte);
  }

}

Which board are you using? while(!Serial) is only useful on 32U4 based boards (e.g. Leonardo or Micro) and maybe on ARM based boards (not sure, never used one). On e.g. an Uno, it will always evaluate to true and hence the while loop will be skipped.

andrea080690: Sorry, I have used now the AutoFormat:

You have also made a lot of other changes and you did not comment on my suggestion so I have no idea how to compare the new program with the old one.

Tell us what the code in Reply #6 actually does and what it should do.

...R

In to the while there is the code wich should work when the arduino is not connected to the PC. Out of the while there is the code that should work when the arduino is connected to the PC, so it should work the fingerprint's software : https://startingelectronics.org/articles/GT-511C3-SDK-demo-arduino-uno/

The problem is that this software doesn't work, I can not connect this software using com3 ant 9600 baud. The other part is not working as well in any case, with usb connected or not.

pert: Which Arduino board are you using?

pert:

Arduino UNO

Then you need to read reply #7.

pert: Then you need to read reply #7.

Ok, so how can I recognize when the arduino is connected to the PC? Are there some functions?

You’ll know when the Arduino is connected to the PC when it shows up in Windows’ Device Manager.

An Uno has no means to know when it is connected to a PC unless the PC sends a message and the Uno is listening for that message. That would require a program to be running on the PC that can communicate with the Arduino - I suspect that making that into an automatic process on the PC would be complex.

...R

ieee488: You'll know when the Arduino is connected to the PC when it shows up in Windows' Device Manager.

I know it, but I want the arduino bheave in different way when it is connected and when it is not. How can I do it?

The closest you are is with the code in reply #2. However I think the approach is a little flawed. You can not detect if you're connected but you can safely assume that if serial data is available you are connected. If you don't receive serial data, you might be connected (but PC application has not send anything) or you might not be connected.

void loop()
{
  // variable to keep track if we're connected; initial value not connected
  static bool isConnected = false;

  if(Serial.available() > 0)
  {
    // indicate that we're connected
    isConnected = true;

    // read serial data and pass to GT-511C3
    ...
    ...
  }

  if(gtSerial.available() > 0)
  {
    // read data from GT-511C3 and pass to PC
    ...
    ...
  }

  if (isConnected == true)
  {
    // do something that is needed when connected
    ...
    ...
  }
  else
  {
    // do something else that is needed when not connected
    ...
    ...
  }

  // do the stuff that does not depend on connection
  ...
  ...

}

Once connected, it will think that it's connected forever (till reset). You can add a millis() based timeout mechanism to reset the connected status (isConnected) to not-connected if you don't receive anything in e.g. 5 seconds.

You can also first receive a complete command from the PC, next pass that to the GT-511C3, next collect a complete reply and once you have received that pass it back to the PC. You will need to study GT-511C3 datasheet to understand what is happening exactly to be able to implement.