Simple button release/depress code.

I'm working on something really simple. I want to change the button example in Arduino so that the light will blink a certain amount per second when depressed and a different amount when it is released. How would I go about doing this? This is what I have but I won't be able to test it until tomorrow.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);
}

void loop() {
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
 if (buttonState == HIGH) {
   // turn LED on:
   digitalWrite(ledPin, HIGH);
   delay(1000)
 } else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
   delay(500)
 }
}

Do the testing and come back. I can't make that test.

so that the light will blink a certain amount per second

In the code you have now, what determines how fast the light blinks? Could you make that a variable instead of a hard coded number? Can you write an if statement based on buttonState that changes that variable?

Please read the "How to use this forum" post at the top of any of the boards. Especially the part about how to properly post code.

I think pseudocode for what I have to do is as follows.

if (buttonState == HIGH){
blink every 1 second;
}
else {
blink every 2 seconds;

Or alternatively

do {
blink every 1 second;
}
while {buttonState == HIGH)

do{

blink every 2 seconds;
}
while {buttonState == LOW)

I'm not sure how I would tell it to blink every 1 or 2 seconds though.

You'll probably want to read up on and incorporate Blink Without Delay. Once you put a little time into understanding what's happening, it's really not difficult at all.

I think I figured it out. This is what I have now

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void blink (int waitTime)
{
  //turn LED on:
  digitalWrite(ledPin, HIGH);
  delay(waitTime);
  //turn LED off:
  digitalWrite(ledPin, LOW);
  delay(waitTime);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // blink 1 per second
    blink(1000);
  } else {
    // blink twice a second
    blink(500);
  }
} //end loop

Ablution:
I think pseudocode for what I have to do is as follows.

if (buttonState == HIGH){
blink every 1 second;
}
else {
blink every 2 seconds;

Or alternatively

do {
blink every 1 second;
}
while {buttonState == HIGH)

do{

blink every 2 seconds;
}
while {buttonState == LOW)

I’m not sure how I would tell it to blink every 1 or 2 seconds though.

No. Separate the decision on how long to blink from the blinking.

And please do as I asked and read the “How to use this forum” thread. Help dries up pretty quick if you keep ignoring that.

I think something like this is what you want.

if (button is pressed) {
    blinkInterval = 1000;
}
else {
    blinkInterval = 2000;
}


// if it has been more than blinkInterval milliseconds since the last toggle

if(millis() - lastBlinkTime >= blinkInterval){
      //toggle the light here
      lastBlinkTime =  millis();
}

Don’t think of your loop as running once through the story of what your code does. Think of your loop function as a high speed checklist running over and over thousands of times per second and occasionally seeing that it is time to do something.