Arduino + nike sensor = Car Keyless entry

Hello
I'm using a Sparkfun Pro-Micro Arduino with a nike reader breakout board from the same company.
I'm trying to read the sensor and if the correct one is heard, unlock and unfold the mirrors on my car, when it stops hearing the sensor it locks the car and folds the mirrors, by making the arduino simulate a button press on the original remote.

I have some code here, wich is for IDE 1.0.1, all my comments are present, for easier understanding and, the hardware part is also done (relays, transistors, etc.) but not connected yet, I'm using some LEDs to check what the pins are doing for now.

 //Init strings for nike reader.
                                           
int init1[] = {0xFF,0x55,0x04,0x09,0x07,0x00,0x25,0xC7};
int init2[] = {0xFF,0x55,0x02,0x09,0x05,0xF0};
int frame[32];


// Internal software variables for loops, counts, etc.
int i;                                     // General variable to send init strings.
int locked = 1;                            // Variable to keep track if car is locked (true) or, unlocked (false).
unsigned long unlocked_time = 0;           // Variable to keep track of the time that car as been unlocked.
unsigned long previous_unlocked_time = 0;  // Variable to keep track of the time car was unlocked.


//Pin assignments
int ignition = 2;                          // Pin to sense ignition input
int lock = 3;                              // Pin to send lock signal to key remote.
int unlock = 4;                            // Pin to send unlock signal to key remote.
int fold = 5;                              // Pin to trigger mirror folding motor.
int unfold = 6;                            // Pin to trigger mirror unfold motor.

void setup()  
{
  Serial1.begin(57600);
  
  pinMode(ignition, INPUT);
  pinMode(lock, OUTPUT);
  pinMode(unlock, OUTPUT);
  pinMode(fold, OUTPUT);
  pinMode(unfold, OUTPUT);
  digitalWrite(lock, LOW);
  digitalWrite(unlock, LOW);
  digitalWrite(fold, LOW);
  digitalWrite(unfold, LOW);

  for (int i=0;i<9;i++)                       // Send 1st init string.
  {
    Serial1.write(init1[i]);
  }
  delay(1000);                                // Wait 1 second before sending 2nd init string (during this time there is the 1st response string coming back).
  
  for (int i=0;i<7;i++)                       // Send 2nd init string
  {
    Serial1.write(init2[i]);
  }
  while (Serial1.read() != 0xC8)              // Wait until it hears the last character (C8) from the reader response. 
  {;}
  attachInterrupt(1, ignition_ON, RISING);    // When the ignition is ON trigger a funtion that does nothing. ( No need to read sensors or lock car, etc.)


void loop()
{
  if (Serial1.available() > 0)                 // If there is something in the Serial port it means the reader "heard" a sensor
  {
    for(int i = 0 ; i < 34 ; i++)              // Fill "frame" variable with entire contents from sensor.
    {
      frame[i] = Serial1.read();
      delay(1);                                // Delay needed, not exactly sure why, but it keeps the reading accuracy.
    }
    if (frame[7] == 0xB3 && frame[8] == 0xC9 && frame[9] == 0x4D && frame[10] == 0x2F)    // Check if sensor heard is the correct one.
    {
      unlocked_time = millis() - previous_unlocked_time;                                  // "unlocked_time" variable gets the time since the car was last unlocked.
      if (locked == 1 || locked == 0 && unlocked_time >= 35000)                           // Test to see if the car is locked (direct unlock) OR is unlocked and the time since that last unlock is 35 or more seconds (auto relocking override while there is a sensor near by).
      {;
        unlock_and_unfold();                                                              // Unlock function with mirror unfold
      }
    }
  }
  if (locked == 0 && Serial1.peek() == -1) // If the car is unlocked and no sensor can be heard, then start locking process.
  {
    delay(5000);                           // Wait 5 seconds.
    if (Serial1.peek() == -1)              // If you can't hear any sensor, continue the locking process. (-1 is the value the reader sends when is not hearing any sensor)
    {
      delay (5000);                        // Wait another 5 seconds.
      if (Serial1.peek() == -1)            // If you still can't hear any sensor, lock the car. (-1 is the value the reader sends when is not hearing any sensor)
      {
        lock_and_fold();                   // Lock function, with mirror fold.
      }
    }
  } 
}

void ignition_ON()                         // Function to come when the ignition is on, if yes there is no need to do anything.
{
  while (digitalRead(ignition) == 1)       // Stay here until ignition is turned off.
  {
    locked = 0;
    previous_unlocked_time = millis();
  }
}                                          // By the time we leave here, the car is unlocked and the time needed to unlock again is reset.

void lock_and_fold()
{
  locked = 1;                  // Set variable as locked
  digitalWrite(lock, HIGH);    // Bringing this pin high makes the remote send lock code.
  digitalWrite(fold, HIGH);    // Bringing this pin high makes the relay switch 12V to the folding motor.
  delay(100);                  // This delay replicate the time a humam finger is usually on the button.
  digitalWrite(lock, LOW);     // Release the button on the remote.
  delay(4900);                 // An additional 4.9 seconds (5 seconds total) to give time to the motor to fully fold the mirror.
  digitalWrite(fold, LOW);     // Stop the motor, mirror folded.
}

void unlock_and_unfold()
{
  previous_unlocked_time = millis();
  locked = 0;                  // Set variable as unlocked.
  digitalWrite(unlock, HIGH);  // Bringing this pin high makes the fob send unlock code.
  digitalWrite(unfold, HIGH);  // Bringing this pin high makes the relay switch 12V to the unfolding motor.
  delay(100);                  // This delay replicate the time a humam finger is usually on the button.
  digitalWrite(unlock, LOW);   // Release the button.
  delay(4900);                 // An additional 4.9 seconds (5 seconds total) to give time to the motor to fully unfold the mirror.
  digitalWrite(unfold, LOW);   // Stop the motor, mirror unfolded.
}

Now my problem is, I had this working until I tried to disconnect from the PC and go standalone, wich is to say disconnect the USB cable and provide the arduino power from a regulator.
It stopped working, I then got it to work again (with no code change). And now is a case of hit and miss, sometimes it works with the PC sometimes not, sometimes it works standalone, sometimes not. All I'm doing now is reprogramm the same code over and over again disabling only the line where the arduino waits for "C8" to appear from the reader. Again, sometimes it works, sometimes it doesn't
The arduino board is fine, I can program other stuff in it and all pins work, serial comms are OK (both serial for PC and serial1 for hardware serial).
The nike reader and sensor are fine, I can create a simple program to read it, and it works fine.

Can anyone provide some help?
I'm relatively new to the arduino, and, my code will probably show this, but since it was working fine in testing I'm really lost here.
Thanks for your time.
Best Regards

  if (Serial1.available() > 0)                 // If there is something in the Serial port it means the reader "heard" a sensor
  {
    for(int i = 0 ; i < 34 ; i++)              // Fill "frame" variable with entire contents from sensor.
    {
      frame[i] = Serial1.read();
      delay(1);

If there is one byte, read all 34 of them. How's that working for you?

The Pro Micro has only one hardware serial port. Why are you using Serial1 to reference it?

Hi, from checking the program with some serial comms to the PC, to see where it is, when it doesn't work it doesn't even enter that part of the code (no serial comms between the reader and arduino). I don't understand what you mean, I need to read all bytes to check the serial number from the sensor to make sure it's mine.

Also I'm using serial1 because the pro-micro is a leonardo derivative and serial is for PC and serial1 is hardware serial. At least from my understanding.

Also I'm using serial1 because the pro-micro is a leonardo derivative

I'm not looking at the same thing you are, then, because I don't see that. It's time for you to post some links.

I don't understand what you mean, I need to read all bytes to check the serial number from the sensor to make sure it's mine.

In the same way that you can't read my reply until I get done typing it, you can't read serial data before it arrives. You are testing that there is at least one byte of data, and then assuming that it is safe to read 34 bytes. It is quite likely that there is exactly one byte available to read, so you will be stuffing your array with 33 -1s.

Hi, the link for the pro-micro is this Pro Micro - 3.3V/8MHz - DEV-12587 - SparkFun Electronics
You were probably thinking about the pro-mini, very similar name.

I know for sure that as soon there is something in the serial port the sensor has sent it's contents, thats why i have it reading this way.
The sensor sends it's string of bytes, every second while it's being shaked, banged, etc, any movement will trigger it.
When i walk away then the serial comms start breaking up and the array might get filled with "garbage", then serial number check kicks in and the unlocking fails, when i'm a certain distance away then the serial comms are severed and -1s start coming in and the lock function is called.

Tiagoamarante:
I know for sure that as soon there is something in the serial port the sensor has sent it's contents

Actually, you don't. When Serial.available returns 1, all you know is that there is 1 byte available to read.

OK.
I know that whenever there is something above 0 in the serial port, i know that it is a sensor and that the sensor always sends 34 bytes every second. I'm not deniyng that the check could be more thorouh, check for FF wich is always the first byte from sensor and then check for 55 wich is always the second, and so on, but for simplicity, i don't think is needed, from experimentation this is what happens every time, and when it's not, it's either that the serial communication is breaking up (walking away from detection zone) or that there is no sensor being heard (-1 in the port).

Also i've been checking to see where the arduino is getting stuck and it's in the setup loop, possibly to due with the "while" check at the end of it, but i've removed that and is still hit and miss. when hit everithing works ok when a miss then nothing appears on serial1, so maybe to do with my initilization strings. Any other way sending those with more reliably?

Tiagoamarante:
OK.
I know that whenever there is something above 0 in the serial port, i know that it is a sensor and that the sensor always sends 34 bytes every second.

Yes, but you don't know whether they've ARRIVED YET.

Your Arduino runs very quickly compared to the time taken to transmit 34 bytes over a serial link, and it is perfectly capable of spotting the first byte and processing it before the subsequent bytes have finished transmission.

The point we are trying to make is that if you need to read 34 bytes, you must wait to do so until Serial.available() returns 34 or more.

Alternatively, you can read and store whatever has arrived, as soon as it arrives, but do nothing with the data until the 34th byte (or end marker if there is one) has arrived.

Problem solved.

Apparently i needed a small delay after I started the serial1, now it works everytime.

I now understand what you mean about trying to read without being sure its there, like i could be overtaking the serial stream.
I have changed that to check for all the bytes before i read them. Even thou it works without that check it is bad practice.
Thanks for your help.

One other thing, do you think i'll run into problems when millis overflows in 50 days?
I don't really expected to be using the autorelocking override much often (just get in and start the car). But any ideias to improve that?

One other thing, do you think i'll run into problems when millis overflows in 50 days?

It looks like all your time stuff involves subtraction. You'll be fine.