Commanders
Arduino buttons/bus library
ButtonsCommanderAnalogPushes.cpp
1 /*************************************************************
2 project: <Commanders>
3 author: <Thierry PARIS>
4 description: <Composite push button array on analog pin with debounce.>
5 *************************************************************/
6 
7 #include <Commanders.h>
8 #ifndef NO_BUTTONSCOMMANDER
9 #ifndef NO_BUTTONSCOMMANDERANALOGPUSHES
10 
12 {
13  this->analogPin = 0;
14  this->lastButtonState = 10000;
15 
16  this->lastDebounceTime = 0;
17  this->debounceDelay = 50;
18 
19  this->size = 0;
20 }
21 
22 void ButtonsCommanderAnalogPushes::begin(int inButtonPin, uint8_t inNumberOfItems, unsigned long *inpIds, int *inpButtonValues, int inTolerancy)
23 {
24  this->size = inNumberOfItems;
25  this->pButtons = new ButtonsCommanderAnalogPushesItem[this->size];
26 
27  digitalWrite(inButtonPin, HIGH);
28  this->analogPin = inButtonPin;
29  this->readingTolerancy = inTolerancy;
30 
31  for (int i = 0; i < this->size; i++)
32  {
33 #ifdef COMMANDERS_DEBUG_MODE
34  if (inpButtonValues[i] < 0 || inpButtonValues[i] > 1023)
35  {
36  Serial.print(F("Analog push buttons. Invalid value "));
37  Serial.print(inpButtonValues[i]);
38  Serial.print(F(" for button "));
39  Serial.print(i);
40  Serial.println(F(". Value must between 0 and 1023 !"));
41  }
42 #endif
43  this->pButtons[i].begin(inpIds[i], inpButtonValues[i], inTolerancy);
44  }
45 
46  this->lastButtonState = 10000;
47  this->lastDebounceTime = 0;
48 
49  pinMode(this->analogPin, INPUT);
50 }
51 
53 {
54  for (int i = 0; i < this->size; i++)
55  if (this->pButtons[i].GetId() == inId)
56  return &(this->pButtons[i]);
57 
58  return 0;
59 }
60 
62 {
63  unsigned long foundID = UNDEFINED_ID;
64 
65  if (this->analogPin == 0)
66  return foundID;
67 
68  // read the state of the switch into a local variable:
69  int reading = analogRead(this->analogPin);
70 
71 #ifdef COMMANDERS_DEBUG_VERBOSE_MODE
72  Serial.print(F("Analog push button value"));
73  Serial.println(reading);
74 #endif
75 
76  // check to see if you just pressed the button
77  // (i.e. the input went from LOW to HIGH), and you've waited
78  // long enough since the last press to ignore any noise:
79 
80  // If the button changed, due to noise or pressing:
81  if (this->lastDebounceTime == 0 && (reading < this->lastButtonState - this->readingTolerancy || reading > this->lastButtonState + this->readingTolerancy))
82  {
83  // reset the debouncing timer
84  this->lastDebounceTime = millis();
85  this->lastButtonState = reading;
86  }
87 
88  if (this->lastDebounceTime > 0 && (millis() - this->lastDebounceTime) > this->debounceDelay)
89  {
90  // whatever the reading is at, it's been there for longer
91  // than the debounce delay, so take it as the actual current state:
92 
93  // if the button state has changed:
94  if (reading >= this->lastButtonState - this->readingTolerancy && reading <= this->lastButtonState + this->readingTolerancy)
95  {
96  for (int i = 0; i < this->size; i++)
97  if (this->pButtons[i].IsPushed(reading))
98  {
99  foundID = this->pButtons[i].GetId();
101 #ifdef COMMANDERS_DEBUG_MODE
102  Serial.print(F("Analog push button "));
103  Serial.print(i);
104  Serial.println(F(" pressed"));
105 #endif
106  }
107  }
108 
109  this->lastDebounceTime = 0;
110  this->lastButtonState = reading;
111  }
112 
113  return foundID;
114 }
115 
117 {
118  this->lastButtonPressed = -1;
119 }
120 
121 #ifdef COMMANDERS_PRINT_COMMANDERS
122 void ButtonsCommanderAnalogPushes::printCommander()
123 {
124  Serial.print(F(" AnalogPushes - Pin :"));
125  Serial.println(this->analogPin);
126  Serial.print(F(" / Reading Accuracy: "));
127  Serial.println(this->readingTolerancy);
128  Serial.print(F(" / Debounce delay: "));
129  Serial.print(this->debounceDelay);
130 
131  for (int i = 0; i < this->size; i++)
132  {
133  Serial.print(F(" "));
134  this->GetItem(i)->printCommander();
135  }
136 }
137 #endif
138 #endif
139 #endif
ButtonsCommanderAnalogPushesItem * GetItem(uint8_t inNumber)
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27
unsigned long GetId() const
void begin(unsigned long inId, int inAnalogValue, int inTolerancy)
ButtonsCommanderButton * GetFromId(unsigned long inId)
void begin(int inButtonPin, uint8_t inNumber, unsigned long *inpIds, int *inpButtonValues, int inTolerancy = 20)
#define UNDEFINED_ID
Definition: Events.h:38