Multiple pin state to trigger pin to high

Hi,
I am new to all this sketch writing. I am hoping someone cold help me out on my first sketch. I have been playing around with the basic "Button" sketch and wanted to make the led pin high after the "buttonState" pin has got from low to high and back to low again and i cant seem to find any info on this at all.
Thanks
Tom

GeekShack:
I am new to all this sketch writing.

It's called coding or writing code :wink: A sketch might seem less daunting but you're really coding! :wink:

GeekShack:
from low to high and back to low again

It's called state change detection. Open that example :wink:

If the button pin is HIGH, set a variable, i.e. ButtonHigh = TRUE
If the button pin is LOW and ButtonHigh == TRUE, button just went low. Do whatever needs to be done. Set ButtonHigh = LOW

septillion:
It's called coding or writing code :wink: A sketch might seem less daunting but you're really coding! :wink:
It's called state change detection. Open that example :wink:

Thanks have its sorted now. Is there a way of changing a delay (x) by seting a pin to high?
I.e
if pin3 == HIGH delay (5000)
else if pin 4 == HIGH delay blaa blaa
in the below code

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
 // initialize the button pin as a input:
 pinMode(buttonPin, INPUT);
 // initialize the LED as an output:
 pinMode(ledPin, OUTPUT);
 // initialize serial communication:
 Serial.begin(9600);
}
 
void loop() { // read the pushbutton input pin:
 buttonState = digitalRead(buttonPin);

 // compare the buttonState to its previous state
 if (buttonState != lastButtonState) {
   // if the state has changed, increment the counter
   if (buttonState == LOW) {
     // if the current state is HIGH then the button
     // wend from off to on:
     buttonPushCounter++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(buttonPushCounter);
   }
   else {
     // if the current state is LOW then the button
     // wend from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,
 //for next time through the loop
 lastButtonState = buttonState;


 // turns on the LED every four button pushes by
 // checking the modulo of the button push counter.
 // the modulo function gives you the remainder of
 // the division of two numbers:
 if (buttonPushCounter % 2 == 0)
 {
   digitalWrite(ledPin, LOW);
  
 } else {
   // time before purge
   delay (3000);
   digitalWrite(ledPin, HIGH);
   // time purges for
   delay (3000);
   digitalWrite(ledPin, LOW);
   exit(0);
 }
 
 }

Yeah, why not. Don't see the problem.

But please edit your post and add code tags. It's nearly impossible to read now.

But the thing I can read, exit(0), really? There is no need for you as a newbie to call exit()...

And keep in mind, delay() stops EVERYTHING. It's blocking the program. And blocking functions are hard to work with. So the second chapter in the Arduino howto should really be "Never ever use delay() again"... Have a look at Blink without delay :wink:

Hi,
I will take a look at the blink without delays, thanks for the advice.
I used the end() because it kept blinking every three seconds and I only need it to go once.
What do the code tags look like?

How to post your code in the Arduino forum:

See item nr. 7 in this link;
http://forum.arduino.cc/index.php/topic,148850.msg1118324.html#post_codetags
Or:
After you have typed your message, start the Arduino IDE if not already running and load or type in your code.
Right click in the editor window then left click [Select All].
Right click again then left click [Copy].
And, in the reply page, type:

[code]

Type or
paste your
code here
[/code]

Thanks

GeekShack:
I used the end() because it kept blinking every three seconds and I only need it to go once.

Then:
OR stop doing it aka record you did itand then don't. That's you programming
OR Place it in setup because that's only run once

GeekShack:
What do the code tags look like?

That's why there is a big fat sticky at the top of every board called How to use the forum. :wink:

Then:
OR stop doing it aka record you did itand then don't. That's you programming
OR Place it in setup because that's only run once

i am not sure how to put it in setup, i tried by it did nothing.
I would still like it to start again when the button changes state,

I am now trying to use a Loop(); to call it back to the start of the loop.

*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}
  
void loop() { // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0)
  {
    digitalWrite(ledPin, LOW);
   
  } else {
    // time before purge
    delay (3000);
    digitalWrite(ledPin, HIGH);
    // time purges for
    delay (3000);
    digitalWrite(ledPin, LOW);
    loop(); 
  }
  
  }

This does not work, i can ether get it LedPin to blink once for 3 sec and not be triggered again or come on and blink for 3 sec and not stop. I would like it to trigger the Ledpin after x secs for x sec once everything the Buttonstate is triggered. I will keep trying

Loop() will already loop, no need to call it. And it will even give you weird recursion if you do…

But really, why count if you just want to detect when a button is released? Why make it so much more complicated than he state change example I suggested?