would like run the code ( program) continously.

hello

this is my first project/ I got this code here runs fine but after few second or may be when the code ends it turns off.

For example a when i put a simple code for a led ... why the led can't stay on forever instead it turn off and lights back

I would like this to stay on let say for ever continuously. there was a issue with wiring diagram that I resolved.
I got this code from; http://civicjunkie.com/main/2014/09/02/how-to-install-a-start-button/#more-49

thanks in advance if someone could help.

// (C) 2014 Scott Hubble. NOT FOR COMMERCIAL USE.
//Push button ignition switch for automobiles.
//Made for 1988-2000 Honda Civic/CRX, but may be adapted to other vehicles.
//By Scott Hubble. Original version started 2114 Aug. 11.
//Ver. 6 started 2014 Aug. 15. Completed 2014 Aug. 15 @ 23:10 -7 UTC.
//UPDATE: 2014 Aug. 16 - Added LED output for button illumination.
//UPDATE: 2014 OCT. 2 - Changed for standalone Atmega.


int b1 = 3; //Start button.
int b2 = 2; //Clutch interlock switch.
int acc = 9; //Accessory output.
int ign = 8; //Ignition output.
int st = 7; //Starter output.
int ledPin = 12; //LED for button illumination.
int accLed = 11; //ACC indicator.
int onLed = 13; //ON indicator.

long unsigned time = 0; // Timer.
long unsigned time2 = 0; //2nd timer.
int ledFlash = 0;
int ledFlash2 = 0;
int ledOn = 0;
int b1Stat = 0; //Was the primary switch pressed?
int b2Stat = 0; // Secondary switch pressed?
int did = 0;
int did2 = 0;
int did3 = 0;
int on = 0;
int on2 = 0;

