So I'm trying to understand what I'm doing wrong here. what the problem is that when I run sketch it gets to the point where the first if statement where it need to runs "Serial.println("Pin Is HIGH");", but dose not run "digitalWrite(outPin, HIGH);". So I tried using a different board, the Arduino Leonardo and the same result. So in essence not sure why the pin is being set to high. Any help would be appreciated.
int outPin = 13; //control relay module
int inputPin = 12; //get input from the car.
int shutdownPin = 11; //send signal to raspberry pi 3+ to shutdown
int val;
long previousMillis = 0;// will store last time pin was updated
long interval = 0; //set to 0 for testing. should be set to 1740000
long restTime = 1209600000; //2 weeks in milliseconds
void setup() {
pinMode(outPin, OUTPUT); // sets the digital pin to output
pinMode(inputPin, INPUT); // sets the digital pin to input
pinMode(shutdownPin, OUTPUT); // sets the digital pin to output
Serial.begin(9600);
}
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void loop() {
unsigned long currentMillis = millis(); //get current millis from arduino
while (true) {
val = digitalRead(inputPin); // read the input pin
if (val == HIGH) {
digitalWrite(outPin, HIGH); //Power on
Serial.println("Pin Is HIGH");
}
else if (val == LOW) {
Serial.println("Pin is LOW");
if (currentMillis - previousMillis > interval) {
Serial.println("right time");
previousMillis = currentMillis;//save the last time
digitalWrite(shutdownPin, HIGH); //turn pin to high and tell's pi to turn off
delay(60000);
digitalWrite(outPin, LOW); // sets the LED to the button's value
}
}
else if (currentMillis - previousMillis > restTime) {
resetFunc(); //call reset be for 60 days
}
else {
Serial.println("Why?");
}
}
}
-
Relay or relay board? Link can be useful.
2)
How is the relay wired? Schematic (photo of handdrawn is OK) including power connections.
3)
What is controlled by the relay?
Why do you use a while-loop? As a result, your currentMillis is only updated once in the lifetime of the program.
Sorry, yes it is a relay board. I have a 5V power, GND and pin 13 going to the relay board. For what it the relay board controls is just power to a Raspberry Pi 3+ to turn it off and on. Not sure why I used a while-loop, it just felt right. Here a link to photos (for some reason I wasn't able to attach them) https://drive.google.com/open?id=13y5qMORnbkku3Ca0YluSGuoYz5_gLJaH
Do you have anything wired to inputPin? Since you didn't set the pinMode to INPUT_PULLUP it will randomly read as HIGH or LOW if not connected to something.
Do you know if the input to the relay board is active HIGH or LOW? Some relay boards turn ON with a LOW input.
Your if statements are not logical, you have:
If val = HIGH
// do something
else if val = LOW
// do something
else if ... //you will never reach this statement because val can only be HIGH or LOW
Yes I do have a wire going to the inputPin and it is a 3V output with a switch to turn on and off power to test HIGH and LOW state of the inputPin.
The relay board is active on HIGH.
I actually didn't notice that error in my logic on the statement because I haven't got to that point yet. I feel pretty stupid for not noticing that,
The main problem is that the first if statement condition where if (val == HIGH). It runs the Serial.println just fine, but the digitalWrite dose not run or dose not set the pin to HIGH. One thing I did triy was making a new sketch with just the digitalWrite and the pin turns to HIGH and the relay board worked just fine.
if (val == HIGH) {
digitalWrite(outPin, HIGH); //either not being called, not setting pin to HIGH or skipping over, not sure
Serial.println("Pin Is HIGH"); //But this runs with no problem
}
else if (val == LOW) {
Serial.println("Pin is LOW");
if (currentMillis - previousMillis > interval) {
Serial.println("right time");
previousMillis = currentMillis;//save the last time
digitalWrite(shutdownPin, HIGH); //turn pin to high and tell's pi to turn off
delay(60000);
digitalWrite(outPin, LOW); // sets the LED to the button's value
}
};
After looking my relay board it's a active when low, even though the relay board manual says high. So thank you asking that question, because I believe you just solve my problem and gave me a lot to edit on my code. Thank you again. 