J'ai donc fais comme ceci
#include <LCD4Bit_mod.h>
#include <stdio.h>
#define SMALLSTEP 1
#define BIGSTEP 10
#define MINCONV 60000
#define NUM_KEYS 5
LCD4Bit_mod lcd = LCD4Bit_mod(2);
bool bTimerIsRunning;
unsigned long ulDuration;
unsigned long ulUserAction;
unsigned long ulCurrentTime;
unsigned long ulPreviousTime;
unsigned long ulEndTime;
unsigned long ulPreviousRemainingTime;
int adc_key_val[5] ={30, 150, 360, 535, 760 };
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup()
{
// variables initialization
ulEndTime = 0;
ulCurrentTime = 0;
ulPreviousTime = 0;
ulDuration = 0;
ulUserAction = 0;
ulPreviousRemainingTime = 0;
bTimerIsRunning = false;
// display initialization
lcd.init();
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn(" Choisir duree ");
}
void loop() { error: 'void loop()' previously defined here
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
LCD4Bit_mod lcd = LCD4Bit_mod(2);
// wait a bit:
delay(150);
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
LCD4Bit_mod lcd = LCD4Bit_mod(2);
// wait a bit:
delay(150);
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
LCD4Bit_mod lcd = LCD4Bit_mod(2);
// wait a bit:
delay(150);
}
// delay at the end of the full loop:
delay(1000);
}
void loop() {
//récup valeur button
delay(80);
ulUserAction = GetButtonValue(0);
switch (ulUserAction)
{
case 0 : // right button : +10min
ulDuration += BIGSTEP;
DisplayRemainingTime(ulDuration);
break;
case 1 : // up button : +1min
ulDuration += SMALLSTEP;
DisplayRemainingTime(ulDuration);
break;
case 2 : // down button : -1min
if (ulDuration >= 1)
{
ulDuration -= SMALLSTEP;
}
else
{
ulDuration = 0;
}
DisplayRemainingTime(ulDuration);
break;
case 3 : // left button : -10min
if (ulDuration >= BIGSTEP)
{
ulDuration -= BIGSTEP;
}
else
{
ulDuration = 0;
}
DisplayRemainingTime(ulDuration);
break;
case 4 : // select button
if (ulDuration > 0)
{
bTimerIsRunning = true;
ulEndTime = millis() + (ulDuration * MINCONV);
}
break;
}
delay(80);
// no user action, process countdown if running
if (bTimerIsRunning)
{
ulCurrentTime = millis();
if (ulCurrentTime-ulPreviousTime > 1000)
{
if (ulEndTime < ulCurrentTime)
{
// countdown ended
DisplayEnd();
}
else
{
// countdown still going
DisplayRemainingTime((ulEndTime-ulCurrentTime)/MINCONV);
}
ulPreviousTime = ulCurrentTime;
}
}
}
void DisplayRemainingTime(unsigned long ulRemainingTime)
{
if (ulPreviousRemainingTime != ulRemainingTime)
{
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn("Duree restante:");
char buffer[16];
sprintf(buffer,"%d minute(s)", ulRemainingTime); // %d pour les variables décimale. (int)temp = 3 et temp1 = 14
lcd.cursorTo(2, 0);
lcd.printIn(buffer);
ulPreviousRemainingTime = ulRemainingTime;
}
}
void DisplayEnd()
{
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn(" Duree atteinte");
lcd.cursorTo(2, 0);
lcd.printIn("Appui sur RESET");
bTimerIsRunning = false;
ulCurrentTime = 0;
ulEndTime = 0;
}
int GetButtonValue(int iAnalogSensor)
{
adc_key_in = analogRead(iAnalogSensor);
//digitalWrite(13, HIGH);
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
adc_key_in = analogRead(iAnalogSensor); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
oldkey = key;
}
}
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
{
k = -1; // No valid key pressed
}
return k;
}