void setup()
{
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(acc, OUTPUT);
  pinMode(ign, OUTPUT);
  pinMode(st, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(accLed, OUTPUT);
  pinMode(onLed, OUTPUT);
  time = millis();
  time2 = millis();
  digitalWrite(acc, HIGH);
  digitalWrite(ign, HIGH);
  digitalWrite(st, HIGH);
  digitalWrite(accLed, HIGH);
  digitalWrite(onLed, HIGH);
  digitalWrite(ledPin, HIGH);
}

void loop()
{
  // -------------------------Read the bottons:-----------
  if(digitalRead(b1) == HIGH && b1Stat == 0){
    b1Stat++;
    on++;
    delay(90);
  }
  if(digitalRead(b1) == LOW && b1Stat == 1){
    b1Stat--;
    on--;
    delay(90);
  }
  
  
  if(digitalRead(b2) == HIGH && b2Stat == 0){
    b2Stat++;
    on2++;
    delay(90);
  }
  if(digitalRead(b2) == LOW && b2Stat == 1){
    b2Stat--;
    on2--;
    delay(90);
  }
  //--------------------Operation:---------------------------
  if(on == 1 && on2 == 1 && did == 0){ // Start engine, IGN on.
    digitalWrite(acc, HIGH);
    digitalWrite(ign, LOW); //Write LOW to activate relay.
    digitalWrite(st, LOW);
    did = 1;
    did2 = 0;
    did3 = 1;
    ledOn = 1;
  }
  
  if(on == 0 && did == 1){ //Starter off.
    digitalWrite(st, HIGH);
    digitalWrite(onLed, LOW);
    did = 2;
  }
  
  if(did == 2 && on == 0){ //ACC on after engine start.
    digitalWrite(acc, LOW);
    digitalWrite(accLed, HIGH);
    did = 3;
  }
  
  if(did == 3 && on == 1){ //Turn everything off.
    digitalWrite(ign, HIGH);
    digitalWrite(acc, HIGH);
    digitalWrite(onLed, HIGH);
    digitalWrite(accLed, HIGH);
    did = 4;
    ledOn = 0;
    time2 = 0;
  }
  
  if(did == 4 && on == 0){
    did = 0;
    did2 = 0;
    did3 = 0;
  }
  //-----------------NON STARTER OPERATION--------------------
  
  if(on == 1 && did2 == 0 && did3 == 0){ //Push for ACC
    digitalWrite(acc, LOW);
    digitalWrite(accLed, LOW);
    did2 = 1;
    ledOn = 1;
  }
  
  if(on == 0 && did2 == 1 && did3 == 0){
    did2 = 2;
  }
  
  if(did2 == 2 && on == 1 && on2 == 0){ //IGN without start
    digitalWrite(ign, LOW);
    digitalWrite(onLed, LOW);
    digitalWrite(accLed, HIGH);
    did2 = 3;
  }
  
  if(on == 0 && did2 == 3 && did3 == 0){
  did2 = 4;
  }
  
  if(did2 == 4 && on == 1 && on2 == 0){ //ALL SYSTEMS OFF.
    digitalWrite(ign, HIGH);
    digitalWrite(acc, HIGH);
    digitalWrite(accLed, HIGH);
    digitalWrite(onLed, HIGH);
    did2 = 5;
    ledOn = 0;
    time2 = 0;
  }
  
  if(did2 == 5 && on == 0){
    did2 = 0;
  }
  //-----------------LED OUTPUT/BUTTON FLASH/illum.-------------------
  if(ledOn == 1 && time2 == 0){
  digitalWrite(ledPin, LOW);
  time2 = 1;
  }else if(millis() - time > 5000 && ledOn == 0 && b2Stat == 0){
    digitalWrite(ledPin, LOW);
    time = millis();
    ledFlash = 1;
  }
  if(millis() - time > 100 && ledFlash == 1 && ledOn == 0 && b2Stat == 0){
    digitalWrite(ledPin, HIGH);
    time = millis();
    ledFlash = 0;
  }
  if(b2Stat == 1 && b1Stat == 0 && ledOn == 0){
    digitalWrite(ledPin, LOW);
    delay(50);
    digitalWrite(ledPin, HIGH);
    delay(60);
  }
}

after few second let say when ends very it turns off.

Sorry, but I am not clear what you mean.

Add some Serial.print()s so that you can see which code is being executed and the value of relevant variables at various points.

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long to study quickly without copying to a text editor.

...R

Got it.

So anyone understand what I want???

azhar49:
Got it.

So anyone understand what I want???

No, you never made it clear. You never said what the "it" was that you want to stay on.

Ok what i want is that...

Car radio and other electronic to stay on .... it stay only for a minute or so.

azhar49:
So anyone understand what I want???

Car radio and other electronic to stay on .... it stay only for a minute or so

At the top of your program it says "//Push button ignition switch for automobiles."

What has that got to do with a radio ?

...R

Ok push-button start should do yhe following;

  • 1st press accessories ( radioand other acc) turn on.

  • 2nd press ignition turns on
    If press again everything off

  • 3rd press with clutch switch engine starts
    And if press again everything off

arduino controls a relay module... which connected to car battery

works fine but after a moment every thing turn off by it self...

How are your switches wired? You are not using the internal pullup resistor, so you need an external resistor - either pullup or pulldown.

I'm guess you don't have one, so your switch pin is floating when the switch is not pressed.

There were some wire not in right pin I correct that

The code and your diagram has two button switches. But your description in Reply #7 only refers to one switch.

I think you need to write down the succession of states clearly in plain language, not code. It should then be very straightforward to write code to do that.

Have a look at Planning and Implementing a Program

Your description in Reply #7 does not seem to allow for the accessories to be ON and later OFF without turning the iginition on.

Using meaningful names for variables, functions and I/O pins makes a program easier to understand.

You should not use delay() anywhere in the program. Use millis() to manage time without blocking as illustrated in the above link and in Several Things at a Time

...R

Question: is it possible if I have the relay module is power from arduino that's what make everything turned off???

And you are right about accessories not stay on after the ignition is off....

Can help rectify the code

Everything work fine except if I could turn off ignition (engine ) and not the accessories and then turn off everything...

Post a link to the datasheet for the relay module. The coil of a relay should not be powered from an Arduino I/O pin.

What about paragraph 1 of Reply #10 ?

The comment I made in paragraph 4 is not addressing the same thing as your comment "you are right about accessories not stay on after the ignition is off".

If you do not have a clear description of the problem it will be difficult to find a solution and even more difficult for me who knows nothing about your system other than what you tell me.

...R

Robin2:
The code and your diagram has two button switches. But your description in Reply #7 only refers to one switch.

I think OP is only referring to the NON-START operation where the clutch is not pressed.

azhar49:
Everything work fine except if I could turn off ignition (engine ) and not the accessories and then turn off everything...

The above does not match

azhar49:
Car radio and other electronic to stay on .... it stay only for a minute or so.

Nowhere in your code is a delay of 'a minute or so'; only 5 seconds and 100 milliseconds for the flashing and sub-hundred delays.

OK, if your code is working fine, where do you want the changes? In the 'normal operation' (start the engine) or in the 'non-start operation.

Both use a statemachine so you can easily add /remove functionality to / from either of them.