Buongiorno amici sto scrivendo la mia prima libreria e ho iniziato con una cosa molto semplice, infatti ho scritto una libreria per i pulsanti che si trovano sulla shield LCD per Arduino. Questa è la libreria, il file *.h:
#ifndef KeypadLCD_h
#define KeypadLCD_h
#include "Arduino.h"
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
class KeypadLCD{
public:
KeypadLCD(int pin);
int ReadButtons();
private:
int _pin;
};
#endif
E il file *.cpp
#include "Arduino.h"
#include "KeypadLCD.h"
int adc_key_in = 0;
KeypadLCD::KeypadLCD(int pin){
pinMode(pin, INPUT);
_pin = pin;
}
int KeypadLCD::ReadButtons(){
adc_key_in = analogRead(_pin);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
}
Ora vorrei un po' ampliarla infatti ci vorrei aggiungere la funzione per l'antirimbalzo:
#include <KeypadLCD.h>
//Variabile per reset millis()
extern unsigned long timer0_millis;
KeypadLCD key(0);
int stato = btnNONE;
int pre_stato = btnNONE;
int lcd_key = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
stato = key.ReadButtons();
//Antirimbalzo
if(pre_stato == btnNONE && stato != btnNONE){
pre_stato = stato;
delay(50);
}
if(pre_stato != btnNONE && (stato == btnNONE || (((stato == btnUP) ||(stato == btnDOWN)) && millis() > 1000))){
a = millis();
if((stato == btnUP || stato == btnDOWN) && a > 1000){
//Resetto millis()
resetMillis();
}
delay(50);
lcd_key = pre_stato;
pre_stato = btnNONE;
//Fine antirimbalzo
switch(lcd_key){
case btnRIGHT:
Serial.println("RIGHT ");
break;
case btnUP:
Serial.println("UP ");
break;
case btnDOWN:
Serial.println("DOWN ");
break;
case btnLEFT:
Serial.println("LEFT ");
break;
case btnSELECT:
Serial.println("SELECT ");
break;
case btnNONE:
Serial.print("NONE ");
break;
}
}
}
//Richiamo questa funzione per resettare la funzione millis()
void resetMillis() {
cli();
timer0_millis = 0;
sei();
}
Questo è un codice di esempio che non fa altro che scrivere a video quale pulsante è stato premuto, la mia intenzione sarebbe quella di spostare questa funzione di antirimbalzo dallo sketch nella libreria.
Potete aiutarmi? Ho provati in vari modi ma non funziona.
Grazie,
Antonio