I have been trying to make a circuit with an off delay and an on delay potentiometer. the input is a sensor and the output is a relay. I have tried many things but nothing seems to be working. I believe that a map function should work but I am not too positive that I am doing it correctly. I am fairly new to Arduino so a lot of this still confuses me. I appreciate any tips or advice that you might have about my code.
const int analogPin = A0; // pin that the potentiometer is attached to
const int analogPin2 = A4; // pin that the potentiometer is attached to
const int relay = 13; // SPDT relay
const int sensor = 2; // pin that the sensor is attached to
bool latch = 0;
void setup() {
// initialize the relay pin as an output:
pinMode(relay, OUTPUT);
pinMode(sensor, INPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(A0);
analogValue = map(analogValue, 0, 1023, 0, 10);
delay(50);
int analogValue2 = analogRead(A4);
analogValue2 = map(analogValue2, 0, 1023, 0, 10);
delay(50);
int sensorValue = digitalRead(sensor);
if (sensorValue == 1) {
latch = 1;
}
if (latch == 1) {
digitalWrite(relay, HIGH);
delay(analogValue);
} else {
digitalWrite(relay, LOW);
delay(analogValue2);
}
latch = 0;
// print the analog value:
Serial.print(analogValue);
Serial.print(" ");
Serial.print(analogValue2);
Serial.print(" ");
Serial.print(sensorValue);
Serial.print(" ");
Serial.println(latch);
delay(500); // delay in between reads for stability
}