A robot isn't truly a robot until it can sense its surrounds. It's time add a sensor. We'll start with a light sensor, but the technique is similar for many different types of sensors. Start by wiring in the light sensor to one of the analog pins. These pins are on the same side of the as the power plug and all labelled A0-A7. For this example, connect it to pin A0 (the one closest to the power plug), making sure the wires are connected as shown below.
The light sensor measures the amount of light hitting the disk on the front. This disk is a photoresistor or light-dependent resistor which mean that it resists the flow of electricity a different amount depending on how much light it sees.
Writing code for this requires the use of a variable. It will be set to the resistance of the photoresistor which will be constantly changing. Start by grabbing a set integer variable block from the Vaiables/Constants category. Change the variable name to Light. Replace the block in the value space with an analog pin block from the Pins category. Change the analog pin # to 0. Now that the amount of light is stored as the variable Light, we need to do something with it. Let's send that value back to the computer and display it on the screen. To do this we need a serial println block from the Communication category. Change "message" to "Light = ". This will be the first thing shown on the screen. Next we need to attach the variable to this line and the best way to do that is with the top glue in Communication (square on the left side and pointy on the right). Attach it to the "Light = " message block, then attach an integer var name block from Variables/Constants. Change the variable name to Light. Add a 100 millisecond delay and you're ready to try it out. After uploading the code, go to the Arduino IDE and click Tools >> Serial Monitor. This brings up a window where you can see the light values from your robot. Try covering the sensor with your hands. What happens to the value?
Looking at code, we start by declaring a variable called _ABVAR_1_Light with int and the variable name. This name was created by ArduBlock, but if you're writing code without ArduBlock, you can just call it Light or any other name you'd like. In the setup section the variable is set equal to 0 and we start serial communication with the computer at 9600 baud. In the loop, the variable is set to the input on analog pin 0 with _ABVAR_1_Light = analogRead(A0). Then we start sending messages back to the computer with the Serial.print command. The Serial.println near the bottom tells the computer to go to a new line on the Serial Monitor. Finally a short delay makes the monitor easier to read.
int _ABVAR_1_Light;
void setup()
{
Serial.begin(9600);
_ABVAR_1_Light = 0;
}
void loop()
{
_ABVAR_1_Light = analogRead(A0) ;
Serial.print( "Light =" );
Serial.print( _ABVAR_1_Light );
Serial.println("");
delay( 100 );
}