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