-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotentiometer.cpp
96 lines (88 loc) · 2.11 KB
/
Potentiometer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* $Id: Potentiometer.cpp 1198 2011-06-14 21:08:27Z bhagman $
||
|| @author Alexander Brevig <[email protected]>
|| @url http://wiring.org.co/
|| @url http://alexanderbrevig.com/
|| @contribution Brett Hagman <[email protected]>
||
|| @description
|| | Hardware Abstraction Library for Potentiometers.
|| | Provides an easy way of making/using potentiometers.
|| |
|| | Wiring Cross-platform Library
|| #
||
|| @license Please see cores/Common/License.txt.
||
*/
#include "Potentiometer.h"
#include <Arduino.h>
#define POTENTIOMETER_ADC_RESOLUTION 1024
/*
|| @constructor
|| | Initialize the Potentiometer object
|| #
||
|| @parameter potPin the pin this Potentiometer is connected to
*/
Potentiometer::Potentiometer(byte potPin)
{
pin = potPin;
setSectors(6);
}
/*
|| @constructor
|| | Initialize the Potentiometer object and specify the number of sectors of it
|| #
||
|| @parameter potPin the pin this Potentiometer is connected to
|| @parameter sectors the pin this Potentiometer is connected to
*/
Potentiometer::Potentiometer(byte potPin, uint16_t sectors)
{
pin = potPin;
setSectors(sectors);
}
/*
|| @description
|| | Get the current value of the Potentiometer
|| | This is a wrapper around the analogRead core call
|| | Why? To make the code that handles Potentiometers easier to read
|| #
||
|| @return The value of the Potentiometer [0,1023]
*/
uint16_t Potentiometer::getValue()
{
return analogRead(pin);
}
/*
|| @description
|| | Get the current sector of the Potentiometer
|| #
||
|| @return The current sector of this Potentiometer [0,sectors]
*/
uint16_t Potentiometer::getSector()
{
return analogRead(pin) / (POTENTIOMETER_ADC_RESOLUTION / sectors);
}
/*
|| @description
|| | Set the number of sectors for this Potentiometer
|| #
||
|| @parameter newSectors the number of sectors of this Potentiometer
*/
void Potentiometer::setSectors(uint16_t newSectors)
{
//map newSectors within the bounds of the accepted values?
if (newSectors < POTENTIOMETER_ADC_RESOLUTION && newSectors > 0)
{
sectors = newSectors;
}
else
{
sectors = POTENTIOMETER_ADC_RESOLUTION - 1;
}
}