Hello.
I'm having trouble getting my project to work properly.
It's supposed to toggle relays that drives an hydraulic press, But for some reason beyond my understanding it does not allways work.
The general idea is to toggle both up and down with 1 button, And controll a light relay according to what's happening.
Step 1
Press button = turn LED on and relay1 for 7 seconds then release relay1, Wait for new input.
Step 2
Press button again = Flash LED and turn on relay2 for 7 seconds then turn relay2 and LED off, Wait for new input.
But what happens is sometimes when i power the arduino it waits for 5-60 seconds then turns the led and relay1 on for 7 seconds, then turns them both off.
If this happens and i press the button it repeats this faulty behaviour, Not leaving the Led on. And only toggles this function of Step 1, Never doing Step 2.
This can repeat with button presses for up to 20 times before it goes into "normal" working mode.
And sometimes it works as intended, Toggling between Step 1 and 2.
Button input had 10k pull-down to ground, Is operated from 5v.
Input is 12v with Diode on both sides (+ and -) as this is sitting in a car.
Running on an Arduino Uno, Please help as this is driving me crazy and all i've tried fails.
int buttonPin = 8;
int OutputPin7 = 7;
int OutputPin6 = 6;
int LedPin5 = 5;
int state = 0;
int newButtonState;
int oldButtonState = LOW;
long time = 0;
long debounce = 200;
void setup() {
pinMode(OutputPin7, OUTPUT);
pinMode(OutputPin6, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(LedPin5, OUTPUT);
}
void loop()
{
newButtonState = digitalRead(buttonPin);
if (newButtonState == HIGH && oldButtonState == LOW && millis() - time > debounce) {
if (state == 0) {
state = 1;
digitalWrite(LedPin5, HIGH);
digitalWrite(OutputPin7, HIGH);
delay(7000);
digitalWrite(OutputPin7, LOW);
} else {
state = 0;
digitalWrite(OutputPin6, HIGH);
digitalWrite(LedPin5, LOW);
delay(2000);
digitalWrite(LedPin5, HIGH);
delay(2000);
digitalWrite(LedPin5, LOW);
delay(2000);
digitalWrite(LedPin5, HIGH);
delay(1000);
digitalWrite(LedPin5, LOW);
digitalWrite(OutputPin6, LOW);
}
}
oldButtonState = newButtonState;
}