Re: Delayed event with arduino?

Have a look at the BlinkWithDelay example. It uses the millis() function to get "now" and uses "now" to determine when the turn the LED on or off next.

Think it was the blink without delay PaulS ment

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
  The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 17 Jun 2009
 by Tom Igoe
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, is the difference
  // between the current time and last time we blinked the LED bigger than
  // the interval at which we want to blink the LED.
  if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

@Pasky
Somehow the "out" slipped out while I wasn't looking. :wink:

Another question I had about button presses is how can I tell the Arduino to keep a button held down for a specified amount of time?

The Arduino can't hold a button down, that's what your finger is for :slight_smile:

I assume what you mean is something like, remember for a little while that a button was pressed?

const int HoldTime = 500; // remember button press for 500 mSec
const int buttonPin = 9;  // jst some pin, you choose
int button = LOW;
unsigned long buttonHoldTime = 0;

void setup() { 
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  button = digitalRead(buttonPin);
  if (button == HIGH) {
      buttonHoldTime = millis() + HoldTime;   // remember when to release
  }
  
  // ...
  if ( buttonHoldTime > millis()) { 
      // do thing that needs to happen soon after button press
  }
}

WARNING - This code is Verify'ed but not tested

For example, lets say we hooked the Arduino up to a keyboard and I want the 'ALT' key to always be pressed by the arduino

So who is doing the pressing and releasing?
You or the Arduino?

Are you describing something like the CAPS LOCK button on a keyboard where when the human presses, it toggles between two states?

There are still things which are unclear:

  1. What happens when the 'ALT' is pressed or being 'held'?
  2. Does that have to continue happening while it is pressed?
  3. What happens when the hold time is complete?
  4. What happens if the 'ALT' key is pressed again before the hold time has completed?

Anyway, here is the basic idea of carrying out a action when the button is pressed, and then automatically detecting when the hold time has elapsed

const unsigned int HoldTime = 5000; // remember button press for 5 Sec
const int buttonPin = 9;
int button = LOW;
unsigned long buttonHoldTime = 0L;

int buttonState = false;

void setup() { 
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  button = digitalRead(buttonPin);
  if (button == HIGH) {
      buttonState = true;
      buttonHoldTime = millis() + HoldTime;    // remember when to release
      // do the thing that needs to happen when the button is pressed
  }
  
  // Time to release the button?
  if ((buttonState == true) && (millis() < buttonHoldTime)) {
       buttonState = false; 
      // do thing that needs to happen when the button is automatically released
  }
}

So, if you know what happens when the button is first pressed, and then the hold time expires, that it should be ready to go.

(Note: to keep it simple, this doesn't debounce the button)

HTH
GB

WARNING - Code Verify'ed but not tested.

BTW, A helpful technique to get everything clear is to draw a state diagram. Just on paper, no fancy software.
There is a reasonable example here REKLAB manual [McGill Faculty of Medicine - BioMedical Engineering Department]

The idea is to write down everything that needs to happen, and what causes the system to move to a new state, i.e. what it needs to remember that has happened. It can help.

Okay, I think this corresponds to the pictures.
The addition is to wait until the button has been released.

void loop() {
  
  button = digitalRead(buttonPin);
  if (button == HIGH) {
      buttonState = true;
      while (button == HIGH) {
        button = digitalRead(buttonPin);
      }
      buttonHoldTime = millis() + HoldTime;    // remember when to release
      // do the thing that needs to happen when the button is pressed
      // Press button B
  }
  
  // Time to release the button?
  if ((buttonState == true) && (millis() < buttonHoldTime)) {
       buttonState = false; 
      // do thing that needs to happen when the button is automatically released
      // release button B
  }
}

I'm not trying to make your life hard, a state diagram really can be very clear.

Let me try to create one.

GB

I was under the assumption that as soon as I set a button to "on" with digitalWrite(button,HIGH), that it only pulses the button and immediately returns to not giving it voltage.

No, digitalWrite(button,HIGH) doesn't pulse.
The state of the pin is held until it is changed with another digitalWrite.

BUT, I don't understand why you're talking about digitalWrite() to a button. The pin connected to a button is usually an INPUT.

Nil problemo, amigo (as Homer Simpson says)

BUT, I don't understand why you're talking about digitalWrite() to a button. The pin connected to a button is usually an INPUT.

It would be very helpful if you describe exactly what you need to do.
If you accidentally ask a misleading question, you'll likely get a misleading answer.

The Arduino can't hold a button down, that is a physical thing.

It can hold an OUTPUT pin, and hence a signal wire HIGH or LOW with a digitalWrite, but that is not the same as physically pressing a button.

A digital output pin from the Arduino could be wired in parallel with a button, but it isn't the same as pressing the button. The button is still free to be pressed. It would also need a bit of care to wire up correctly as if the button is pressed, it might drive the Arduino pin too hard so that it damages the Arduino.

What is it that needs to be pressed?
Is it a physical button (in which case you need a mechanical actuator like a servo or solenoid to press it), or do you need a signal to be held HIGH or LOW?
Is there actually a button B? What is the button attached to?

Okay, I understand better, thank you.

So do you know how to simulate pressing the other button?

If not, can you do a schematic, even a sketch, of the parts involved? (paper and a photo will be fine, if that is easier)

Okay, so their are two buttons, A & B.
Are the buttons wired with pull-up, or pull-down resistors?
If they are, approximately how big?

So other than the Arduino, what is each button connected to? Just ground and 5V?

(Can you sketch what is on the controller board, or is it a commercial product? Just a rough sketch, not all of the details.)

I'm puzzled again.

What does pressing button B do?

hi im strugaling to get my arduino to type a key on the computer. i am new at using arduinos and i hear there are multaple steps involved but i dont have the slightest idea how to go about doing this. any code, info, tips would be helpfull. thanks.

hi im strugaling to get my arduino to type a key on the computer.

An Arduino can't type a key on your computer unless you use a physical actuator like a motor, servo or solenoid.

How about opening a new thread, and explain what you want to do?
Don't do that in here as the thread will become confused.
If you ask politely, and are clear so folks can understand, they may be able to help.