Last active
January 31, 2022 01:12
-
-
Save mpflaga/91e45f9539b6cd1ebc58fedae70eb33c to your computer and use it in GitHub Desktop.
Arduino library of an array of averaged Analog Inputs connected PhotoCells (aka LDR)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "PhotCellArrayAveraged.h" | |
PhotoCell ldr[] = {{Pins{A0, A1}, 10}, {Pins{A2, A3}, 10}, {Pins{A4, A5}, 10}, {Pins{A6, A7}, 10}}; | |
unsigned period = 1000; | |
unsigned long nextPrintTime; | |
void setup() { | |
// initialize serial communications at 9600 bps | |
Serial.begin(500000); | |
nextPrintTime = (unsigned long) (millis() + period); | |
} | |
void loop() { | |
unsigned long currentTime = millis(); | |
for (int i = 0; i < ((sizeof(ldr) / sizeof(ldr[0]))); i++) { | |
ldr[i].updateReading(); | |
} | |
if (currentTime > nextPrintTime) { | |
nextPrintTime = (unsigned long) (currentTime + period); | |
for (int i = 0; i < ((sizeof(ldr) / sizeof(ldr[0]))); i++) { | |
Serial.print(ldr[i].average); | |
Serial.print("("); | |
Serial.print(ldr[i].thresholdStatus); | |
Serial.print("), "); | |
} | |
Serial.println(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "PhotCellArrayAveraged.h" | |
PhotoCell::PhotoCell(Pins _pins, int samples) { | |
pins = _pins; | |
pinMode(pins.sig, INPUT_PULLUP); | |
pinMode(pins.gnd, OUTPUT); | |
digitalWrite(pins.gnd, LOW); | |
m_samples = samples; | |
m_readings = new int[m_samples]; | |
m_period = 1000 / samples; | |
threshold = 100; | |
thresholdStatus = false; | |
reset(); | |
} | |
PhotoCell::~PhotoCell() { | |
delete [] m_readings; | |
} | |
int PhotoCell::read() { | |
return analogRead(pins.sig); | |
} | |
// add a new reading and return the new moving average | |
int PhotoCell::updateReading() { | |
unsigned long currentTime = millis(); | |
int newReading = analogRead(pins.sig); | |
if (currentTime > m_nextReadingTime) { | |
m_nextReadingTime = (unsigned long) (currentTime + m_period); | |
// add each new data point to the sum until the m_readings array is filled | |
if (m_nbrReadings < m_samples) | |
{ | |
++m_nbrReadings; | |
m_sum = m_sum + newReading; | |
} | |
// once the array is filled, subtract the oldest data point and add the new one | |
else | |
{ | |
m_sum = m_sum - m_readings[m_next] + newReading; | |
} | |
m_readings[m_next] = newReading; | |
if (++m_next >= m_samples) m_next = 0; | |
average = (m_sum + m_nbrReadings / 2) / m_nbrReadings; | |
thresholdStatus = (average > threshold) ? true : false; | |
return average; | |
} | |
} | |
// start the moving average over again | |
void PhotoCell::reset() { | |
m_nbrReadings = 0; | |
m_sum = 0; | |
m_next = 0; | |
m_nextReadingTime = (unsigned long) (millis() + m_period); | |
average = 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
struct Pins { | |
uint8_t sig; | |
uint8_t gnd; | |
}; | |
class PhotoCell { | |
public: | |
PhotoCell(Pins _pins, int samples); | |
~PhotoCell(); | |
int read(); | |
int updateReading(); | |
int average; | |
int getCount() { | |
return m_nbrReadings; | |
} | |
void reset(); | |
int* getReadings() { | |
return m_readings; | |
} | |
int threshold; | |
bool thresholdStatus; | |
protected: | |
Pins pins; | |
int m_samples; // number of data points for the moving average | |
int m_nbrReadings; // number of readings | |
long m_sum; // sum of the m_readings array | |
int m_next; // index to the next reading | |
int *m_readings; // pointer to the dynamically allocated samples array | |
unsigned m_period; | |
unsigned long m_nextReadingTime; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment