Using a Nano to replace 50+ year old relay system

Hello all,

I've got an old 65 Lincoln that has old relays that control the rear windows. Specifically, when you push the door handle button, it closes a switch, causing that window to drop about 5 inches or so, allowing it to clear the convertible seals, then when you close the door, the switch is opened, and then the window goes back up again.

I've have a version of this code that I got from a fellow enthusiast, but was looking to add a potentiometer input to A0 for the left side, and A1 for the right side, allowing me to adjust the drop/rise time of the window, as these window motors are not necessarily all the same after 50+ years of operation, lubrication states, friction between seals, etc.

Here is the original version, using active low relays, and 2000 millis as the drop/rise time (unchanging):

const int leftRelayUpTimeLimit = 2000; //time to roll window up, in millis
const int leftRelayDownTimeLimit = 2000; //time to roll window down, in millis
const int leftButtonPin = 3;  //D3
const int leftRelayUpPin =  12;  //D12
const int leftRelayDownPin =  13;//D13
int leftOperation = 0; //0:closed, 1:opening, 2:open, 3:closing
int leftCounter = 0;

const int rightRelayUpTimeLimit = 2000; //time to roll window up, in millis
const int rightRelayDownTimeLimit = 2000; //time to roll window down, in millis
const int rightButtonPin = 2;  //D2
const int rightRelayUpPin =  10;  //D10
const int rightRelayDownPin =  11;  //D11
int rightOperation = 0; //0:closed, 1:opening, 2:open, 3:closing
int rightCounter = 0;

void setup() {
  pinMode(leftRelayUpPin, OUTPUT);
  pinMode(leftRelayDownPin, OUTPUT);
  pinMode(leftButtonPin, INPUT);
  digitalWrite(leftRelayUpPin, HIGH);
  digitalWrite(leftRelayDownPin, HIGH);
  
  pinMode(rightRelayUpPin, OUTPUT);
  pinMode(rightRelayDownPin, OUTPUT);
  pinMode(rightButtonPin, INPUT);
  digitalWrite(rightRelayUpPin, HIGH);
  digitalWrite(rightRelayDownPin, HIGH);
}

void loop() {
  //LEFT WINDOW OPERATION
  if (digitalRead(leftButtonPin) == HIGH && leftOperation == 0) {
    leftCounter = 0;
    leftOperation = 1;
  } else if(digitalRead(leftButtonPin) == LOW && leftOperation == 2) {
    leftCounter = 0;
    leftOperation = 3;
  }

  if(leftOperation == 1){
    if(leftCounter < leftRelayDownTimeLimit/10){
      digitalWrite(leftRelayUpPin, LOW);
      digitalWrite(leftRelayDownPin, HIGH);
      leftCounter++;
    } else{
      digitalWrite(leftRelayUpPin, HIGH);
      digitalWrite(leftRelayDownPin, HIGH);
      leftOperation = 2;
    }
  }else if(leftOperation == 3){
    if( < leftRelayUpTimeLimit/10){
      digitalWrite(leftRelayUpPin, HIGH);
      digitalWrite(leftRelayDownPin, LOW);
      leftCounter++;
    } else{
      digitalWrite(leftRelayUpPin, HIGH);
      digitalWrite(leftRelayDownPin, HIGH);
      leftOperation = 0;
    }
  }

  //RIGHT WINDOW OPERATION
  if (digitalRead(rightButtonPin) == HIGH && rightOperation == 0) {
    rightCounter = 0;
    rightOperation = 1;
  } else if(digitalRead(rightButtonPin) == LOW && rightOperation == 2) {
    rightCounter = 0;
    rightOperation = 3;
  }

  if(rightOperation == 1){
    if(rightCounter < rightRelayDownTimeLimit/10){
      digitalWrite(rightRelayUpPin, LOW);
      digitalWrite(rightRelayDownPin, HIGH);
      rightCounter++;
    } else{
      digitalWrite(rightRelayUpPin, HIGH);
      digitalWrite(rightRelayDownPin, HIGH);
      rightOperation = 2;
    }
  }else if(rightOperation == 3){
    if(rightCounter < rightRelayUpTimeLimit/10){
      digitalWrite(rightRelayUpPin, HIGH);
      digitalWrite(rightRelayDownPin, LOW);
      rightCounter++;
    } else{
      digitalWrite(rightRelayUpPin, HIGH);
      digitalWrite(rightRelayDownPin, HIGH);
      rightOperation = 0;
    }
  }

  delay(10);
}

Here is the version I modified to incorporate a potentiometer and active high relays. I've only got the left side hooked up on the bench for testing, and would replicate the working method to the right side:

int leftPot = 0;
int leftRelayUpTimeLimit = 0; //time to roll window up, in millis
int leftRelayDownTimeLimit = 0; //time to roll window down, in millis
const int leftButtonPin = 3;
const int leftRelayUpPin =  12;
const int leftRelayDownPin =  13;
int leftOperation = 0; //0:closed, 1:opening, 2:open, 3:closing
int leftCounter = 0;



void setup() {
  Serial.begin(9600);

  pinMode(leftRelayUpPin, OUTPUT);
  pinMode(leftRelayDownPin, OUTPUT);
  pinMode(leftButtonPin, INPUT);
  digitalWrite(leftRelayUpPin, LOW);
  digitalWrite(leftRelayDownPin, LOW);

 
}

void loop() {
  Serial.print("Raw-");
 
  leftPot = analogRead(A0);
   Serial.print(leftPot);
  int leftRelayDownTimeLimit = map(leftPot, 0, 1023, 0, 4000); //time to roll window down, in millis
  int leftRelayUpTimeLimit = map(leftPot, 0, 1023, 0, 4000); //time to roll window up, in millis
   
   Serial.print("Time Down-");
   Serial.print(leftRelayDownTimeLimit);
   Serial.print("Time Up-");
   Serial.print(leftRelayUpTimeLimit);
   
   
  //LEFT WINDOW OPERATION
  if (digitalRead(leftButtonPin) == HIGH && leftOperation == 0) {
    leftCounter = 0;
    leftOperation = 1;
  } else if (digitalRead(leftButtonPin) == LOW && leftOperation == 2) {
    leftCounter = 0;
    leftOperation = 3;
  }
  
   
   if (leftOperation == 1) {
    if (leftCounter < leftRelayDownTimeLimit/10) {
      digitalWrite(leftRelayUpPin, HIGH);
      digitalWrite(leftRelayDownPin, LOW);
      leftCounter++;
    } else {
      digitalWrite(leftRelayUpPin, LOW);
      digitalWrite(leftRelayDownPin, LOW);
      leftOperation = 2;
    }
  } else if (leftOperation == 3) {
    if (leftCounter < leftRelayUpTimeLimit/10) {
      digitalWrite(leftRelayUpPin, LOW);
      digitalWrite(leftRelayDownPin, HIGH);
      leftCounter++;
    } else {
      digitalWrite(leftRelayUpPin, LOW);
      digitalWrite(leftRelayDownPin, LOW);
      leftOperation = 0;
    }
     
 
  }


  delay(10);
}

My problem is, by just powering it up, it switches the relays with no user input, and when I activate the switch on D3, it stops it from cycling. I've tried it on 2 different Nanos, different breadboardsand get the same issue. Am I missing something with the counter?

Thank you for the help!

Look at the difference between the 2 different sets of code. I find this in setup.

  digitalWrite(leftRelayUpPin, HIGH);
  digitalWrite(leftRelayDownPin, HIGH);
  digitalWrite(leftRelayUpPin, LOW);
  digitalWrite(leftRelayDownPin, LOW);

That is due to the original using active low relays, which consume power to make the relay. I changed to active highs so that the relays are only made when a window is commanded up or down, not idle.

What relays are you using?

If you switch the Setup code to write "HIGH" instead of "LOW", does the problem go away?

I’m using relays from this seller, but the specific one isn’t listed any more. I have a dual relay board that has a switch to change the relays from active high or active low.

I’ll modify it to see if maybe there’s some sort of crosstalk that causes one relay channel to change the other. Could be why it does the switching by itself.

How are the button switches wired? What is the reading of a pushed button? Are you using an external pull down or pull up?

I have a microswitch wired to the normally open contacts with no pull-up or pull down, from 3v3 through switch when door is opened, closing switch, sending a high to D3.

Would that make a difference (pull up/pull down) in preventing that pin from just floating and it not switching on its own?

I'm not certain of the wiring given your written description, but I think that if a switch closure is sending voltage to D3 to read HIGH, then the pin is floating when the micro switch is open and you want the pin to read LOW. It needs a pull down to ground.

The alternative(and usually preferred) way is to wire one side of the switch to ground and the other side to the input pin. Set the pinMode of the pin to INPUT_PULLUP. The pin will read HIGH when the switch is open and LOW when closed,

That was it, putting in a resistor to ground fixed the issue. Thank you all for the help.