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