Light Following and Avoiding – MiniBloq and Sparkiduino

This week I decided to include an example of the code in both miniBloq and Sparkiduino.

Sparkiduino is a type of programming environment that uses Arduino syntax and is designed for programming Sparki, so is updated with Sparki’s library and example codes.

sparkiduino

I played around with light following and avoiding in this code and actually found attempting to guide Sparki with a torch is really fun and unpredictable as I only had control of a torch and not the other light sources.

In this code Sparki’s light sensors positioned along the front of his shell, measure the light input, print this input on the screen (only Sparkiduino version)  and then use conditional if statements to move towards the light. Obviously, to avoid the light simply program the robot to move in the opposite direction.

In the sparkiduino code Sparki prints out the light readings on the LCD screen
In the sparkiduino code Sparki prints out the light readings on the LCD screen

I used if statements instead of if/else statements as I wanted the measurements of light to be updated often to help Sparki respond quickly. If it was an if/else statement the code would jump straight to else based on the initial reading rather than taking a new reading and then deciding what to do.

Here is a screenshot of the miniBloq code:

Untitled3

and here is the Arduino based code:

#include <Sparki.h> // include the sparki library

void setup() 
{
}

void loop() {
  int left   = sparki.lightLeft();   // measure the left light sensor
  int center = sparki.lightCenter(); // measure the center light sensor
  int right  = sparki.lightRight();  // measure the right light sensor
  sparki.clearLCD();
  sparki.print("left = ");
  sparki.println(left);
  sparki.print("center = ");
  sparki.println(center);
  sparki.print("right = ");
  sparki.print(right);
  sparki.updateLCD();
  delay (2000);
  
  if ( (center > left) && (center > right) ) // if the center light is the strongest
  {  
    sparki.moveForward(); // move forward
  }

  if ( (left > center) && (left > right) )  // if the left light is the strongest
  {   
    sparki.moveLeft(); // turn left
  }

  if ( (right > center) && (right > left) )  // if the right light is the strongest
  {  
    sparki.moveRight(); // turn right
  }
  
  delay(100); // wait 0.1 seconds
}

Finally here’s a video of Sparki in action following the light source. WARNING at the end of the video my torch makes the light flash.

https://drive.google.com/file/d/0B6y9ITteBcqLc1BBOGZNMzVTaUk/view?usp=sharing

Let me know what you think 🙂 🙂

Advertisements

5 thoughts on “Light Following and Avoiding – MiniBloq and Sparkiduino

  1. Wow, I find this very interesting, I’m definitely even more curious about programming by watching your videos, and reading your screenshots.
    Very cool idea by the way.

    Just a quick question, are you ever planning on like, designing your own hardware, and then just program it?

    Maybe that sounds crazy, I might just not be as advanced as you, when it comes to programming. So I wouldn’t really know. :P.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s