Go Down

Topic: Code help, I need a button to do two things when pressed for different times. (Read 368 times) previous topic - next topic

Hey Everyone,
Im NEW to the arduino and writing code(no experience). I have a Arduino Uno R3 and am trying to make a circuit but can get the code right. I need some help to see it this is even possible to do.

Description:
1st- I have a momentary button connected to pin 8 (there is a pull down resistor on it to keep it low when not pressed), when the switch is pushed for LESS than 4 sec I need the LED (connected to pin 13)  to stay on for 8 sec then turn off and wait for the next button push.

2nd- If the button is pushed for more than 4 sec I need the LED to blink on and off for a total of 8 sec.( blinks 1 sec on and 1 sec off)

I have the code to turn the light on when pressed but thats about it I cant find were I can make it since how long the button is pressed I really need help( code below). I cant find a code to help do what i am trying to do pleas give me all the feedback you can pleas be descriptive if you can i have no background in C++ or code and am learning as i go. If you can it would be great to get some sample code of what you are describing thanks so so much in advance .

Code: [Select]

int switchPin = 8;
int ledPin = 13;

void setup()
{
 pinMode(switchPin, INPUT);
 pinMode(ledPin, OUTPUT);
}

void loop()
{
 if (digitalRead(switchPin) == HIGH)
 {
   digitalWrite(ledPin, HIGH);
 }
 else
 {
   digitalWrite(ledPin, LOW);
 }
}
I'm new to this so the more help the better

Grumpy_Mike

You can only ever do anything after 4 seconds or when the button is released.
So when the button is pressed make a note of the current time given by the millis() function.
Keep checking for two things, a button release or millis() being 4 seconds bigger than when the button went down.

For code examples see the blink without delay in the IDE.

skyjumper

I needed to do something similar, although a bit more complex. The code pasted below should help you out.

Code: [Select]

#include "Pulser.h"

Pulser::Pulser(uint8_t pin, unsigned long debInt) : Bounce(pin, debInt)
{
  // Initialize private members
  shortPressCount = 0;
  flag = false;

  SHORT_PRESS_INTERVAL = 500;
  LONG_PRESS_DURATION = 1000;
 
  // Set up the input pin
  pinMode(pin, INPUT);
 
  //digitalWrite(pin, HIGH); // Activate internal Pull-Up
  digitalWrite(pin, LOW); // Deactivate internal Pull-Up
}

Pulser::~Pulser(){}

short Pulser::get(void)
{
  // Get the current status of the button. This needs to be done
  // very frequently, so every loop iteration 
  update();

  // Read the button condition and return the state
  if(!read()) // If button is pressed
  {
    // Note the last time the button was pressed
    lastPressMs = millis();
   
    // Record the duration of this press
    dur = duration();

    // Flag that we have an unread pressed button
    flag = true;
   
    // Return 0, since we don't know anything about this press
    // until the user releases it and we look to see what other
    // presses happened recently
    return 0;
  }

  // Button is not currently pressed. Check duration of last press
  if(dur > LONG_PRESS_DURATION)
  {
    // Long press - clear the short press counters since this cancelles
    // the short press history
    if(flag)
    {
      shortPressCount = 0; // clear the short presses counter

      flag = false; // clear the we-have-a-press-to-read flag
      return 0x10; // return the state for a long press
    } 
  }
 
  // It was not a long press, so we look for a short press series

  // Was there a press within the last 1/4 second?
  // If not, clear the counter and return the number
  // of presses in the series. If so, record the press
  // and return 0, waiting for the next press of the series
  // or the user to be done pressing.
  if(millis() - lastPressMs < SHORT_PRESS_INTERVAL)
  {
    // We are still in a series of presses
    if(flag) // do we have a press to read?
    {
      shortPressCount++;
      flag = false;
    }
    return 0; 
  }
  else
  {
    // No press within the last 1/4 second. we are done with
    // this series of short presses. Or, there is no current series
    // which is effectively a series of 0 presses
    short temp = shortPressCount;
    shortPressCount = 0;
    flag = false;
    return temp;
  } 




Code: [Select]

#include "Pulser.h"

Pulser::Pulser(uint8_t pin, unsigned long debInt) : Bounce(pin, debInt)
{
  // Initialize private members
  shortPressCount = 0;
  flag = false;

  SHORT_PRESS_INTERVAL = 500;
  LONG_PRESS_DURATION = 1000;
 
  // Set up the input pin
  pinMode(pin, INPUT);
 
  //digitalWrite(pin, HIGH); // Activate internal Pull-Up
  digitalWrite(pin, LOW); // Deactivate internal Pull-Up
}

Pulser::~Pulser(){}

short Pulser::get(void)
{
  // Get the current status of the button. This needs to be done
  // very frequently, so every loop iteration 
  update();

  // Read the button condition and return the state
  if(!read()) // If button is pressed
  {
    // Note the last time the button was pressed
    lastPressMs = millis();
   
    // Record the duration of this press
    dur = duration();

    // Flag that we have an unread pressed button
    flag = true;
   
    // Return 0, since we don't know anything about this press
    // until the user releases it and we look to see what other
    // presses happened recently
    return 0;
  }

  // Button is not currently pressed. Check duration of last press
  if(dur > LONG_PRESS_DURATION)
  {
    // Long press - clear the short press counters since this cancelles
    // the short press history
    if(flag)
    {
      shortPressCount = 0; // clear the short presses counter

      flag = false; // clear the we-have-a-press-to-read flag
      return 0x10; // return the state for a long press
    } 
  }
 
  // It was not a long press, so we look for a short press series

  // Was there a press within the last 1/4 second?
  // If not, clear the counter and return the number
  // of presses in the series. If so, record the press
  // and return 0, waiting for the next press of the series
  // or the user to be done pressing.
  if(millis() - lastPressMs < SHORT_PRESS_INTERVAL)
  {
    // We are still in a series of presses
    if(flag) // do we have a press to read?
    {
      shortPressCount++;
      flag = false;
    }
    return 0; 
  }
  else
  {
    // No press within the last 1/4 second. we are done with
    // this series of short presses. Or, there is no current series
    // which is effectively a series of 0 presses
    short temp = shortPressCount;
    shortPressCount = 0;
    flag = false;
    return temp;
  } 



Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy