simple program that seems to get stuck (Solved)

I have created a simple program that will allow a air actuator to be on/off in specific patterns.
I tried to make a program block for all the programs and the reset function however the program gets stuck somewhere and the "L" diod blinks and resets the whole program.

The mock programs is for debug, so for the moment the output is on 1 time for program 1 and 2 times for program 2 etc.
This works, the reset works, however when i start one program, i want the output to stay on after the block is done so that i have to reset it with the reset button. In this case the arduino resets itself after 12-28 seconds.

// Vilka ingångar/utgångar som används till vad
const int knapp1 = 7;      // Program 1
const int knapp2 = 8;      // Program 2
const int knapp3 = 9;      // Program 3
const int knapp4 = 10;     // Program 4
const int knapp5 = 11;     // Program 5
const int knapp6 = 12;     // Reset
const int ventil = 2;      // Tryckluftsventil

// Variablar för om knappar är intryckta
boolean knapplage1 = false;
boolean knapplage2 = false;
boolean knapplage3 = false;
boolean knapplage4 = false;
boolean knapplage5 = false;
boolean knapplage6 = false;

void setup() {
  // Detta körs vid uppstart av systemet


pinMode(knapp1, INPUT);
pinMode(knapp2, INPUT);
pinMode(knapp3, INPUT);
pinMode(knapp4, INPUT);
pinMode(knapp5, INPUT);
pinMode(knapp6, INPUT);
pinMode(ventil, OUTPUT);



}

void loop() {
  
  // Kolla vilken knapp som trycks in

knapplage1 = digitalRead(knapp1);
knapplage2 = digitalRead(knapp2);
knapplage3 = digitalRead(knapp3);
knapplage4 = digitalRead(knapp4);
knapplage5 = digitalRead(knapp5);
knapplage6 = digitalRead(knapp6);


// Starta respektive program eller återställa systemet
if (knapplage1 == 1){
  program1();
}

else if (knapplage2 == HIGH) {
  program2();
  }

else if (knapplage3 == HIGH) {
  program3();
  }

else if (knapplage4 == HIGH) {
  program4();
  }

else if (knapplage5 == HIGH) {
  program5();
  }

else if (knapplage6 == HIGH) {
  reset();
  }

else {
  
  delay(10);
}
}
void program1() {              // Program 1
digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
                               // HIGH = ventil påverkad, LOW = ventil opåverkad

}

void program2() {              // Program 2
digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
delay(500);                   // HIGH = ventil påverkad, LOW = ventil opåverkad
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
}

void program3() {              // Program 3
digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
delay(500);                   // HIGH = ventil påverkad, LOW = ventil opåverkad
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
delay(500);
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH); 
}

void program4() {              // Program 4
digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
delay(500);                   // HIGH = ventil påverkad, LOW = ventil opåverkad
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
delay(500);
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
delay(500);
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH); 
}

void program5() {              // Program 5
digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
delay(500);                    // HIGH = ventil påverkad, LOW = ventil opåverkad
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
delay(500);
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
delay(500);
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
delay(500);
digitalWrite(ventil, LOW);
delay(500);
digitalWrite(ventil, HIGH);
}

void reset() {                  // Sätter kolven i sitt "hemmaläge"
digitalWrite(ventil, LOW);      // HIGH = ventil påverkad, LOW = ventil opåverkad
}

How are your switches wired? What, exactly, is connected to pin 2?

The +5v are wired trough the buttons to the inputs with a 10K resistor agains ground (pull down)
The Pin #2 is connected to a sparkfun relay (+5v control signal)

Do the Arduino and relay share the same power supply? Do you have a freewheeling diode on the relay?

Yes, they are on the same powersuply.
i attach the electrical drawing on the relay pcb

you think it can be power loss on the arduino ?
If you press the reset button on the arduino then the RX/TX diods flash, this is not the case now, only the "L" diod flash rapidly 3-4 times then its back on.
The "L" diod is connected to output 13? then i should be off? i do not use this output.

( i tried to reset it now and the TX/RX dont flash so i was wrong in that)

RelayBoard-Large-v16.pdf (30.5 KB)

Just to be sure i switched the relay pcb to a red LED and it still behaves the same so the power is not the problem

i attach the electrical drawing on the relay pcb

So, you do not have a relay connected to pin 2. What is actually connected to pin 2 is a transistor (the gate, to be specific).

How do you know that the Arduino resets? Use Serial.begin() and Serial.println() in setup() to confirm that the Arduino is resetting.

Double check the wiring for all of your switches.

