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  int val = analogRead(this->pin);
26  this->currentValue = map(val, 0, 1023, this->mini, this->maxi);
27 }
28 
30 {
31 #ifdef COMMANDERS_DEBUG_MODE
32  if (this->Id == UNDEFINED_ID)
33  Serial.println(F("This potentiometer have no ID defined : call begin() !"));
34 #endif
35 
36  int val = analogRead(this->pin);
37 
38  val = map(val, 0, 1023, this->mini, this->maxi);
39 
40  if (val < this->currentValue - this->moveAccuracy || val > this->currentValue + this->moveAccuracy)
41  {
42 #ifdef COMMANDERS_DEBUG_MODE
43  Serial.print(F("Potentiometer new value : "));
44  Serial.println(val, DEC);
45 #endif
46  this->currentValue = val;
48  }
49 
50  return UNDEFINED_ID;
51 }
52 
53 #ifdef COMMANDERS_PRINT_COMMANDERS
54 void ButtonsCommanderPotentiometer::printCommander()
55 {
56  Serial.print(F(" Potentiometer - Pin:"));
57  Serial.print(this->pin);
58  Serial.print(F(" / Mini: "));
59  Serial.print(this->mini);
60  Serial.print(F(" / Maxi: "));
61  Serial.print(this->maxi);
62  Serial.print(F(" / Accuracy: "));
63  Serial.println(this->moveAccuracy);
64 }
65 #endif
66 #endif
67 #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
void begin(unsigned long inId, int inPin, int inMinimum, int inMaximum, int inMoveAccuracy = 1)
#define UNDEFINED_ID
Definition: Events.h:38