RSeries astromech firmware
MicrophoneAmplitude.h
Go to the documentation of this file.
1 
2 #ifndef Microphone_h
3 #define Microphone_h
4 
5 #include "ReelTwo.h"
6 #include "core/AnimatedEvent.h"
8 
17  public AnimatedEvent, public PeakValueProvider
18 {
19 public:
23  MicrophoneAmplitude(const byte analogPin = 0, const int periodMillis = 50) :
24  fPin(analogPin),
25  fPeriod(periodMillis),
26  fSampleCount(0),
27  fStartPeriod(0),
28  fSample(0),
29  fSignalMax(0),
30  fSignalMin(1024),
31  fPeakToPeak(0),
32  fGain(10)
33  {
34  pinMode(fPin, INPUT);
35  }
36 
41  virtual void animate()
42  {
43  unsigned long currentMillis = millis();
44  fSample = analogRead(fPin);
45  if (currentMillis - fStartPeriod < fPeriod)
46  {
47  if (fSample < 1024)
48  {
49  if (fSample > fSignalMax)
51  if (fSample < fSignalMin)
53  }
54  fSampleCount++;
55  }
56  else
57  {
58  fPeakValue = map(fSignalMax, 525, 1024, 0, 255);
59  fPeakToPeak = map((fSignalMax - fSignalMin) * (int)fGain, 0, 1023, 0, 255);
60  fSignalMax = 0; fSignalMin = 1024;
61  fStartPeriod = currentMillis;
62  }
63  }
64 
68  inline void setGain(byte gainFactor)
69  {
70  fGain = gainFactor;
71  }
72 
76  inline byte getPeakValue()
77  {
78  return fPeakToPeak;//peakValue;
79  }
80 
84  inline unsigned int getRawSample()
85  {
86  return fSample;
87  }
88 
89 protected:
90  byte fPin;
91  unsigned fPeriod;
92  unsigned fSampleCount;
93  unsigned long fStartPeriod;
94  unsigned int fSample;
95  unsigned int fSignalMax;
96  unsigned int fSignalMin;
98  byte fGain;
99 };
100 
101 #endif
MicrophoneAmplitude::fSampleCount
unsigned fSampleCount
Definition: MicrophoneAmplitude.h:92
ReelTwo.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
MicrophoneAmplitude::fGain
byte fGain
Definition: MicrophoneAmplitude.h:98
AnimatedEvent.h
PeakValueProvider.h
MicrophoneAmplitude
Reads a microphone amplitude and makes it available as PeakValueProvider input.
Definition: MicrophoneAmplitude.h:16
PeakValueProvider
Base class peak value providers, currently only microphone amplitude.
Definition: PeakValueProvider.h:13
MicrophoneAmplitude::fPeriod
unsigned fPeriod
Definition: MicrophoneAmplitude.h:91
MicrophoneAmplitude::fPeakToPeak
byte fPeakToPeak
Definition: MicrophoneAmplitude.h:97
MicrophoneAmplitude::getPeakValue
byte getPeakValue()
Definition: MicrophoneAmplitude.h:76
MicrophoneAmplitude::fSignalMin
unsigned int fSignalMin
Definition: MicrophoneAmplitude.h:96
MicrophoneAmplitude::fSample
unsigned int fSample
Definition: MicrophoneAmplitude.h:94
MicrophoneAmplitude::animate
virtual void animate()
Reads the microphone connected to the analog port specified by the constructor and calculates the amp...
Definition: MicrophoneAmplitude.h:41
MicrophoneAmplitude::MicrophoneAmplitude
MicrophoneAmplitude(const byte analogPin=0, const int periodMillis=50)
Default Constructor.
Definition: MicrophoneAmplitude.h:23
MicrophoneAmplitude::fPin
byte fPin
Definition: MicrophoneAmplitude.h:90
PeakValueProvider::fPeakValue
byte fPeakValue
Definition: PeakValueProvider.h:41
MicrophoneAmplitude::getRawSample
unsigned int getRawSample()
Definition: MicrophoneAmplitude.h:84
MicrophoneAmplitude::setGain
void setGain(byte gainFactor)
Set the gain.
Definition: MicrophoneAmplitude.h:68
MicrophoneAmplitude::fSignalMax
unsigned int fSignalMax
Definition: MicrophoneAmplitude.h:95
MicrophoneAmplitude::fStartPeriod
unsigned long fStartPeriod
Definition: MicrophoneAmplitude.h:93