timing within a loop

hi there
i have a code that runs when an input is high which obviously runs over and over continously as long as the button is high but the first half of the code i only want to run once but obviously if the button goes low then high again it will reset and will obviously do the first half again once?

thanks Joe

Obviously, you need to post the code.

i have written in the code which bit i want to run once

void loop() {
     int val = analogRead(ionisation);
   //--------------------------------------------------------------------
   
    if (digitalRead(heatbutton)==      HIGH) {
    //                                                                                                         ** from here need's to just run once **
    if (digitalRead(ionisation_in)==   HIGH) {       // if ionisation swith HIGH 
    if (val >900)  {
    digitalWrite(solenoid,              LOW);                                       
   digitalWrite(lockoutled,             HIGH); 
  digitalWrite(heatbutton_feed,         LOW); 
   } 
 }   
  else {
  if (digitalRead(photo_pressure)==     HIGH) {         // if photo swith HIGH 
   digitalWrite(lockoutled,             HIGH);                                        
   digitalWrite(solenoid,               LOW); 
  digitalWrite(heatbutton_feed,         LOW); 
   } 
 } 
  digitalWrite(relay230_24,            HIGH);        // 230V out put to ignition
  digitalWrite(fan,                    HIGH); 
  digitalWrite(ignition_fan_2,         HIGH); 
  delay(2000);        
    //                                                                                                         ** to this point **
  //---------------------------------------------------------------------
  if (digitalRead(saleswitch)==HIGH) {           // sale switch if HIGH
  digitalWrite(solenoid,       HIGH); }
  //---------------------------------------------------------------------
  else {                                            // sale switch if LOW
   digitalWrite(lockoutled,      HIGH); 
   digitalWrite(ignition_fan_2,  LOW);
   digitalWrite(solenoid,        LOW);
   digitalWrite(fan,             LOW); 
   digitalWrite(heatbutton_feed, LOW);
   }
  //---------------------------------------------------------------------
  delay(2000);
   if (digitalRead(ionisation_in)== HIGH) {
  if (val >900)                         {            // ionisation if LOW
   digitalWrite(solenoid,          LOW);
   digitalWrite(ignition_fan_2,    LOW);
   digitalWrite(lockoutled,        HIGH); 
   digitalWrite(heatbutton_feed,   LOW);
   digitalWrite(heatbutton_feed,   LOW);
   } 
 }
   
   if (digitalRead(photo_pressure)==LOW)  {          // photo if LOW
     digitalWrite(solenoid,         LOW);
   digitalWrite(ignition_fan_2,    LOW);
   digitalWrite(lockoutled,        HIGH); 
   digitalWrite(heatbutton_feed,   LOW);
   }
   //---------------------------------------------------------------------
   else {
       digitalWrite(ignition_fan_2, LOW); }
   //---------------------------------------------------------------------
   }
   else {                                          // switching off
        digitalWrite(solenoid,       LOW);
        digitalWrite(ignition_fan_2, LOW);
         digitalWrite(relay230_24,   LOW);
        delay(30000);
        digitalWrite(fan,            LOW); }
   //---------------------------------------------------------------------
    
       }

I'd like to suggest you pass that through the IDE's auto format tool, and re post.

Could I also suggest that you post ALL the code, for a proper evaluation of what you have done. Don't leave us guessing.

hope this is beter

const int heatbutton     = 2 ;
const int ignition_fan_2 = 10;
const int saleswitch     = 4 ;
const int solenoid       = 12;
const int photo_pressure = 3 ;
const int lockoutled     = 8 ;
const int heatbutton_feed= 5 ;
const int fan            = 9 ;
const int relay230_24    = 11;
const int ionisation_in  = 7 ;
int ionisation     = 5 ;   

void setup()
{
  pinMode(heatbutton,       INPUT );    
  pinMode(ignition_fan_2,   OUTPUT);
  pinMode(saleswitch,       INPUT );
  pinMode(solenoid,         OUTPUT);
  pinMode(photo_pressure,   INPUT );
  pinMode(lockoutled,       OUTPUT);
  pinMode(heatbutton_feed,  OUTPUT);
  pinMode(fan,              OUTPUT);
  pinMode(relay230_24,      OUTPUT);
  pinMode(ionisation_in,    INPUT );
  //------------------------------------------- 
  digitalWrite(heatbutton_feed, HIGH);
}

void loop() {
  int val = analogRead(ionisation);
  //--------------------------------------------------------------------

  if (digitalRead(heatbutton)==        HIGH) {
    //                                                  ** from here need's to just run once **

    if (digitalRead(ionisation_in)==   HIGH) {       // if ionisation swith HIGH 
      if (val >900)  {
        digitalWrite(solenoid,              LOW);                                       
        digitalWrite(lockoutled,            HIGH); 
        digitalWrite(heatbutton_feed,       LOW); 
      } 
    }   
    else {
      if (digitalRead(photo_pressure)==     HIGH) {         // if photo swith HIGH 
        digitalWrite(lockoutled,            HIGH);                                        
        digitalWrite(solenoid,              LOW); 
        digitalWrite(heatbutton_feed,       LOW); 
      } 
    } 
    digitalWrite(relay230_24,            HIGH);        // 230V out put to ignition
    digitalWrite(fan,                    HIGH); 
    digitalWrite(ignition_fan_2,         HIGH); 
    delay(2000);        
    //                                                           ** to this point **

    //---------------------------------------------------------------------
    if (digitalRead(saleswitch)==HIGH) {           // sale switch if HIGH
      digitalWrite(solenoid,     HIGH); 
    }
    //---------------------------------------------------------------------
    else {                                            // sale switch if LOW
      digitalWrite(lockoutled,      HIGH); 
      digitalWrite(ignition_fan_2,  LOW);
      digitalWrite(solenoid,        LOW);
      digitalWrite(fan,             LOW); 
      digitalWrite(heatbutton_feed, LOW);
    }
    //---------------------------------------------------------------------
    delay(2000);
    if (digitalRead(ionisation_in)== HIGH) {
      if (val >900)                         {            // ionisation if LOW
        digitalWrite(solenoid,        LOW);
        digitalWrite(ignition_fan_2,  LOW);
        digitalWrite(lockoutled,      HIGH); 
        digitalWrite(heatbutton_feed, LOW);
        digitalWrite(heatbutton_feed, LOW);
      } 
    }

    if (digitalRead(photo_pressure)==LOW)  {          // photo if LOW
      digitalWrite(solenoid,         LOW);
      digitalWrite(ignition_fan_2,   LOW);
      digitalWrite(lockoutled,       HIGH); 
      digitalWrite(heatbutton_feed,  LOW);
    }
    //---------------------------------------------------------------------
    else {
      digitalWrite(ignition_fan_2, LOW); 
    }
    //---------------------------------------------------------------------
  }
  else {                                          // switching off
    digitalWrite(solenoid,       LOW);
    digitalWrite(ignition_fan_2, LOW);
    digitalWrite(relay230_24,    LOW);
    delay(30000);
    digitalWrite(fan,            LOW); 
  }
  //---------------------------------------------------------------------

}

the first half of the code i only want to run once

Define "half"'

If there's code you only want to run once then put it in the setup function.

hi yes i would have put it in setup but i need it to do it the first revolution everytime when the heat button goes high. obviously it resets when it goes low

Then it sounds like you need to keep the previous result of digitalRead(heatbutton) in a variable and run your "run once" code when you see a transition from low to high.

ok
do we have an example of this?

do we have an example of this?

Yes.

Search the forums for prevButtonState. The first link http://arduino.cc/forum/index.php/topic,44431.0.html has an example, but there will be many others.

Joes:
ok
do we have an example of this?

What you're looking for is called edge detection. Here is an example of such using a button:

/*

  • Example for "Edge Detection"
  • Edge detection refers to determining the point at which a digital signal moves
  • from low to high or high to low. Low to high is refered to as the 'positive' or
  • rising edge. High to low is the 'negative or falling' edge. This example uses a
  • button to run a commands at each edge, identifying the edge. Edges occur on a
  • a button when it is either pushed or released. If the input pin is pulled high,
  • pushing the button down wil be the falling edge, while releasing it will be th
  • rising edge. If the input pin is pulled high, the opposite will be true.
  • NOTE: This example does not utilize a debounce, so it's entirely possible that
  • you'll see multiple prints if using a device, such as a button, that bounces
  • during signal change.
    */

// Pin used for our digital signal
const short inputPin = 5;

void setup() {
Serial.begin(57600);
Serial.println("[EdgeDetection]");

pinMode(inputPin, INPUT); // Set the pin as input
digitalWrite(inputPin, HIGH); // enable pull-up resistor. (Pulled high)

}

void loop() {
/* Keep track of the last state of the button. A static variable will

  • only be initialized the first time and will hold it's value through
  • each loop. */
    static short lastState = HIGH;
    // Get the current state of the pin
    short currentState = digitalRead(inputPin);

// Check for the falling edge
if ( (currentState==HIGH) && (lastState==LOW) ) {
fallingEdge();
}
// Check for rising edge
if ( (currentState==LOW) && (lastState==HIGH) ) {
risingEdge();
}

lastState = currentState;
}

void fallingEdge() {
Serial.println("Falling edge");
}

void risingEdge() {
Serial.println("Rising edge");
}

hi there think i have figured it now

int ledPin =  13;    // LED connected to digital pin 13
int button = 2;      // button pin

int count = 0;       // Our blink counter

// The setup() method runs once, when the sketch starts

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT); 
  pinMode(button, INPUT);  
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                    
{
  if (digitalRead(button)== HIGH)  // buttin when high
  {
  if (count < 1) {
    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(1000);                  // wait for a second
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(1000);                   // wait for a second
    
    count++;  // add one (1) to our count
  }
  }
  else {
    if(count == 1) count =0;       // resetting the count 
  } 
}

one more thing, (just to make it a bit simpler, base this on button examle) when the button pin goes low there would be a delay of upto 30seconds before the LED goes low, but if the button is only on for a period of 10seconds then the LED will only stay on after the button goes low for 10seconds. hope you understand what i mean there

also the delay time needs to be without a delay like the example blink without delay.

thanks joe

Looks like.

Having said that

if(count == 1) count =0;       // resetting the count

Why check count? You can unconditionally set it zero (either it was already zero, or it's one and you want it to be zero)

Also, given that count can only take two values, shouldn't it be a boolean?

Joes:
one more thing, (just to make it a bit simpler, base this on button examle) when the button pin goes low there would be a delay of upto 30seconds before the LED goes low, but if the button is only on for a period of 10seconds then the LED will only stay on after the button goes low for 10seconds. hope you understand what i mean there

also the delay time needs to be without a delay like the example blink without delay.

thanks joe

I don't understand, was there a question?

Looks like.

Having said that

Code:

if(count == 1) count =0; // resetting the count

Why check count? You can unconditionally set it zero (either it was already zero, or it's one and you want it to be zero)

Also, given that count can only take two values, shouldn't it be a boolean?

honestly i havent got a clue show me what you mean

I don't understand, was there a question?

yes sorry finding it hard to explain basically when the switch is turned off theres to be a delay before the LED goes off but the delay depends on how long the input was high for so if the button was on for 10s the light will stop on after the button goes low for 10s
but the delay rime can only go upto 30s
i hope ive worded that a bit better

thanks all for your help

int ledPin =  13;    // LED connected to digital pin 13
int button = 2;      // button pin

bool DoneInitialization = false;       // Our blink indicator

void setup()   
{                
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT); 
pinMode(button, INPUT);  
}

void loop()                    
{
if (digitalRead(button)== HIGH)  // buttin when high
  {
  delay(10); //debounce
  if (!DoneInitialization) 
    {
    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(1000);                  // wait for a second
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(1000);                  // wait for a second
    DoneInitialization=true;      // Initialization is done, don't run it again until button has been low
    }
  }
else 
  {
  DoneInitialization=false;       // button was low. Permit Initialization (or whatever it is) to run again 
  } 
}