Question

I have a program for Arduino written such that when I hold a button down, it will turn an LED off. I want to be able to print to the serial monitor how long the button was pressed for after I let go of the button. Does anybody have any ideas? Below is the code I have written thus far:

/I constants wont change. Theyre used here to // set pin numbers: define LED RED LED define buttonPIN PUSH2 const int buttonPin = PUSH2; const int ledPin GREEN LED; // the number of the pushbutton pin // the number of the LED pin /I 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_PULLUP) void loop ) // read the state of the pushbutton value: buttonState digita!Read (buttonPin) ; // check if the pushbutton is pressed // if it is, the buttonState is HIGH: if (buttonStateHIGH) I turn LED on: digitalWrite (ledPin, HIGH) else /I turn LED off: digitalWrite (ledPin, LOW)

Text of Code:

#define LED RED_LED
#define buttonPIN PUSH2


const int buttonPin = PUSH2; // the number of the pushbutton pin
const int ledPin = GREEN_LED; // 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_PULLUP);
}

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);  
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

One way to work around this is using millis() function. When called at a point in the program, this counts elapsed miliseconds since the arduino begins the execution of the current program. (Please refer to the arduino reference on the same function)

To count the total time of holding the button in pushed state, it should be the difference of the timestamps of:

When buttonState == HIGH resulted TRUE and the led state was turned to HIGH by digitalWrite(ledPin, HIGH) upto when the led state was turned to low by digitalWrite(ledPin, LOW)

i.e. we need to place the timers at the following places in the codes:

1) First Timestamp

if (buttonState == HIGH) {
check_time_1=millis();
digitalWrite(ledPin, HIGH);

}

2) Second Timestamp

else {
digitalWrite(ledPin, LOW);
check_time_2=millis(); }

We can now take the different between check_time_2 and check_time_1 to output the desired time spent in holding the button.

NOTE: There are issues with the overflow of the timer count in millis() if the program execution goes beyond certain limit. But, in the given case, it is highly unlikely as holding a button, at maximum, will span few minutes. Though, if required, overflow can also be worked around. Please explore on the techinques for properly measuring time intervals with overflow possibilities in arduino, if required.

Hope it helps! If it works please thumbs up!

Add a comment
Know the answer?
Add Answer to:
I have a program for Arduino written such that when I hold a button down, it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • void setup () Write the following code and upload it to the { Arduino, observe and...

    void setup () Write the following code and upload it to the { Arduino, observe and comment on the pinMode (8, OUTPUT); pinMode (2, INPUT); digitalWrite (8, HIGH); function of the circuit and explain how the code is working. In addition, explain how to turn ON the LED in case it is OFF. void loop () { digitalRead (2) int A if (A 0) { digitalWrite (8, LOW); void setup () Write the following code and upload it to the...

  • Mechatronics void setup () Write the following code and upload it to the { Arduino, observe...

    Mechatronics void setup () Write the following code and upload it to the { Arduino, observe and comment on the pinMode (8, OUTPUT) function of the circuit and explain how the pinMode (2, INPUT); code is working. In addition, explain how to digitalWrite (8, HIGH) turn ON the LED in case it is OFF. void loop () { digitalRead (2) int A if (A 0) { digitalWrite (8, LOW) void setup () Write the following code and upload it to...

  • I'm trying to make a circuit from this code that I found for a personal project,...

    I'm trying to make a circuit from this code that I found for a personal project, but I'm having trouble figuring it out. Could someone make me a Circuit Diagram for this code with an LED, Servo, and Arduino Nano on BreadBoard. (No Battery, just use usb power from Nano) Thank You. Nano Code: const int servoPin = 5; const int buttonPin = 3; const int LEDPin = 4; #include <Servo.h> Servo visorServo; void setup() { visorServo.write(0); // Initial position...

  • I am trying to program my Arduino UNO to implement a flashing pattern with two LEDs....

    I am trying to program my Arduino UNO to implement a flashing pattern with two LEDs. When one LED is off, the other must be on. The program needs to be done in assembly. I have a working program in c++ but I need to convert it to assembly. Here is my c++ program: int led = 13; int led2 = 12; void setup() {    pinMode(led, OUTPUT); pinMode(led2, OUTPUT); } void loop() { digitalWrite(led, HIGH);    delay(1000); digitalWrite(led, LOW);...

  • Using the template provided in the lab details section, formulate a program for the Arduino to...

    Using the template provided in the lab details section, formulate a program for the Arduino to generate a PWM signal duty cycles between 0% and 100% at intervals of 10% where a button will increment the PWM signal. Remember the RC is 10 milliseconds. Provide a copy of the final code and explain how it works. Arduino PWM Example int Pin = 9; void setup() { pinMode (Pin, OUTPUT); } void loop() { analogWrite (Pin, 127); // Generate 50% duty...

  • I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED str...

    I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED strips and 3 - ultrasonics. I have them working independently but I need to combine the code so they work together. Here is what I have: Memory Game #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s byte sequence[100]; // Storage for the light sequence byte curLen = 0; // Current length of the sequence byte inputCount =...

  • Design a circuit and Arduino program that accomplishes the following: An IR distance sensor will detect the presence (t...

    Design a circuit and Arduino program that accomplishes the following: An IR distance sensor will detect the presence (through waving of a hand) A H-Bridge circuit with 9V battery power will control the direction of the DC motor as described in Chapter 4 (pp. 70-79) of your textbook. When a hand is waved over the IR sensor, the motor moves in one direction (simulating opening a door). There is a 2 second pause, and then the motor moves in the...

  • Program is for an Arduino 1. Add the letters “A”, “b”, “c”, “d”, “E”, “F” to...

    Program is for an Arduino 1. Add the letters “A”, “b”, “c”, “d”, “E”, “F” to your homework program. Continually display “0123”, “4567”, “89Ab”, “cdEF” with a one second delay between each one. HINT: Expand pickNumber() to use numbers 10-15 (a-f in hex). Create new functions for a-f. (THIS IS THE CODE FROM THE HOMEWORK) /*************************************** name:Stopwatch function: you can see the number increases by one per second on the 4-digit 7-segment display. ***********************************/ //Email:[email protected] //Website:www.sunfounder.com /**************************************/ #include //the pins...

  • 14. 10 pts. You have the following code lines in an Arduino c++ sketch to blink an LED and control a standard servo (not a continuous servo) and buzzer: #include <Servo.h> Servo frontSer...

    14. 10 pts. You have the following code lines in an Arduino c++ sketch to blink an LED and control a standard servo (not a continuous servo) and buzzer: #include <Servo.h> Servo frontServo; int time 2500; int multiple 3; int leg-5 int red 2; void setup()X pinMode(red,OUTPUT) frontServo.attach(leg): pinMode(4, OUTPUT) void looplf frontServo.write(0); digitalWrite(red, HIGH); delay(time); tone(4,400,500); digitalWrite(red,LOW); time-time"multiple; delay(time); Answer the following: What pin should the LED be connected to? what pin should the information lead wire of the...

  • An Arduino Uno is configured with the voltage divider connected to Analog input pin AO as...

    An Arduino Uno is configured with the voltage divider connected to Analog input pin AO as shown, and the code shown below is uploaded. What happens? testCodeA Arduino 1.8.11 Hourly Build 2019/10/29 11:12 Arduino Uno testCodeA #define LED 13 to sv 13o 5ke & void setup() { pinMode(LED, OUTPUT); to A 10KZ Ž ŞR void loop() { int voltage-analogRead(AO); if (voltage > 1024/2) digitalWrite(LED, HIGH); else digitalWrite(LED, LOW); GND GND Done compiling Sketch uses 778 bytes (2%) of program storage...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT