Arduino photo resistor with potentiometer++


If we knew what we were doing, it wouldn’t be called research. 

Albert Einstein

In https://www.youtube.com/watch?v=dp_gSye8ke4 Dom’s MOBa channel the author wants to improve a simple photo resistor sketch/circuit by throwing a potentiometer into the game. His primary idea seems to be: with a separate potentiometer he could adjust the threshold of the circuit without changing the program. So far so good.

Sadly, he connects the potentiometer to a second Arduino input, gauges this port and uses this as an input threshold value for the other port. 

This seems to be a waste of resources, because actually you do not need the second port. And I am very stingy, when it comes to Arduino ports. 

So, if the photo resistor has a total reading range of let’s say 0..1023 and a useful threshold somewhere between 20 and 800, all you need is a ~ 20 Ω potentiometer, set in series with the photo resistor pin connected with A0. And here comes my sketch:

const int photoPort = A0;   // the port
const int threshold = 800;  // adjust for correct value
const int DELAY = 100;      // adjust delay between measurements

const char *message[] = {"DARKNESS", "ENLIGHTENMENT"}; // the const messages

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (analogRead(photoPort) < threshold)
    action(0);
  else
    action(1);
  delay(DELAY);
}

// do something dependent on the photoPort value
void action(int msg) {
  Serial.println(message[msg]);
}
, ,