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