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!