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 betwwen 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 (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  this->lastButtonState = reading;
87 
88  for (int i = 0; i < this->size; i++)
89  if (this->pButtons[i].IsPushed(reading))
90  {
91  foundID = this->pButtons[i].GetId();
93 #ifdef COMMANDERS_DEBUG_MODE
94  Serial.print(F("Analog push button "));
95  Serial.print(i);
96  Serial.println(F(" pressed"));
97 #endif
98  }
99  }
100  this->lastDebounceTime = 0;
101  }
102 
103  // save the reading. Next time through the loop,
104  // it'll be the lastButtonState:
105  lastButtonState = reading;
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 
122  for (int i = 0; i < this->size; i++)
123  {
124  Serial.print(F(" "));
125  this->GetItem(i)->printCommander();
126  }
127 }
128 #endif
129 #endif
130 #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
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