Commanders
Arduino buttons/bus library
ButtonsCommanderPotentiometer.cpp
1 /*************************************************************
2 project: <Commanders>
3 author: <Thierry PARIS>
4 description: <Potentiometer returning a current value.>
5 *************************************************************/
6 
7 #include <Commanders.h>
8 #ifndef NO_BUTTONSCOMMANDER
9 #ifndef NO_BUTTONSCOMMANDERPOTENTIOMETER
10 
12 {
13 }
14 
15 void ButtonsCommanderPotentiometer::begin(unsigned long inId, int inPin, int inMinimum, int inMaximum, int inMoveAccuracy)
16 {
17  this->Id = inId;
18  this->mini = inMinimum;
19  this->maxi = inMaximum;
20  this->pin = inPin;
21  this->moveAccuracy = inMoveAccuracy - 1;
22  if (this->moveAccuracy <= 0)
23  this->moveAccuracy = 1;
24 
25  digitalWrite(this->pin, HIGH);
26  int val = analogRead(this->pin);
27  this->currentValue = map(val, 0, ANALOG_LIMIT, this->mini, this->maxi);
28 }
29 
31 {
32 #ifdef COMMANDERS_DEBUG_MODE
33  if (this->Id == UNDEFINED_ID || this->pin == DP_INVALID)
34  {
35  if (this->moveAccuracy != 32767) // If the error message has not been yet shown...
36  {
37  // use it as a debug flag !
38  Serial.println(F("This potentiometer have no ID or pin defined : call begin() !"));
39  this->moveAccuracy = 32767; // The error message has been shown...
40  }
41  }
42 #endif
43 
44  if (this->Id == UNDEFINED_ID || this->pin == DP_INVALID)
45  {
46  return UNDEFINED_ID;
47  }
48 
49  int val = analogRead(this->pin);
50 #ifdef COMMANDERS_DEBUG_MODE
51  //Serial.print(F("Potentiometer real value : "));
52  //Serial.println(val, DEC);
53 #endif
54 
55  val = map(val, 0, ANALOG_LIMIT, this->mini, this->maxi);
56 
57  if (val < this->currentValue - this->moveAccuracy || val > this->currentValue + this->moveAccuracy)
58  {
59 #ifdef COMMANDERS_DEBUG_MODE
60  Serial.print(F("Potentiometer new value : "));
61  Serial.println(val, DEC);
62 #endif
63  this->currentValue = val;
65  }
66 
67  return UNDEFINED_ID;
68 }
69 
70 #ifdef COMMANDERS_PRINT_COMMANDERS
71 void ButtonsCommanderPotentiometer::printCommander()
72 {
73  Serial.print(F(" Potentiometer - Pin:"));
74  Serial.print(this->pin);
75  Serial.print(F(" / Mini: "));
76  Serial.print(this->mini);
77  Serial.print(F(" / Maxi: "));
78  Serial.print(this->maxi);
79  Serial.print(F(" / Accuracy: "));
80  Serial.println(this->moveAccuracy);
81 }
82 #endif
83 #endif
84 #endif
unsigned long GetId() const
void begin(unsigned long inId, int inPin, int inMinimum, int inMaximum, int inMoveAccuracy = 1)
#define UNDEFINED_ID
Definition: Events.h:38
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27