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] > ANALOG_LIMIT)
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.print(F(". Value must be between 0 and "));
41  Serial.print(ANALOG_LIMIT);
42  Serial.print(F(" !"));
43  }
44 #endif
45  this->pButtons[i].begin(inpIds[i], inpButtonValues[i], inTolerancy);
46  }
47 
48  this->lastButtonState = 10000;
49  this->lastDebounceTime = 0;
50 
51  pinMode(this->analogPin, INPUT);
52 }
53 
54 
56 {
57  for (int i = 0; i < this->size; i++)
58  if (this->pButtons[i].GetId() == inId)
59  return &(this->pButtons[i]);
60 
61  return 0;
62 }
63 
65 {
66  unsigned long foundID = UNDEFINED_ID;
67 
68  if (this->analogPin == 0)
69  return foundID;
70 
71  // read the state of the switch into a local variable:
72  int reading = analogRead(this->analogPin);
73 
74 #ifdef COMMANDERS_DEBUG_VERBOSE_MODE
75  Serial.print(F("Analog push button value "));
76  Serial.println(reading);
77 #endif
78 
79  // check to see if you just pressed the button
80  // (i.e. the input went from LOW to HIGH), and you've waited
81  // long enough since the last press to ignore any noise:
82 
83  // If the button changed, due to noise or pressing:
84  if (this->lastDebounceTime == 0 && (reading < this->lastButtonState - this->readingTolerancy || reading > this->lastButtonState + this->readingTolerancy))
85  {
86  // reset the debouncing timer
87  this->lastDebounceTime = millis();
88  this->lastButtonState = reading;
89  }
90 
91  if (this->lastDebounceTime > 0 && (millis() - this->lastDebounceTime) > this->debounceDelay)
92  {
93  // whatever the reading is at, it's been there for longer
94  // than the debounce delay, so take it as the actual current state:
95 
96  // if the button state has changed:
97  if (reading >= this->lastButtonState - this->readingTolerancy && reading <= this->lastButtonState + this->readingTolerancy)
98  {
99  for (int i = 0; i < this->size; i++)
100  if (this->pButtons[i].IsPushed(reading))
101  {
102  foundID = this->pButtons[i].GetId();
104 #ifdef COMMANDERS_DEBUG_MODE
105  Serial.print(F("Analog push button "));
106  Serial.print(i);
107  Serial.println(F(" pressed"));
108 #endif
109  }
110  }
111 
112  this->lastDebounceTime = 0;
113  this->lastButtonState = reading;
114  }
115 
116  return foundID;
117 }
118 
120 {
121  this->lastButtonPressed = -1;
122 }
123 
124 #ifdef COMMANDERS_PRINT_COMMANDERS
125 void ButtonsCommanderAnalogPushes::printCommander()
126 {
127  Serial.print(F(" AnalogPushes - Pin :"));
128  Serial.print(this->analogPin);
129  Serial.print(F(" / Reading Accuracy: "));
130  Serial.print(this->readingTolerancy);
131  Serial.print(F(" / Debounce delay: "));
132  Serial.println(this->debounceDelay);
133 
134  for (int i = 0; i < this->size; i++)
135  {
136  Serial.print(F(" "));
137  this->GetItem(i)->printCommander();
138  }
139 }
140 #endif
141 #endif
142 #endif
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27
ButtonsCommanderAnalogPushesItem * GetItem(uint8_t inNumber)
unsigned long GetId() const
ButtonsCommanderButton * GetFromId(unsigned long inId)
void begin(unsigned long inId, int inAnalogValue, int inTolerancy)
void begin(int inButtonPin, uint8_t inNumber, unsigned long *inpIds, int *inpButtonValues, int inTolerancy = 20)
#define UNDEFINED_ID
Definition: Events.h:38