Skip to content

Instantly share code, notes, and snippets.

@mpflaga
Last active January 30, 2022 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpflaga/69a03d32babe1935eb2675f6ef849a2b to your computer and use it in GitHub Desktop.
Save mpflaga/69a03d32babe1935eb2675f6ef849a2b to your computer and use it in GitHub Desktop.
Arduino library of an array of Analog Inputs connected PhotoCells (aka LDR)
#include "Arduino.h"
struct Pins {
uint8_t sig;
uint8_t gnd;
};
class PhotoCell
{
public:
PhotoCell(Pins _pins);
int read();
protected:
Pins pins;
};
PhotoCell::PhotoCell(Pins _pins)
{
pins = _pins;
pinMode(pins.sig, INPUT_PULLUP);
pinMode(pins.gnd, OUTPUT);
digitalWrite(pins.gnd, LOW);
}
int PhotoCell::read() {
return analogRead(pins.sig);
}
PhotoCell ldr[] = {Pins{A0, A1}, Pins{A2, A3}, Pins{A4, A5}, Pins{A6, A7}};
void setup()
{
// initialize serial communications at 9600 bps
Serial.begin(9600);
}
void loop()
{
for (int i = 0; i < ((sizeof(ldr) / sizeof(ldr[0]))); i++) {
Serial.print(ldr[i].read());
Serial.print(", ");
}
Serial.println();
delay(250);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment