Wednesday, August 27, 2014

Arduino example: Self adjust analogRead() to read Photoresistor

This example read analog input of Photoresistor, then turn on 8*8 LED Matrix accordingly.

- It self adjust by checking the maximun and minimum of analog input, then determine the level of light.
- If analog input is less than rejectMin or greater than rejectMax, it will be reject. It's assumed invalid due to bad contact of the breadboard.

Connection:
The resister connect between the photoresistor and GND is 10K ohm.


Program code:
/*
 *  Read Analog I/P from Photoresistor (Analog 0)
 *  and set LED Matrix accordingly
 *  http://arduino-er.blogspot.com/
 */

int PhotoResPin = 0;
int photoResVal = 0;
int photoResMax = 0x00;
int photoResMin = 0x3FF;
int level1, level2, level3, level4;
int level = 0;
const int levelMax = 4;
const int levelMin = 0;
const int NumOfLevel = 5;

const int rejectMin = 0x030;
const int rejectMax = 0x3CF;

// 2-dimensional array of row pin numbers:
const int row[8] = {
  2, 7, 19, 5, 13, 18, 12, 16
};

// 2-dimensional array of column pin numbers:
const int col[8] = {
  6, 11, 10, 3, 17, 4, 8, 9
};

// 2-dimensional array of pixels:
int pixels[8][8];

int count = 1000;

char str[] = "EDCBA";
int strLen = sizeof(str);
int ptrChar = 0;


typedef bool charMapType[8][8];

const charMapType charDummy = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType charMin = {
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1}
};

const charMapType char1 = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType char2 = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType char3 = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType charMax = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 1, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType *charMap[5] = {&charMin, &char1, &char2, &char3, &charMax};

void setup() {
  
  Serial.begin(9600);
  
  // initialize the I/O pins as outputs
  // iterate over the pins:
  for (int thisPin = 0; thisPin < 8; thisPin++) {
    // initialize the output pins:
    pinMode(col[thisPin], OUTPUT);
    pinMode(row[thisPin], OUTPUT);
    // take the col pins (i.e. the cathodes) high to ensure that
    // the LEDS are off:
    digitalWrite(col[thisPin], HIGH);
  }

  
  //setupScreen();
  setupChar();

}

void loop() {

  // draw the screen:
  refreshScreen();

  readPhotoRes();

}

void readPhotoRes(){
  
  int prvLevel = level;
  
  //read photoresistor from analog input
  photoResVal = analogRead(PhotoResPin);
  //Serial.println(photoResVal);

  if(photoResVal < rejectMin) return;  //assume invalid
  if(photoResVal > rejectMax) return;  //assume invalid
  
  if(photoResVal > photoResMax){
    photoResMax = photoResVal;
    adjLevel();
    level = levelMax;
  }else if(photoResVal < photoResMin){
    photoResMin = photoResVal;
    adjLevel();
    level = levelMin;
  }else{
    
    //check valid of photoResMax & photoResMin
    if(photoResMax > photoResMin){
      if(photoResVal < level1){
        level = 0;
      }else if(photoResVal < level2){
        level = 1;
      }else if(photoResVal < level3){
        level = 2;
      }else if(photoResVal < level4){
        level = 3;
      }else{
        level = levelMax;
      }
    }
  }
  
  if(prvLevel != level){
    setupChar();
  }
}

void adjLevel(){
  if(photoResMax > photoResMin){
    int div = (photoResMax - photoResMin)/NumOfLevel;
    level1 = photoResMin + div;
    level2 = level1 + div;
    level3 = level2 + div;
    level4 = level3 + div;
  }
}

void setupChar(){

  const charMapType *cMap = charMap[level];

  for (int x = 0; x < 8; x++) {
    for (int y = 0; y < 8; y++) {
      bool v = (*cMap)[x][y];
      
      if(v){
        pixels[x][y] = LOW;
      }else{
        pixels[x][y] = HIGH;
      }
    }
  }

}

void refreshScreen() {
  // iterate over the rows (anodes):
  for (int thisRow = 0; thisRow < 8; thisRow++) {
    // take the row pin (anode) high:
    digitalWrite(row[thisRow], HIGH);
    // iterate over the cols (cathodes):
    for (int thisCol = 0; thisCol < 8; thisCol++) {
      // get the state of the current pixel;
      int thisPixel = pixels[thisRow][thisCol];
      // when the row is HIGH and the col is LOW,
      // the LED where they meet turns on:
      digitalWrite(col[thisCol], thisPixel);
      // turn the pixel off:
      if (thisPixel == LOW) {
        digitalWrite(col[thisCol], HIGH);
      }
    }
    // take the row pin low to turn off the whole row:
    digitalWrite(row[thisRow], LOW);
  }
}

How it work:

In the video, the wrong color of LED is due to recording fps. It's not too significant visually.

No comments:

Post a Comment