I changed the inputs to INPUT_PULLUP and switched the sense, also changed the output to pin 13. I don't have your hardware, so this is so I could test with just a jumper wire for input. It seems to work. So I would look at hardware problems.

// Vilka ingångar/utgångar som används till vad
const int knapp1 = 7;      // Program 1
const int knapp2 = 8;      // Program 2
const int knapp3 = 9;      // Program 3
const int knapp4 = 10;     // Program 4
const int knapp5 = 11;     // Program 5
const int knapp6 = 12;     // Reset
const int ventil = 13;      // Tryckluftsventil

// Variablar för om knappar är intryckta
boolean knapplage1 = false;
boolean knapplage2 = false;
boolean knapplage3 = false;
boolean knapplage4 = false;
boolean knapplage5 = false;
boolean knapplage6 = false;

void setup() {
  // Detta körs vid uppstart av systemet


  pinMode(knapp1, INPUT_PULLUP);
  pinMode(knapp2, INPUT_PULLUP);
  pinMode(knapp3, INPUT_PULLUP);
  pinMode(knapp4, INPUT_PULLUP);
  pinMode(knapp5, INPUT_PULLUP);
  pinMode(knapp6, INPUT_PULLUP);
  pinMode(ventil, OUTPUT);



}

void loop() {

  // Kolla vilken knapp som trycks in

  knapplage1 = digitalRead(knapp1);
  knapplage2 = digitalRead(knapp2);
  knapplage3 = digitalRead(knapp3);
  knapplage4 = digitalRead(knapp4);
  knapplage5 = digitalRead(knapp5);
  knapplage6 = digitalRead(knapp6);


  // Starta respektive program eller återställa systemet
  if (knapplage1 == LOW) {
    program1();
  }

  else if (knapplage2 == LOW) {
    program2();
  }

  else if (knapplage3 == LOW) {
    program3();
  }

  else if (knapplage4 == LOW) {
    program4();
  }

  else if (knapplage5 == LOW) {
    program5();
  }

  else if (knapplage6 == LOW ) {
    reset();
  }

  else {

    delay(10);
  }
}
void program1() {              // Program 1
  digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
  // HIGH = ventil påverkad, LOW = ventil opåverkad

}

void program2() {              // Program 2
  digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
  delay(500);                   // HIGH = ventil påverkad, LOW = ventil opåverkad
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
}

void program3() {              // Program 3
  digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
  delay(500);                   // HIGH = ventil påverkad, LOW = ventil opåverkad
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
  delay(500);
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
}

void program4() {              // Program 4
  digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
  delay(500);                   // HIGH = ventil påverkad, LOW = ventil opåverkad
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
  delay(500);
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
  delay(500);
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
}

void program5() {              // Program 5
  digitalWrite(ventil, HIGH);    // Ställ tider här 1000 = 1 sekund
  delay(500);                    // HIGH = ventil påverkad, LOW = ventil opåverkad
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
  delay(500);
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
  delay(500);
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
  delay(500);
  digitalWrite(ventil, LOW);
  delay(500);
  digitalWrite(ventil, HIGH);
}

void reset() {                  // Sätter kolven i sitt "hemmaläge"
  digitalWrite(ventil, LOW);      // HIGH = ventil påverkad, LOW = ventil opåverkad
}

Ok, so now i am even more confused...
I put Serial.begin(9600); and Serial.println(knapplage1); in setup()
ran the program and then it dont reset the output IF i have the serial monitor up, if i dont bring up the serial monitor it behaves as before.
The wiring is ok on the buttons. dubble checked everything.

So to sum it up.

  1. Arduino starts

  2. I press a button and the selected program starts

  3. The LED or Relay (whatever i have connected) runs and stays on

  4. After 10-30 sec it turns off without me pressing the reset button

  5. The "L" led on the arduino PCB flashes 4 times quick, goes dark and then turns back on

  6. If i put the Serial.begin(9600); Serial.println(knapplage1); and opens the monitor it works flawless

  7. If i then close the monitor the same error reappear after 10-30 sec.

Post a schematic of the switch hardware. Load an example program like BlinkWithoutDelay, just to make sure things are generally okay.

Actually, please post a schematic of the entire system.

So.... i bow my head in shame and realize that there were no error in the programming and no error in the electrical wiring.

The program and the arduino worked perfectly when i had the serial monitor up because it then blocked all other comunication than its own.

I put it out from the computer on its own 9V power and it works as intended.
So the fault lays in my computer that probably are sending signals on the cable making the arduino to reset.

Thank you all for your help !