millis, digitalWrite, RFID and erratic results...

I’m nearly done with my RFID gate control. I’m working on the code to lift the gate. I’m at a pause because I only have one relay. But I’ve run into some unpredictable and erratic behavior.

The main problem, it will stop reading RFID Tags if i have an actuator attached to the relay.

This is the very first line of the loop

 if (!mfrc522.PICC_IsNewCardPresent()) { //if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
    Serial.println("looking for new card");
    return; //if it did not find a new card is returns a '0' and we return to the start of the loop
  }

All it does is look for a card to scan. It loops through this until there is a card near the scanner. It loops pretty fast. more on this in a moment.

This bit of code I added to start working the logic to raise and lower the gate. It will run fine without anything attached to the relay, but attach the motor to the relay and everything stops functioning.

Remember that first line? I notice that when it faults, the whole arduino, or at least the rate its printing the comment, slows down??? I don’t know what to make of this. on the occation i can run the loop 2 or 3 times it doesn’t slow down.

unsigned long interval=5000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
unsigned long startMillis = millis(); // grab current time
      raisegate();
      Serial.println("gate is raised");
      //lower gate
      previousMillis=0;
      startMillis = millis();
      while((unsigned long)(millis() - startMillis) <= interval){// check if "interval" time has passed (5000 milliseconds)
        if(digitalRead(2) == LOW){
          lowergate();
        }else if(digitalRead(2) == HIGH){
          raisegate();
        }else{
        digitalWrite(3,HIGH);
      }
    }

in fact it seems this has kinda been sparatic, sometimes i seem to need to compile and upload the code a couple times in order to get the rfid tag to read, sometimes i need to load a different sketch then reload my sketch then itl read.

i’m not sure where to go from here. I’ve attached the whole sketch. Everything seems to be working up till i attach an actuator motor up to the relay.

thank you very much for the help.

and thank you forum for helping me get this far. i’ve learned quite a bit. slowly but, you know.

ppcc_gate.ino (13.5 KB)

I'm messing with it. being sure to have the motor a couple feet away. but the moment i touch the lead wire to the motor, it will no longer read rfid cards.

birddseedd:
I’m messing with it. being sure to have the motor a couple feet away. but the moment i touch the lead wire to the motor, it will no longer read rfid cards.

You need to make a pencil drawing showing how everything is connected and post a photo of the drawing. Also post a photo of the actual hardware. See this Simple Image Guide

You have over 13K of code. When did this problem emerge? Presumably you have an earlier version that does work properly? What is the difference between the working and non-working versions?

…R

I suspect that the last step that birdseedd did was using the latest working code and adding the motor.

Sounds like a hardware problem to me; possibly interference and I'm not knowledgeable in that area.

Well. I switched the leads on the relay. So instead of energy flowing one way when it. Is activated, it flows the other way.

That seems to have fixed it for the most part. I really dont understand why.

Still will randimly quit. That might be because if the cheap breadboard. I dont know.