Go Down

Topic: Simple button release/depress code.  (Read 251 times) previous topic - next topic

Ablution

Sep 16, 2019, 11:13 pm Last Edit: Sep 17, 2019, 01:34 am by Ablution
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.

Code: [Select]
// 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)
 }
}


Railroader

Do the testing and come back. I can't make that test.
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

Delta_G

#2
Sep 17, 2019, 01:01 am Last Edit: Sep 17, 2019, 01:02 am by Delta_G
Quote
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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

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.

DangerToMyself

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.

Ablution

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

Code: [Select]
// 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

Delta_G

#6
Sep 17, 2019, 03:40 am Last Edit: Sep 17, 2019, 03:41 am by Delta_G
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.
Code: [Select]


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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up