There is nothing really new in the code that we will use for this example. We will, just like in the previous one, simply read the digital input corresponding to the phototransistor and only if it is a HIGH
value, which means it is not receiving any light and thus not conducting, will we make the output LED blink.
Once again, just for the sake of simplicity, and given that now we know that we don't really need to store the value read with digitalRead()
and we can simply call the functions inside the if
condition, I have opted this time to save up this variable and simplify even a little more of the code:
/* Chapter 05 - Sensing the real world through digital inputs Optical coin detector By Francis Perea for Packt Publishing */ // Global variables we will use int led = 13; int phototransistor = 8; // Configuration of the board: one output and one input void setup() { pinMode(led, OUTPUT); pinMode(phototransistor, INPUT); } // Sketch execution loop void loop(){ // We read the optocoupler pin // and if the phototransistor doesn't receive light keep blinking if(digitalRead(phototransistor)==HIGH){ //simply blink digitalWrite(led, LOW); delay(50); digitalWrite(led, HIGH); delay(50); } }