Last active
January 30, 2022 21:44
-
-
Save mpflaga/69a03d32babe1935eb2675f6ef849a2b to your computer and use it in GitHub Desktop.
Arduino library of an array of 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 "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