Mozzi  version v2.0
sound synthesis library for Arduino
RCpoll.h
1 /*
2  * RCpoll.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2014-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 #ifndef RCPOLL_H
13 #define RCPOLL_H
14 
15 
22 template <unsigned char SENSOR_PIN>
23 class RCpoll
24 {
25 
26 public:
29  RCpoll():result(0),rc_cued(true), output(0)
30  {
31  ;
32  }
33 
39  inline
40  unsigned int next(){
41  if (rc_cued){
42  pinMode(SENSOR_PIN, INPUT); // turn pin into an input and time till pin goes low
43  digitalWrite(SENSOR_PIN, LOW); // turn pullups off - or it won't work
44  rc_cued = false;
45  }
46  if(digitalRead(SENSOR_PIN)){ // wait for pin to go low
47  result++;
48  }
49  else{
50  output = result;
51  result = 0;
52  pinMode(SENSOR_PIN, OUTPUT); // make pin OUTPUT
53  digitalWrite(SENSOR_PIN, HIGH); // make pin HIGH to discharge capacitor - see the schematic
54  rc_cued = true;
55  }
56  return output;
57  }
58 
59 private:
60  unsigned int result;
61  boolean rc_cued;
62  unsigned int output;
63 
64 };
65 
66 #endif // #ifndef RCPOLL_H
67 
A class for reading voltage on a digital pin, derived from http://arduino.cc/en/Tutorial/RCtime.
Definition: RCpoll.h:24
RCpoll()
Constructor.
Definition: RCpoll.h:29
unsigned int next()
Checks whether the capacitor has charged, and returns how long it took for the most recent charge.
Definition: RCpoll.h:40