Follow and Avoid Light

Teach Sparki to move towards light, and to run away from it


Lessons You Should Know

In this tutorial, we’ll cover how to program Sparki to follow or avoid light.

You’ll Need


How It Works

Sparki has three light sensors that it can use to sense light:

Top - Light SensorSparki’s 3 light sensors

Each of Sparki’s light sensors can measure how much light is hitting them. The idea behind this follow light example is that the light is strongest on the sensor nearest to the light. Just like being further away from someone yelling makes it quieter, being further away makes the light dimmer:

Spark_Light_PropotionalStrength of light is proportional to distance from the source

The closer the sensor is to the light, the stronger the light from the source. In this example, the light arriving at the left sensor is going to be the strongest, the center sensor will receive the middle-most light, and the right sensor is going to get the least light.  

Programming It With Sparki

Warning_Triangle Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki. We will have Sparki following light using this idea and if statements. We will conduct three if tests. Each test will determine if the sensor in that test is getting more light than either of the two other sensors. If that sensor is getting more light than the other two, it will move in that sensor’s direction:

Spark_Light_TurningSparki will move in the direction of the strongest light source

Following Light

Try out the example code for light following in Examples->Follow_Light menu on your SparkiDuino programming environment. This code uses basic code, integers, the light sensor, the wheel code, and if statements:  

Avoiding Light

Of course, the same logic (and nearly the same program) can be used to have Sparki avoid light instead of following it: Spark_Dark_Turning You can find the code in Examples->Avoid_Light menu: