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 🙂 🙂

5 comments

  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

    • Hey no that doesn’t sound crazy but I am rubbish at diy and I really wouldn’t know where to start to make something myself. That’s why I was so excited to find Sparki as he’s prebuilt but programmable. I’m thinking of getting one of these in the near future although it too is prebuilt :

      http://4tronix.co.uk/store/index.php?rt=product/product&path=66_68&product_id=432

      Also, even if I did design my own hardware most things run on raspberry pi or arduino so being able to use arduino is pretty good 🙂

      I don’t think I’ll ever really be good enough to make something, mainly I don’t have much of an understanding of the electronics involved. I just like making stuff happen by programming 🙂

      Liked by 1 person

Leave a comment