Advertisement
igendel

Informatzah source code for Arduino

Apr 3rd, 2014
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. // Informatzah by Ido Gendel, 2014
  2. // Use freely, at your own risk.
  3.  
  4. #include <LiquidCrystal.h>
  5.  
  6. // LDRs are addressed via a 3-bit analog multiplexer
  7. // e.g. CD74HC4051E
  8. #define LDR_ADDR0_PIN A3
  9. #define LDR_ADDR1_PIN A4
  10. #define LDR_ADDR2_PIN A5
  11. #define SENSOR_PIN A1
  12.  
  13. // Active LDRs - up to 8
  14. #define LDRS 6
  15.  
  16. const byte MAX_READING = (1 << LDRS) - 1;
  17.  
  18. // Baseline is the fully lit condition
  19. long int LDRBaseline[LDRS];
  20.  
  21. byte currReading, stableReading, index, zeroExpected;
  22.  
  23. LiquidCrystal lcd(8,9,4,5,6,7);
  24.  
  25. //---------------------------------------------------------
  26. int getLDRReading(byte sensorID) {
  27.  
  28.   digitalWrite(LDR_ADDR0_PIN, sensorID & 1);
  29.   digitalWrite(LDR_ADDR1_PIN, sensorID & 2);
  30.   digitalWrite(LDR_ADDR2_PIN, sensorID & 4);  
  31.  
  32.   delayMicroseconds(10);
  33.   return analogRead(SENSOR_PIN);
  34.  
  35. } // getLDRReading
  36.  
  37. //---------------------------------------------------------
  38. void getLDRBaseline() {
  39.  
  40.   const long REPEATS = 64;
  41.   int r;
  42.   byte j;
  43.  
  44.   for (j = 0; j < LDRS; j++) {
  45.  
  46.     LDRBaseline[j] = 0;
  47.     for (r = 0; r < REPEATS; r++)
  48.       LDRBaseline[j] += getLDRReading(j);
  49.     LDRBaseline[j] /= REPEATS;
  50.    
  51.   } // for
  52.  
  53. } // getLDRBaseline
  54.  
  55. //---------------------------------------------------------
  56. byte getFullReading() {
  57.  
  58.   byte j, result = 0;
  59.   int reading;
  60.  
  61.   for (j = 0; j < LDRS; j++) {
  62.     reading = getLDRReading(j);
  63.     if (LDRBaseline[j] - reading < 100) result += 1 << j;
  64.   } // for
  65.  
  66.   return result;
  67.  
  68. } // getFullReading
  69.  
  70. //---------------------------------------------------------
  71. void printBits(const byte b) {
  72.  
  73.   byte j = LDRS;
  74.   //  Assuming LCD cursor is at the correct position
  75.   while (j--) {
  76.     lcd.print(b & (1 << j) ? "1" : "0");
  77.   } // while
  78.  
  79. } // printBits
  80.  
  81. //---------------------------------------------------------
  82. void resetReader() {
  83.  
  84.    lcd.clear();
  85.    lcd.print("Matzah Reader!");
  86.    stableReading = 0;
  87.    index = 0;
  88.    zeroExpected = 1;
  89.  
  90.    while (getFullReading() == MAX_READING) ;                    
  91.    lcd.clear();
  92.  
  93. } // resetReader
  94.  
  95. //---------------------------------------------------------
  96. void setup() {
  97.  
  98.    pinMode(LDR_ADDR0_PIN, OUTPUT);
  99.    pinMode(LDR_ADDR1_PIN, OUTPUT);
  100.    pinMode(LDR_ADDR2_PIN, OUTPUT);  
  101.  
  102.   lcd.begin(16, 2);
  103.  
  104.   getLDRBaseline();
  105.   resetReader();
  106.  
  107. } // setup
  108.  
  109. //---------------------------------------------------------
  110. void loop() {
  111.  
  112.   lcd.home();
  113.  
  114.   currReading = getFullReading();
  115.   printBits(currReading);
  116.  
  117.   switch (currReading) {
  118.    
  119.     case 0 : zeroExpected = 0;
  120.              if (stableReading) {
  121.                lcd.setCursor(index++, 1);
  122.                lcd.write(stableReading + 31);
  123.                stableReading = 0;  
  124.              }
  125.              break;
  126.                  
  127.     case MAX_READING :
  128.              resetReader();
  129.              break;
  130.    
  131.     default: if (!zeroExpected) stableReading |= currReading;
  132.    
  133.   } // switch
  134.  
  135.   delay(200);
  136.  
  137. } // loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement