this code works very well, but I need a small change:
//////////////////////////////////////////////////////////////////////////////
// Arduino button tutorial 3.
//
// Demonstrates:
// - detection of releasing edge and deglitching
// - state variables for iterative update call
// - distinction of short press and long press
//
// Push-button must be connected as follows:
// __,__
// Pin2 ------o o------ GND
//
// (C) 2011 By P. Bauermeister
// This example code is in the public domain.
//////////////////////////////////////////////////////////////////////////////
// Adapt these to your board and application timings:
#define BUTTON_PIN 2 // Button
#define LONGPRESS_LEN 25 // Min nr of loops for a long press
#define DELAY 20 // Delay per loop in ms
//////////////////////////////////////////////////////////////////////////////
enum { EV_NONE=0, EV_SHORTPRESS, EV_LONGPRESS };
boolean button_was_pressed; // previous state
int button_pressed_counter; // press running duration
void setup()
{
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH); // pull-up
Serial.begin(9600);
button_was_pressed = false;
button_pressed_counter = 0;
}
int handle_button()
{
int event;
int button_now_pressed = !digitalRead(BUTTON_PIN); // pin low -> pressed
if (!button_now_pressed && button_was_pressed) {
if (button_pressed_counter < LONGPRESS_LEN)
event = EV_SHORTPRESS;
else
event = EV_LONGPRESS;
}
else
event = EV_NONE;
if (button_now_pressed)
++button_pressed_counter;
else
button_pressed_counter = 0;
button_was_pressed = button_now_pressed;
return event;
}
void loop()
{
// handle button
boolean event = handle_button();
// do other things
switch (event) {
case EV_NONE:
Serial.print(".");
break;
case EV_SHORTPRESS:
Serial.print("S");
break;
case EV_LONGPRESS:
Serial.print("L");
break;
}
// add newline sometimes
static int counter = 0;
if ((++counter & 0x3f) == 0)
Serial.println();
delay(DELAY);
}
with this code,
when you ‘short press’ it, it will ‘Serial.print(“S”)’ only ones, (when the button is released), if released before set time.
and
when you ‘long press’ it, it will ‘Serial.print(“L”)’ only ones, (when the button is released), if released after set time.
what must be changed
basically…
if I press the button and release it before set time, it must ‘Serial.print(“S”)’ and flash LED ones (this part is all good for me)
but…
if I press the button and hold , it must '‘Serial.print(temperature)’ continuously after set time, and until I release the button.
Your sample code is far too complicated for what you want to do. Think of it as responding to a button push. When you press the button, start you timer. Then you gave two conditions, either the button is released before a set time, in which case flash the LED, or the button is still depressed in which case print your temp WHILE the button is still depressed.