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  this->analogPin = inButtonPin;
28  this->readingTolerancy = inTolerancy;
29 
30  for (int i = 0; i < this->size; i++)
31  {
32 #ifdef COMMANDERS_DEBUG_MODE
33  if (inpButtonValues[i] < 0 || inpButtonValues[i] > 1023)
34  {
35  Serial.print(F("Analog push buttons. Invalid value "));
36  Serial.print(inpButtonValues[i]);
37  Serial.print(F(" for button "));
38  Serial.print(i);
39  Serial.println(F(". Value must between 0 and 1023 !"));
40  }
41 #endif
42  this->pButtons[i].begin(inpIds[i], inpButtonValues[i], inTolerancy);
43  }
44 
45  pinMode(this->analogPin, INPUT);
46 }
47 
49 {
50  for (int i = 0; i < this->size; i++)
51  if (this->pButtons[i].GetId() == inId)
52  return &(this->pButtons[i]);
53 
54  return 0;
55 }
56 
58 {
59  unsigned long foundID = UNDEFINED_ID;
60 
61  if (this->analogPin == 0)
62  return foundID;
63 
64  // read the state of the switch into a local variable:
65  int reading = analogRead(this->analogPin);
66 
67  // check to see if you just pressed the button
68  // (i.e. the input went from LOW to HIGH), and you've waited
69  // long enough since the last press to ignore any noise:
70 
71  // If the button changed, due to noise or pressing:
72  if (this->lastDebounceTime == 0 && (reading < this->lastButtonState - this->readingTolerancy || reading > this->lastButtonState + this->readingTolerancy))
73  {
74  // reset the debouncing timer
75  this->lastDebounceTime = millis();
76  }
77 
78  if (this->lastDebounceTime > 0 && (millis() - this->lastDebounceTime) > this->debounceDelay)
79  {
80  // whatever the reading is at, it's been there for longer
81  // than the debounce delay, so take it as the actual current state:
82 
83  // if the button state has changed:
84  if (reading < this->lastButtonState - this->readingTolerancy || reading > this->lastButtonState + this->readingTolerancy)
85  {
86  // save the reading. Next time through the loop,
87  // it'll be the lastButtonState:
88  this->lastButtonState = reading;
89 
90  for (int i = 0; i < this->size; i++)
91  if (this->pButtons[i].IsPushed(reading))
92  {
93  foundID = this->pButtons[i].GetId();
95 #ifdef COMMANDERS_DEBUG_MODE
96  Serial.print(F("Analog push button "));
97  Serial.print(i);
98  Serial.println(F(" pressed"));
99 #endif
100  }
101  }
102  this->lastDebounceTime = 0;
103  }
104 
105  return foundID;
106 }
107 
109 {
110  this->lastButtonPressed = -1;
111 }
112 
113 #ifdef COMMANDERS_PRINT_COMMANDERS
114 void ButtonsCommanderAnalogPushes::printCommander()
115 {
116  Serial.print(F(" AnalogPushes - Pin :"));
117  Serial.println(this->analogPin);
118  Serial.print(F(" / Reading Accuracy: "));
119  Serial.println(this->readingTolerancy);
120 
121  for (int i = 0; i < this->size; i++)
122  {
123  Serial.print(F(" "));
124  this->GetItem(i)->printCommander();
125  }
126 }
127 #endif
128 #endif
129 #endif
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
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
ButtonsCommanderAnalogPushesItem * GetItem(uint8_t inNumber)