Arduino automatically sets output pins high.

I am working on a RFID car starter using an arduino nano, 3 12v relays, RFID module and misc. resistors/transistors. Also turn off ignition with rfid based on oil pressure switch input(if engine is already running or not). I don't have a schematic on how everything is connected, but I am confident that all my hardware is connected properly. This is my first time using the arduino and do not have experience with programming. When I upload my sketch 2 relays energize followed my the third, which I put in a 3 second delay to allow fuel system priming before cranking. I have looked at different examples of simple pushbutton inputs controlling an led, which I figured would be similar to what I am trying to achieve.

const int r1Pin = 4;  //Ign. relay 1 on pin 4
const int r2Pin = 6;  //Ign. relay 2 on pin 6
const int r3Pin = 8;  //Starter relay on pin 8
const int rfidPin = 10;  //RFID input on pin 10
const int killPin = 13;  //Oil Pressure Switch on pin 13

void setup () {
  pinMode(r1Pin, OUTPUT);  //Defines inputs or outputs
  pinMode(r2Pin, OUTPUT);
  pinMode(r3Pin, OUTPUT);
  pinMode(rfidPin, INPUT);
  pinMode(killPin, INPUT);
}

//should set r1Pin and r2Pin HIGH when rfidPin is HIGH
//should set r3Pin HIGH while rfidPin is HIGH after 3 second delay.
void loop(){
  int digitalRead(rfidPin);
   if (rfidPin == HIGH);
   {
    digitalWrite(r1Pin, HIGH);
    digitalWrite(r2Pin, HIGH);
    while (rfidPin == HIGH);
    {
    delay(3000);
      digitalWrite(r3Pin, HIGH);
    }
  }
}

This was written to see if the relays would turn on with the RFID input. I still have to finish it with the input from the oil pressure switch. I was just bench testing to see if it was working with my sketch. I feel like I'm missing something simple. Any help would be greatly appreciated and I will answer any questions as I have may left out somethings.

   if (rfidPin == HIGH);

If statements rarely end with a semicolon. You are unconditionally doing the stuff in the block that follows.

int digitalRead(rfidPin);

So read the value on the rfidPin and put it nowhere, why bother? Did you mean to assign it to a variable?

while (rfidPin == HIGH);

The variable rfidPin is always going to be exactly 13 which is what you set it to. Should this be the variable you missed?

Paul - that may be my whole problem. Mike - As a digital input, do they need to be assigned as a variable since they will always read either HIGH or LOW? I was under the impression that variables were mostly used on analog inputs where you would read a range on a sensor? Thanks for the help. I'll try to change the sketch and test again when I get a few minutes.

  int digitalRead(rfidPin);

This reads the rfidPin and then throws away the value it read.

Mark

I was under the impression that variables were mostly used on analog inputs where you would read a range on a sensor?

You might have been but you are wrong. A variable holds a value, you have a variable called rfidPin that holds the value 13. True digital pins return digital values HIGH or LOW so you need a boolean variable type to hold it. In fact it will fit into a int but you shouldn't do that it is bad practice. So you need to do:-

boolean rfidValue = digitalRead(rfidPin);

Then:-

while (rfidValue == HIGH) {  // do stuff here including a:- 
rfidValue = digitalRead(rfidPin); // otherwise you will never get out of the while loop

I made a few changes to the sketch before I left work, and the relays now work properly and should start the car. I added a few more bits just now to incorporate the oil pressure switch to shut off ignition when signaled with rfid and engine running (killPin HIGH). Thanks again for the help.

const int r1Pin = 4;  //Ign. relay 1 on pin 4
const int r2Pin = 6;  //Ign. relay 2 on pin 6
const int r3Pin = 8;  //Starter relay on pin 8
const int rfidPin = 10;  //RFID input on pin 10
const int killPin = 13;  //Oil Pressure Switch on pin 13
int rfidState = 0;
int killState = 0;

void setup () {
  pinMode(r1Pin, OUTPUT);  //Defines inputs or outputs
  pinMode(r2Pin, OUTPUT);
  pinMode(r3Pin, OUTPUT);
  pinMode(rfidPin, INPUT);
  pinMode(killPin, INPUT);
}

//Set r1Pin and r2Pin HIGH when rfidPin is HIGH
//Set r3Pin HIGH while rfidPin is HIGH after 1/2 second delay.
void loop(){
  rfidState = digitalRead(rfidPin);
  killState = digitalRead(killPin);
   if (rfidState == HIGH)
   {
    digitalWrite(r1Pin, HIGH);
    digitalWrite(r2Pin, HIGH);
    delay(500);
      digitalWrite(r3Pin, HIGH);
   }
      else if (rfidState == LOW)
      digitalWrite(r3Pin, LOW);
     else if (rfidState && killState == HIGH)//killPin is HIGH with engine running
  {                                          //Shuts off Ignition when RFID and
    digitalWrite(r1Pin, LOW);                //killPin are HIGH
    digitalWrite(r2Pin, LOW);
  }
}

There is alot of good information on this website, but would anyone recommend a book to help with my (lack of) programming skills? Should I look for C/C++ for dummies or something arduino specific to start out with?

Hi,

I don’t have a schematic on how everything is connected, but I am confident that all my hardware is connected properly.

I would suggest you do some reverse engineering and draw up a circuit diagram, we need to know how you have connected your inputs and outputs, as well as bypassing and gnd continuity.

A CAD circuit in jpg, png or pfd, or a picture of a hand drawn circuit would be invaluable.

Tom…Hope to help… :slight_smile: