1
SKILL
LEVEL
Ringo Educational Guide Rev04.1 ~ Plum Geek
Using Ringo’s Light Sensors
#include “RingoHardware.h” //include Ringo background functions
int sensorValue;
//declares variable “sensorValue”
void setup(){
HardwareBegin(); //initialize Ringo’s circuitry
PlayStartChirp(); //play startup chirp and blink eyes
}
void loop(){
digitalWrite(Source_Select, HIGH);
//select top side light sensors
sensorValue = analogRead(LightSense_Right);
//read right light sensor
Serial.println(sensorValue);
//print sensorValue through serial port
Serial.println();
//print a blank line feed
delay(250);
//delay 250 milliseconds
}
The best way to see how the sensors work is to use the Arduino
Serial.print
functions to send the light sensor readings to your computer screen so you can
see how they change as you point lights at Ringo, or cast shadows on his sensors.
Plug Ringo into his programming adaptor, then plug the programming adaptor into
your computer. Go ahead and leave the adaptor connected to Ringo during this
lesson. Let’s load up the following code example and see what happens.
In previous examples we simply told Ringo to do something. In this example,
we’re going to read a value from a sensor. Ringo needs to mark a location in his
memory space to remember the value when it is read from the sensor. The line
int
sensorValue;
“declares” the variable “sensorValue”. Once the variable is declared,
the variable can be used to store a value, to perform an operation on a value (like
adding a number to it), or to recall the value from it.
We then use
digitalWrite
to set the
Source_Select
pin to
HIGH
, which enables
the top side light sensors. Next we tell Ringo to perform an analog read of the
LightSense_Right
pin, and place the result of that analog read into our variable
called
sensorValue
. An analog read simply creates a numerical value based on
a voltage created by the light sensor. As the light is increased, this voltage is
increased and thus, the result of the
analogRead
will increase.
Ringo now knows the present light level of his sensor (stored in the
sensorValue
variable), but you can’t see it yet because it’s only
stored in Ringo’s brain. The line
Serial.println(sensorValue);