MagicCode Lesson 7
Reading an Analog Signal
Read an analog Signal and display the output
For this example we use the potentiometer on the Magicbit board, which is connected to pin, D39. It generates a voltage between 0 and 3.3V according to the angle of the potentiometer.
We read the analog signal and store it in an int type variable(0v= 0 analog value, 3.3v = 4096 analog value), sensorValue, later, we use this value to light up the red LED(D27) if the analog value exceeds than 2048.
This program starts by clicking the green flag. The forever block continuously runs the code held inside the block. ‘If and else’ block controls the script according to the value of the potentiometer. If the potentiometer reading is greater than 512 then, “Set digital pin [pin] [state]” block set the digital pin 27 to “HIGH” and else set the digital pin 27 to “LOW”.
Note: Do the same example using the LDR on the board (D36)