Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Informatzah by Ido Gendel, 2014
- // Use freely, at your own risk.
- #include <LiquidCrystal.h>
- // LDRs are addressed via a 3-bit analog multiplexer
- // e.g. CD74HC4051E
- #define LDR_ADDR0_PIN A3
- #define LDR_ADDR1_PIN A4
- #define LDR_ADDR2_PIN A5
- #define SENSOR_PIN A1
- // Active LDRs - up to 8
- #define LDRS 6
- const byte MAX_READING = (1 << LDRS) - 1;
- // Baseline is the fully lit condition
- long int LDRBaseline[LDRS];
- byte currReading, stableReading, index, zeroExpected;
- LiquidCrystal lcd(8,9,4,5,6,7);
- //---------------------------------------------------------
- int getLDRReading(byte sensorID) {
- digitalWrite(LDR_ADDR0_PIN, sensorID & 1);
- digitalWrite(LDR_ADDR1_PIN, sensorID & 2);
- digitalWrite(LDR_ADDR2_PIN, sensorID & 4);
- delayMicroseconds(10);
- return analogRead(SENSOR_PIN);
- } // getLDRReading
- //---------------------------------------------------------
- void getLDRBaseline() {
- const long REPEATS = 64;
- int r;
- byte j;
- for (j = 0; j < LDRS; j++) {
- LDRBaseline[j] = 0;
- for (r = 0; r < REPEATS; r++)
- LDRBaseline[j] += getLDRReading(j);
- LDRBaseline[j] /= REPEATS;
- } // for
- } // getLDRBaseline
- //---------------------------------------------------------
- byte getFullReading() {
- byte j, result = 0;
- int reading;
- for (j = 0; j < LDRS; j++) {
- reading = getLDRReading(j);
- if (LDRBaseline[j] - reading < 100) result += 1 << j;
- } // for
- return result;
- } // getFullReading
- //---------------------------------------------------------
- void printBits(const byte b) {
- byte j = LDRS;
- // Assuming LCD cursor is at the correct position
- while (j--) {
- lcd.print(b & (1 << j) ? "1" : "0");
- } // while
- } // printBits
- //---------------------------------------------------------
- void resetReader() {
- lcd.clear();
- lcd.print("Matzah Reader!");
- stableReading = 0;
- index = 0;
- zeroExpected = 1;
- while (getFullReading() == MAX_READING) ;
- lcd.clear();
- } // resetReader
- //---------------------------------------------------------
- void setup() {
- pinMode(LDR_ADDR0_PIN, OUTPUT);
- pinMode(LDR_ADDR1_PIN, OUTPUT);
- pinMode(LDR_ADDR2_PIN, OUTPUT);
- lcd.begin(16, 2);
- getLDRBaseline();
- resetReader();
- } // setup
- //---------------------------------------------------------
- void loop() {
- lcd.home();
- currReading = getFullReading();
- printBits(currReading);
- switch (currReading) {
- case 0 : zeroExpected = 0;
- if (stableReading) {
- lcd.setCursor(index++, 1);
- lcd.write(stableReading + 31);
- stableReading = 0;
- }
- break;
- case MAX_READING :
- resetReader();
- break;
- default: if (!zeroExpected) stableReading |= currReading;
- } // switch
- delay(200);
- } // loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement