Having trouble with a "toggle" program.

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;
}
 if (newButtonState == HIGH && oldButtonState == LOW && millis() - time > debounce) {

You have part of a debouncing routine there, but you never give time any value other than zero. time needs to have the time of the last transition on the switch.

Please use code tags. The forum software eats little pieces of your code if you use other types of formatting.

Very well done that you have given each of your pins a name at the top of the program. But names like OutputPin7 are a bad idea. The name should describe the function of the pin. If this is the "up" relay then call it UpRelay or something like that.

The time variables should be unsigned long, not just long. You never change time from the initial zero, so your debouncing doesn't work.

Are you running anything else off the Uno's 5v output? Despite the specification, 12v is a little too much voltage for the main power regulator on the Uno.