how do i run the programme from a digital input? help please

hello,
could anyone help me out (novice) with my sketch i have that i'm using to turn on some relays, all quite basic stuff i know but i'm still learning,

what i need is for the sketch to only start when it sees 12 volts coming in on a digital input, im already using pins 2 to 8 so i will use pin 9 as the input,
is this the best way to do what i want or is there a better way?
also the sketch should only run once then stop until it sees the input to start it again?

any help is very much appreciated
thanks

  int relay1 = 2;
  int relay2 = 3;
  int relay3 = 4;
  int relay4 = 5;
  int relay5 = 6;
  int relay6 = 7;
  int relay7 = 8;
  
void setup() {
  pinMode (relay1,OUTPUT);
  pinMode (relay2,OUTPUT);
  pinMode (relay3,OUTPUT);
  pinMode (relay4,OUTPUT);
  pinMode (relay5,OUTPUT);
  pinMode (relay6,OUTPUT);
  pinMode (relay7,OUTPUT);
}
 
void loop() {
  // put your main code here, to run repeatedly:
digitalWrite (relay1, HIGH);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, HIGH);
digitalWrite (relay5, HIGH);
digitalWrite (relay6, HIGH);
digitalWrite (relay7, HIGH);

// flash all lights for 80 miliseconds to test for short circuit
delay (12000);
digitalWrite (relay1, LOW);
delay (80);
digitalWrite (relay1, HIGH);
delay (80);
digitalWrite (relay2, LOW);
delay (80);
digitalWrite (relay2, HIGH);
delay (80);
digitalWrite (relay3, LOW);
delay (80);
digitalWrite (relay3, HIGH);
delay (80);
digitalWrite (relay4, LOW);
delay (80);
digitalWrite (relay4, HIGH);
delay (80);
digitalWrite (relay5, LOW);
delay (80);
digitalWrite (relay5, HIGH);
delay (80);
digitalWrite (relay6, LOW);
delay (80);
digitalWrite (relay6, HIGH);
delay (80);
digitalWrite (relay7, LOW);
delay (80);
digitalWrite (relay7, HIGH);
delay (3000);

what i need is for the sketch to only start when it sees 12 volts coming in on a digital input

Use a smoke-detector.

bobmax1:
when it sees 12 volts coming in on a digital input

.... the pin and or the Arduino will be damaged.

As stated above: don't apply 12V to any pin!

//pseudo code
void loop() {
     if (button pushed) {
        bool buttonPushed =1;
     }

     if (buttonPushed) {
         do the relay thing;
         buttonPushed = 0;
     }
}

I think even pseudocode should at least nod in the general direction of scope rules.

You will need to reduce the 12V signal to less than the Vcc of your Arduino or you will damage the Arduino. The value of Vcc depends on the model of Arduino that you are using (5V on an Uno). A voltage divider is capable of doing that. To run the program only once on a button push, look at the state change detection example in the IDE examples (File, Examples, Digital). You can set it up so that the program runs only when the button becomes pressed (as opposed to when it is pressed).

AWOL:
I think even pseudocode should at least nod in the general direction of scope rules.

Quite right. My bad. What I get for sneaking in a check of the forums at work.

//pseudo code
void loop() {
     bool buttonPushed;
     if (button pushed) {
        buttonPushed =1;
     }

     if (buttonPushed) {
         do the relay thing;
         buttonPushed = 0;
     }
}

ok , thanks, i thought after writing the first post i cant use 12 volts in :o , i can drop that to 5 volts though,

the input is not going to be coming from a button as such but i guess the arduino wont care where its coming from as long as it sees 5 volts in it will start the sketch?
thanks

The idea of the state change is to record the state of the input, then the next time through loop() check to see if the state is different. It is not necessarily when it is 5V, but the change from not 5V to 5V.

thanks for the replies,
so can anyone show me what to actually write at the start of the code?
as i said im a novice but we all got to start somewhere right!
thanks

OK, this may get you started. Add in your code where appropriate.

// this constant won't change:
const int  inputPin = 9;    // the pin that the input voltage is attached to


// Variables will change:
int inputState = 0;         // current state of the input
int lastInputState = 0;     // previous state of the input

void setup()
{
  // initialize the input pin as a input: (not really necessary as the default is INPUT
  pinMode(inputPin, INPUT);
  Serial.begin(9600);
}


void loop()
{
  // read the input pin:
  inputState = digitalRead(inputPin);

  // compare the inputState to its previous state (lastInputState)
  if (inputState != lastInputState)  
  {
    if (inputState == HIGH)
    {
      // if the current state is HIGH then the input
      // went from not high to high:
      Serial.println("Went high");

      // *****************************************
      // do the stuff that a HIGH input triggers
      // *****************************************

    } 
     else 
    {
      // if the current state is LOW then the input
      // went from high to not high: do nothing except print something if you want.
      Serial.println("Went low");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastInputState = inputState;
}

I've saved groundfungus' sketch as a state-change template, very handy, thanks.

Only thing I don't see discussed here is the possible need for a pulldown resistor. I don't think OP said anything about the input being guaranteed low if it's not known high; if not, a pulldown would be needed I reckon.

manor_royal, that is just the example from the IDE with the button count removed and button changed to input. I really wish that that example could be updated to use the internal pullup and active low like we do in the real world.

And I agree that we need to know more about the 12V signal to be sure that it is read properly.

groundfungus:
manor_royal, that is just the example from the IDE with the button count removed and button changed to input.

Yep I know, bit just very handy to have it cleaned out of the count stuff.