| Mozzi version v1.1.0 sound synthesis library for Arduino |

RCpoll.h
1 #ifndef RCPOLL_H
2 #define RCPOLL_H
3
4
5 /**
6 A class for reading voltage on a digital pin, derived from http://arduino.cc/en/Tutorial/RCtime.
7 This is designed to be used in updateControl(). Each time it is called, it checks if a capacitor has charged,
8 and returns an output reflecting how long it took for the most recent charge.
9 */
10
11 template <unsigned char SENSOR_PIN>
12 class RCpoll
13 {
14
15 public:
16 /** Constructor.
17 */
18 RCpoll():result(0),rc_cued(true), output(0)
19 {
20 ;
21 }
22
23 /** Checks whether the capacitor has charged, and returns how long it took for the most recent charge.
24 This would preferably be called in updateControl(), but if the resolution isn't fine enough or the
25 pin charges too fast for updateControl() to catch, try it in updateAudio().
26 @return the sensor value, reflected in how many checking cycles it took to charge the capacitor.
27 */
28 inline
29 unsigned int next(){
30 if (rc_cued){
31 pinMode(SENSOR_PIN, INPUT); // turn pin into an input and time till pin goes low
32 digitalWrite(SENSOR_PIN, LOW); // turn pullups off - or it won't work
33 rc_cued = false;
34 }
35 if(digitalRead(SENSOR_PIN)){ // wait for pin to go low
36 result++;
37 }
38 else{
39 output = result;
40 result = 0;
41 pinMode(SENSOR_PIN, OUTPUT); // make pin OUTPUT
42 digitalWrite(SENSOR_PIN, HIGH); // make pin HIGH to discharge capacitor - see the schematic
43 rc_cued = true;
44 }
45 return output;
46 }
47
48 private:
49 unsigned int result;
50 boolean rc_cued;
51 unsigned int output;
52
53 };
54
55 #endif // #ifndef RCPOLL_H
RCpoll::next
unsigned int next()
Checks whether the capacitor has charged, and returns how long it took for the most recent charge...
Definition: RCpoll.h:29
RCpoll
A class for reading voltage on a digital pin, derived from http://arduino.cc/en/Tutorial/RCtime.
Definition: RCpoll.h:12
RCpoll::RCpoll
RCpoll()
Constructor.
Definition: RCpoll.h:18