Filters
Close
RSS

Blog

A Word about Frobo

Frobo is an initiative by hobbyandyou team to help children learn basic concepts of electronics, crafts and programming. We earnestly believe in improving learning abilities by context based education. Frobo is a modular teaching based toy craft programme.  We will be happy to work with education professionals and schools.

Module FroboV1-

This is the beginners craft kit aiming at teaching model building skills with basic electrical knowledge of voltage, current, resistance, load and conversion of energy into light, sound and motion. This craft kit is meant for children above 8 years. This module will help improve the following skills.

  • Identifying different parts of the model
  • Ability to visualize the process of assembly
  • Familiarilty with different electrical components and their function
  • Wiring the basic circuit with understanding of basic electricity and energy conversion
  • Building a functional hopping Frobo with motion, light and sound

Module FroboV2-

This kit is also for beginners with additional components. This kit becomes the platform for kids to add programmable micro controller with sensors. This model will allow kids to understand concepts of motion in greater detail. We created this kit as an intermediate after they have become familair with basic concepts of model building and electricity. This kit is a perfect choice for kids above 8 years.

Module FroboV3-

This add on modular kit is for those who have assembled their V2 model and are now ready to start their first lessons in controlling the Frobo. This is foundation training in programming in arduino- the robotics most popular platform. This module provides a comprehensive training in connecting and programming different kinds of sensors and motor control methods. We strongly believe that children need to relate to what they are being taught. This training will enable them to improve the following.

  • Introduction to Arduino programming
  • Learning to control motors and lights
  • Learning to read different sensors and interpret
  • Improving problem identification and finding solutions 

This module is a comprehensive package for learning basic to advanced arduino programming and hardware configuration. We have designed step by step instructions to guide children to learn basic programming and then slowly challege them to do more complex tasks.

Module FroboV4-

This is the most advanced module of this project. By the end of completion of this module, the child has already reached a stage of comfort with the robotic technology and  applications. This module introduces-

  • Radio transmission and receiving
  • Remote control programming
  • Two way wireless communication

We designed this module to augment understanding of everyday technology, we all use but know little about. This part will help stimulate creative genius of kids and will prepare them to think beyond boundaries. Frobo team is contantly working on more advanced training modules to help children realize their potential and help them discover themselves.

Frobo V3 assembling instructions

Frobo V3 can be controlled by programming. Before we do that, let us now go over what Frobo V3 kit includes-

The kit includes-

  1. Control Circuit PCB
  2. Arduino Uno with cable
  3. 1 X HC-SR4 ultrasonic sonar sensor module
  4. Back locking plate for Sonar
  5. Front cover with CDS photo sensors module
  6. 1 X Mini piezo buzzer
  7. 1 X IR diode
  8. 1 X IR photo sensor
  9. 1 X Mini servo for front wheel

Assembly instructions Frobo V3-

            

  1. It is easy to mount the motors on the lower frame. It is done with the help of a pair of motor-holders for each motor. There are 4 slots to insert these holders in the broader rear part of the lower frame. After inserting the holders and placing the motor in between them, use two screws to fix each motor. Please see the pictures.                                                                                                                                                     
  2. Once the motors are mounted, wheels can be fixed on the shafts extending outwards.                                                                                                                            
  3. Mount the control circuit PCB with screws and nuts on the lower frame. There are four holes of about 5mm each about 2cm from the front end of the lower frame. One IR diode and one photosensor are to be inserted one each side. These need to be inserted from below the frame. Please see picture.
  4. Join the Arduino Nano microcontroller with the help of the 30 PIN adaptor to the included PCB—as  per the given diagram, so that micro controller is connected properly. It should be fixed in correct orientation. If you fix it the other way, micro controller will not work. 
  5. Take the upper frame and glue the frog theme sticker on it carefully.                                                                                              
  6. A total of 7 LEDs need to be fixed on the upper frame. 2 green LEDs in the front, and 5 (2green+2red+1yellow) LEDs in the rear part. Further, 1 IR diode and 1 photosensor are to be fixed in the centre of the broader part of upper frame. A total of 9 holes are provided in the upper frame for this purpose.
  7. Mounting the front castor wheel along with the servo on the lower frame is the next step. As per the image given below, first fix the small white plastic strap with the black U-shaped metallic holder for wheel. Now, the white wheel should be fixed to its holder with the nut and screw provided. This assembly will be fixed to the lower frame from below, while the servo will be attached from above. Both these will be fixed on lower frame with the help of a single screw.                                                                                                                                  
  8. Now, ultrasonic sensor is to be fixed—with the help of 4 small screws—on a small rectangular piece of sheet provided (the back locking plate). The 2 CDS sensors (LDR) are to be inserted one on each side of this sheet—tiny holes are there. This rectangular frame can then be inserted on the front end of the lower frame with the help of two tiny extensions to match with the tiny slots.  A similar tiny extension will fit in the corresponding slot on the upper frame when we place the upper frame on the lower frame. But it will be done after wiring is done. The upper frame will join the lower frame with the help of 3 brass spacers. One screw on each end of every spacer will fix the two frames together.
  9. It is now time for understanding the programming part. We will start learning in parts different programming modules for different sensors and motion types. 

Wiring instructions Frobo V3-

Please see the wiring diagram below for clear understanding. Correct wiring is very important for the Frobo to function properly.

  1. Connect Red left motor wire to '+ve' "M1" port and green to '-ve' port. Doing it in reverse order will make motor move in opposite direction.
  2. Similarly, connect Right motor wires to port "M2".
  3. Connect Servo Red, green and orange wires to "S" port '+ve', '-ve', 'O' respectively. 
  4. Connect left led Red and Green wires to "L1" port '+ve' and '-ve', and right led to L2 port.
  5. Connect buzzer Red and Green wires to "B" '+ve' and '-ve' ports.
  6. Connect left CDS sensor Red, Green and Orange wires to "C1" '+ve', '-ve', and 'O' ports and right CDS to "C2" respectively.
  7. Connect IR diode Red and Green wires to "I" port '+ve' and '- ve' and Photo sensor Red and Orange to "P" '+ve' and 'O' ports.
  8. Connect battery terminals Read and Green wires to "PW" '+ve' and '-ve' ports. 

Test the wiring-

The micro controller come pre-programmed with wiring test. Just take our micro controller from arduino Uno development borad carefully and fix it on the 28 Pin adaptor on the control PCB. Fix Motor driver IC 293D (14 Pin) in 14 pin adaptor and Op-amp LM358 (8 Pin) in the adaptor respectively.

This wiring test program checks if your wiring is correct or not. Please observe carefully. If the steps listed below do not show as stated, check the wiring and try again. In case of persistent problem, contact customer_service@hobbyandyou.com.

  1. Slide the switch on and wait 2-3 seconds. The leds will flash in sequence along with buzzer. 
  2. Now, after led flashing, left led will glow and right will turn off and left motor will make forward and backward motion. Your left motor is connected right.
  3. The same sequence will reapeat for right motor.
  4. After motor tests, both leds will start flasing alternately and servo will move left and then right. Your servo wiring is right.
  5. Now bring your hand right 6 inches  in front of the ultrosonic sonar.  The leds will glow bright. It means ultrasonic sensor is reading your movement. 
  6. Now bring your hand 2 cms near the Ir Photo diode. Both the leds will flash together. It means IR photo sensor senses your hand.
  7. Cover left CDS sensor on the front cover with your finger, the servo will move left side and cover the right CDS sensor and servo will move right.

After testing the wiring, You are now ready to experiment with your first Arduino program.

Frobo V3 Arduino programming PART-1

Frobo V3 Arduino programming PART-2

Frobo V3 Arduino programming PART-3

Frobo V3 Arduino programming PART-4

Frobo V3 Arduino programming PART-5

Frobo V3 Arduino programming PART-6

Frobo V4 Arduino programming PART-7

Frobo V3 Programming Lesson -Part 1

Welcome to the Arduino programming lesson part 1. 

What is arduino?

Arduino is an open source electronics programming software and harware. You can use Arduino to read sensor input like a switch, echo of sound, light relections and millions of other inputs and create commands to control motors, lights and other actuators. Arduino is simply the most popular robotic learning platform for students, hobbyists and professionals. This being an open source platform, a vast amount of information is easily available to develop thousands of projects.

Learn more about Arduino

What is Froboduino?

Froboduino is an initiative by Hobbyandyou.com team to take this movement forward to teach children this the fundamentals of electricity an electronics. We will guide you through thse easy to undersatnd lessons in several parts. We are sure that after the completion of Frobo module V4, you will be in position to develop your own applications of increasing complexities. We have created our own development board called froboduino with a motor driver and a two channel Op-Amp LM358 to enable children to develop not only Frobo module but also a lot of other useful projects. Froboduino can be programmed with Arduino uno or any FTDI programmer. Separate programming ports are provided for easy to use programming.

How to get started?

You will need a laptop or a desktop to get started learning this. You can do with any laptop with windows, MAC or LNUX based operating systems. Arduino has a lot of older versions available for different generations of systems.

You need to download right version of Arduino IDE (Integrated development environment) from the link below.

Arduino IDE download

After downloading the IDE, instal the IDE by following instructions. You can find detailed instructions here.

Your Frobo V3 kit includes a separate Arduino Uno as a programmer and also as your indenpendent programming board for other applications. On the Froboduino board there is 4 Pin programming port marked as PR. The port 1 of PR goes to RSET. We need to connect RSET to the next 5V port. There is two pin jumper in te kit. Connect RSET and 5V with the jumper. Connect PIN2 of PR to Rx piun on Uno and Pin3 to Tx pin of arduino. Connect Uno to your compuer with the USB cable. Take two red Leds from the kit and connect Red wire to L1 port +ve and Green wire to L1 port -ve, similarly, connect the other Led to L2 +ve and -ve ports.

This Arduino Uno comes with a USB 2.0 Male tyeA to Male type B  perpheral cable. Connect USB to your compuer USB port and the other male B type to your Uno. Click open Arduino IDE and from top menu select "Tools > Boards> Arduino Nano

Arduino>Tools.Boards>Arduino Nano

Now, select "Tools>Ports> right port  (Usually COM3, COM5 etc on windows). (See picture below)

Arduino>tools>ports

After you have succesfully set up your arduino IDE. It is now time for your first program on Froboduino. We will first begin with connecting motors and controlling motion with two geared motors provided with Frobo kit. Connect +ve terminal of left Motor to M1+ and - ve terminal to M1-. Similarly connect right Motor to M2 +ve and -ve ports on Froboduino. Let us look at the motor controlling code.

 

// Connect left motor +ve terminal to M1+ and -ve to M1- ports and similarly connect M2.
// Declare constants.

// accelerator is used to control Frobo speed. Remember PWM!
const int accelerator = 5;
//Moves left motor in forward direction.
const int leftMotorFwd = A0;
// Moves left motor is backward direction.
const int leftMotorRev = A1;
//Moves right motor in forward direction.
const int rightMotorFwd = A2;
// Moves right motor in backward direction.
const int rightMotorRev = A3;

//We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts.

void setup()

{
// Define LEDs and Motors as outputs. Inputs are defined by default.

pinMode(accelerator, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorRev, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorRev, OUTPUT);
// Let us open the serial ports with data rate 9600 bits /second

Serial.begin(9600);

}

//This loop code runs repeatedly

void loop()

{
//This is the basic froboduino code for controlling motors to move in different directions.
//This code will move Frobo forwards for 30 seconds.
// accelerator powers the motors, so any movement, we have to set it "High".
digitalWrite (accelerator, HIGH);
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);

// Let this motion continue for 30 seconds (30000 mili seconds).
delay(30000);


// This code will now turn frobo left for 5 seconds and will move it forward for 10 seconds.
// by stopping left motor and running right motor, we will move Frobo in left direction.
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);

delay(5000);

digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);

delay(10000);

//This code will Frobo backwards for 15 seconds.
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, HIGH);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, HIGH);

delay(15000);


//This code will turn frobo in right direction for 5 seconds in backward motion.
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, HIGH);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, LOW);

delay(5000);

// This code will stopFrobo for 5 seconds.
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, LOW);

delay(5000);

//Imagine controlling motion to create dance effects. Try moving from forward for 1 second and then turn left for a second and turn right for a second and make it move backwards for 4 seconds. This is an exercise for you.

}

How did you like your your first lesson in Frobduino/Arduino? This is just the beginning of an exciting series of robotic lessons. In the subsequent parts, we will learn about other output devices and sensor data reading. 

Frobo V3 programming lesson- Part 2

Frobo V3 programming lesson Part 2

In part 2 of Froboduino programming lessons, we will look at connection and control of Frobo LEDs as glwoing eyes. Look at the Arduino Led blinking code below for basic understanding. This kit contains 12 Leds in three colours; red, green, yellow, RGB. Have a close look at any Led. Each Led (Other than RGB) has two legs each with different lengths. The longer leg is +ve and shorter one is -ve (Ground). Like any other Diode, Led also allows current to flow from +ve to -ve. If connected wrongly, the Led will not work. Connect left Led to L1+ and L1 -ve ports and similarly connect right Led to L2 ports. Insert the Leds to eye holes on upper frame of Frobo. Connect your Frobo micro USB port to your computer and select Arduino Nano and ports (As we learnt in lesson Part 1).

Now, time to look at the code.

//Let us first declare which pins of froboduino are connected to left Led and right Led.

// On froboduino these are connected to programming pin 9 and pin 10 (Both are PWM capable, we will learn about that shortly)

// We will discuss later why actual pin numbers on 328P are different from programming pin numbers.

const int ledLeft = 9;

const int ledRight = 10;

// setup code runs once when the froboduino is swtched on.

void setup()

{

// Here, we declare that ledLeft and ledRight are Outputs. It is important. Inputs are taken by default.

pinMode(ledLeft, OUTPUT);

pinMode(ledRight, OUTPUT);

//This is meant for program testing in serial monitor.

Serial.begin(9600);

}

// This code runs again and again when froboduino is powered.

void loop()

{

//This code is to turn left Led on for 100 milli seconds then off for 100 mili seconds. i second = 1000 milli seconds

digitalWrite(ledLeft, HIGH);

// This stops the program for 100 milliseconds, but last commands will continue. Left Led will remain on for 100 milli seconds.

delay(100);

digitalWrite(ledLeft, LOW);

delay(100);

//This code is to turn right Led on for 100 milli seconds then off for 100 milli seconds. 

digitalWrite(ledRight, HIGH);

delay(100);

digitalWrite(ledRight, LOW);

delay(100);

Copy the above the code, and paste in your arduino dialog screen and save. After saving the code, click on "Compile" on top right of your IDE and see if the code complies ok. Now, if your froboduino is connected to your computer via Arduino uno, click "Upload". See the bottom of IDE for uploading progress window. If your IDE says "Done uploading", your program has been successfully uploaded on your froboduino. Your leds will start blinking alternately. Whoo.., you have just become an Froboduino/Arduino programmer. Congratulations !.

This is just the beginning of your Froboduino journey. Let us look at some important things to know at this moment.

"delay() function is very often used in Arduino programming, but this function causes the program to stop while it lasts. The micro processor cannot handle any commands during this period. It is recommended to use 'delay()' only when the MCU is not required to handle any other commands or read "Input data".

How do we blink Leds without using the 'delay()' function.

Below is the code that will help you understand how to use millis() function to do just the same. 'millis()' is very useful function that uses the timer to calculate time from the last event and then executes the command scheduled after that. The good thing is that this function doesn't stop the program from doing other tasks.

Copy the code below.

// Declare pins for your Leds. const int, #define are used to define fixed pins.

const int ledLeft = 7;

const int ledRight = 8;

//Declare led state variables below. These define the current state of Leds. Notice that we are setting one state "LOW" and the other state "HIGH"

int ledLeftState = LOW;

int ledRightState = HIGH;

/*Declare time interval variables below. Notice that we are using "unsigned long" here and we used "int" earlier for led states. Why? 

Please know about these different variable types below.*/

unsigned long previousMillis = 0;

unsigned long interval = 100;

unsigned long currentMillis = 0;

// setup code runs once when the froboduino is swtched on.

void setup()

{

// Here, we declare that ledLeft and ledRight are Outputs. It is important. Inputs are taken by default.

pinMode(ledLeft, OUTPUT);

pinMode(ledRight, OUTPUT);

//This is meant for program testing in serial monitor.

Serial.begin(9600);

}

// This code runs again and again when froboduino is powered.

void loop()

{

//This  is to set the value of "currentMillis" equal to milli seconds since the program started.

currentMillis = millis();

/*previousMillis began with value 0, so we check the difference of currentMillis and previousMillis, and if this bigger than or equal to "interval"

(defined in variables in starting), we command to change the led states*/

if ((currentMillis - previousMillis) >= interval){

previousMillis = currentMillis;

if (ledLeftState == LOW && ledRightState == HIGH){

ledLeftState = HIGH;

ledRightState = LOW;

}

// if the above statement is not correct, that is if two Led states are different, we give an "otherwise" command with "else".

else {

ledLeftState = LOW;

ledRightState = HIGH;

}

// Here we command the left and right Leds to follow the states. If ledLeftState is HIGH, the left Led will turn on.

digitalWrite(ledLeft, ledLeftState);

digitalWrite(ledRight, ledRightState);

/*You can read about various variable types here. https://www.arduino.cc/en/Reference/VariableDeclaration 

Variable "int" is 2 byte (1 byte is 8 bits, so it is 16 bit number) variable which can be both negative and postive number. the range of "int" variable value is -32,768 to 32,767, which is equal to -2^15  to 2^15-1.

Variable "unsigned long" is 4 byte(32 bit) number with maximum value of 2^32-1, which is 4,294,967,295. All unsigned variables are only positive numbers. This is a useful variable to store time in milli second and micro second.

IMPORTANT- After the variable value reaches the maximum , it rolls over to the minimum value.*/

}

}

I think, this is already too much for just lesson part 2. So we will stop here. Think deeper about the functions described above. Try to create your own code to for creating diffrent patterns for Leds blinking. Let us know on this forum if you have any questions.

Frobo V3 Programming Lesson- Part 3

In this lesson, we will learn about setting up a servo steering. Servos are small box-shaped electro-mechanical devices that contain a DC motor, electronics to control the motor from a signal, a gear system to produce slow/strong output to a shaft. Normally, robotic servos are designed to sweep 180 degrees, but there are servos available that rotate full circle for special applications. 

Servos usually come with several different arms you can attach depending on your design. Usually you use a Servo to position something, like steering direction, arm/leg movement etc. For most servos the position angle of the servo is controlled by the length of the pulse in milliseconds (usually between about 1.00-2.00ms). 1.50ms is usually Center, or Stopped (For Robot Servo).Plastic gear micro servo for robots

Normally, servos have three wire connections. One (usually red) is for power supply (in this case 5V), the orange or white is usually signal wire to control servo and green or black fror ground. The signal wire here is connected to the Froboduino microcontroller pin D6 viw servo signal port. Insert servo three wires to the servo port while make sure that signal, 5V and ground connect to the right ports on froboduino.

Now, it's time for some coding. The reason why we waited for all this while for servo attachment, because it introduces a new feature in arduino coding. Froboduino requires a special library called "Servo.h". Just like other programming environments, Froboduino/Arduino also uses "libraries" to extend the functions of the program. Arduino IDE comes with a number preloaded libraries. You can see in Arduino>sketch>include library to check which libraries are already installed. Libraries can also be accessed through file explorer- documents>arduino>libraries. Other than these installed libraries, you can also add other libraries as you will learn in further lessons.

Before you attach the wheel clamp to the servo, we first need to know the middle position of the gear on servo. For marking the middle position, you should upload this code in arduino. Yoiu will notice that we have added a delay of 5 seconds in "set up" code where we declare "steeringServo.write(90);" as the middle position. Servo motor moves 0 tp 180 degrees. Mark the position and remove cable from Froboduino and mouth clamp such that front wheel is aligned with the middle position.

Its' coding time now.

 

//

// We need to include servo.h library. Servo.h library is included in all latest Arduino IDE versions as a standard Arduino Library.
#include <Servo.h>
//delare servo as object.
Servo steeringServo;

// accelerator is used to control Frobo speed. Remember PWM!
const int accelerator = 5;
//Moves left motor in forward direction.
const int leftMotorFwd = A0;
// Moves left motor is backward direction.
const int leftMotorRev = A1;
//Moves right motor in forward direction.
const int rightMotorFwd = A2;
// Moves right motor in backward direction.
const int rightMotorRev = A3;
//Servo motor servoPosition. SErvo sweeps 0- 180 degress. We set the servo right in the middle at start.
int servoPos;

//We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts.

void setup()

{
// Define LEDs and Motors as outputs. Inputs are defined by default.

pinMode(accelerator, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorRev, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorRev, OUTPUT);
// Let us open the serial ports with data rate 9600 bits /second
steeringServo.attach(6);
Serial.begin(9600);
//Declare starting postion. Use this to set your wheel.
steeringServo.write(90);

//Use delay of 5 seconds first time so you can mark centre postion to mount your wheel.

delay(5000);
}

//This loop code runs repeatedly

void loop()

{
//This is the basic froboduino code for controlling servo to move in different directions.
//This code will move steering wheel 90 degrees left and then 180 degrees right, then it will come back to middle.
// Now we declare the starting servoPostion od steeringServo at 90 and turn by 1 degree each to full left at 0 degree.
// Read up on how to use "for" statements. These statements are used to repeat statement between two curly braces.

Serial.println("turning right");
for (servoPos = 90; servoPos <= 180; servoPos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
steeringServo.write(servoPos); // tell servo to go to servoPosition in variable 'servoPos'
delay(15); // waits 15ms for the servo to reach the servoPosition
}
delay(2000);
Serial.println("turning left");

for (servoPos = 180; servoPos >= 0; servoPos -= 1) { // goes from 180 degrees to 0 degrees
steeringServo.write(servoPos); // tell servo to go to servoPosition in variable 'servoPos'
delay(15); // waits 15ms for the servo to reach the servoPosition
}
delay(2000);
Serial.println("Back to the middle");

for (servoPos = 0; servoPos <= 90; servoPos += 1) { // goes from 180 degrees to 0 degrees
steeringServo.write(servoPos); // tell servo to go to servoPosition in variable 'servoPos'
delay(15); // waits 15ms for the servo to reach the servoPosition
}
delay(2000);
}

Now, our Frobo is quite well equipped with hardware of diffrent kinds. It's now time to create a composite program to enable Frobo to do multiple tasks with a single program. This will require some patience. So, we will end this lesson here. The next lesson will be your graduation into the professional programming. In case of any queries, please ask through comments section.

Frobo V3 Programming Lesson- Part 4

So far, we learnt uisning Leds as output and how to blink them in different patterns. Let us know try to learn how to read an input from a sensor. Frobo V3 kit comes with 3 different kinds of input sensors. 

  1. HC-SR04 ultrasonic sensor. This works on the principle of sound echo. 
  2. CDS- LDR Photo Sensor is a light sensitive resistive sensor, i .e. the sensor changes it's electrical resistance when brightness of light falling on it changes. We measure the resistance change and calvcualte the light brightness. We will learn more of it lesson- Part 4.
  3. IR Photocouple Sensor is a special sensor to measure reflected infrared to measure distance from a nearby object. This sensor is used in many applications like automobiles and industrial applications. We will learn more on this sensor is lesson Part 5.

This lesson will teach you about using ultrasonic sensor to calcualte distance of Frobo V3 from an object in front. Let us first try to understand what is ultrasonic and how it is different from other sounds.

DO YOU KNOW WHY YOU DON'T HEAR ULTRASONIC?

Sound travels like a wave. A wave is defined as a progression of energy from one point to another through a medium without transporting the medium. A wave travels by giving energy to the neighboring particles of the medium. These particles start oscillating and further transfer energy to the next particle. The waves propagate through the medium with a specific frequency, wavelength and amplitude. Wave length is inversely proportional to frequency, i.e. higher the frequency, lower the wave length. The amplitude refers to the maximum extent of oscillation. Please see the diagram below to understand the difference. Human ear is designed to hear sound frequencies between 20 Hz (Sound with wave frequency which goes 20 times up and down in a second) and 20,000 Hz or 20 KHz (Kilo Hertz). Any sound frequency below or above is heard by normal huma ear. So, the sound frequencies between 20 Hz to 20 KHz are know as SONIC waves. Below 20 Hz are called INFRASONIC and above 20KHz are know as ULTRASONIC.

You must read more about sound. Here is a useful link for some important details.

 What is HC-SR04 Ultrasonic Sensor?

Frobo V3 HC-SR04 Ultrasonic Sensor   

 Do you know how sound echoes? Have you ever noticed in an empty room, your voice repeats. This happens because sound waves travel through air and when the oscillating air particles collide with a solid object, it bounces them back, much in the same way as a ball bounces back. When the room is empty, these reflected sound waves reach your ears like repeated sounds. Our sensor HC-SR04 uses the same principle. It emits very high frequency (40 KHz) ultrasonic from its frequency generator and when these ultrasonic waves collide with an object (within 4 meters), it creates ultrasonic echo. The sensor has an ultrasonic echo transducer which converts these reflected waves to electric voltage. This echo volt is then amplified through an onboard amplifier. Our froboduino pin 24 (Arduino programming pin A1) receives this echo voltage as a signal. We record the time difference between when ultrasonic sound was generated and when echo was received to calculate how much time passed before echoes were received. We know the spped of  sound in air 343meters/second. So we can calculate how far the object is. You will learn more about distance calculation below when we explain the program to read this sensor.

This 4 pin easy to use sensor is very popular in robotic applications. Two pins are used to power the device with 5V and ground and the other two pins make the device work. One pin is called "Trig" or "Trigger". This pin is set to HIGH voltage by MCU and it starts emitting ultrasonic sound waves. The other pin is called "Echo". This pin sends data to the MCU when it receives a reflected ultrasonic echo after colliding with an object. This sensor has a detection range of approximatley 2 cms to 4 meters. Beyond 4 meters the reflected sound intensity drops below capacity of this sensor.

Please check datasheet of the sensor here. The datasheet carries all technical specifiactions for the sensor by the manufacturer.

Now, let us look at the program to read the sensor distance measurement program.

 
// define pins numbers for "Trig" and "Echo" Pins of HC-SR04. 
//Our Froboduino Ultrasonic (U) ports are connected to arduino pin D2 and D3. const int trigPin = 2; const int echoPin = 3; // defines variables long duration; int distance; void setup() {
// Sets the trigPin as an Output
pinMode(trigPin, OUTPUT); 
 // Sets the echoPin as an Input

pinMode(echoPin, INPUT);
// Starts the serial communication

Serial.begin(9600); }

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
 // Sets the trigPin on HIGH state for 5 micro seconds 
digitalWrite(trigPin, HIGH); 
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance. Sound travel at 343 M/s. So distance =durationX343 Meters/second = durationX34300 cms/ 1000*1000 microseconds = duration*.0343
// Divide the total distance travelled by sound by 2 as it goes and comes back to get distance from object.
distance= duration*0.0343/2;
// Prints the distance on the Serial Monitor . Serail monitor is not left top side of your arduino IDE.
// Make sure to select right board and prt from tool menu.
Serial.print("Distance: ");
Serial.println(distance); }

Try uploading this program on your arduino Uno and connect Ultrasonic to Arduino Pins "Trig" 14 (A0), "Echo" to 15 (A1) and %v to 5V and GND to GND and read the values on your serial monitor.

Now, that we can measure distance of an object, Can we make this data to actuate some "output" when distance reads below and above certain value. We already learnt about blinking Leds. Ok, Let's try the following-

  • When distance measured is below 10 cms . Light up left Led.
  • When distance measured is graeter than 10 but less than 1000 cms( 1 meter). Light up right Led.
  • When distance measured is greater than 1000 cms. Light up both Leds.
 

// define pins numbers for "Trig" and "Echo" Pins of HC-SR04.
//Our Froboduino Ultrasonic (U) ports are connected to arduino pin D2 and.
// Define left and right Leds pins to D9, D10
const int trigPin = 2;
const int echoPin = 3;
const int leftLed = 9;
const int rightLed = 10;

// defines variables
long duration;
int distance;

void setup() {

// Set left Led as output.

pinMode(leftLed, OUTPUT);

// Set right Led as output.

pinMode(rightLed, OUTPUT);
// Sets the trigPin as an Output
pinMode(trigPin, OUTPUT);
// Sets the echoPin as an Input. It is not important to define inputs, but for clarity of this we are showing it heere.

pinMode(echoPin, INPUT);
// Starts the serial communication

Serial.begin(9600); }

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 5 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance. Sound travel at 343 M/s. So distance =durationX343 Meters/second = durationX34300 cms/ 1000*1000 microseconds = duration*.0343
// Divide the total distance travelled by sound by 2 as it goes and comes back to get distance from object.
distance= duration*0.0343/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
//Code for When distance measured is below 10 cms . Light up left Led. Turn off right led.

if (distance <= 10){
//Light up left Led
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, LOW);
}
//Code for When distance measured is above 10 cms but less than 1000 cms . Light up right Led. Turn off left Led.
if (distance >= 10 && distance <=1000){
//light up right Led
digitalWrite(rightLed, HIGH);
digitalWrite(leftLed, LOW);
}
//Code for When distance measured is above 1000 cms . Light up left and right Led.
if (distance >= 1000){
//light up left and right Led
digitalWrite(rightLed, HIGH);
digitalWrite(leftLed, HIGH);
}

}

Try uploading tghis code to your arduino/Froboduino and see ig leds light up as planned. Aren't we learning something exciting. Try more patterns by blinking leds rapidly or slowly when distance changes within a range. Experiment with different ranges.

We will learn about using CDS LDR Photo sensors in lesson Part 4.

Frobo V3 programming lesson- Part 5

Hope you are enjoying this series of Froboduino/Arduino tutorials. In this Frobo V3 programming lesson Part 4, we will look at LDR CDS Photosensor closely. LDR is light dependent resistor.

LDR CDS photo sensor

CDS LDR sensors have been around for a while and are widely used by robotic and electronic hobbyists and professionals. These sensors use a special property of cadmium sulfide to change its electrical resistance when light intensity changes. We measure this changing resistance using what is known as "voltage divider circuit". We connect another fixed value resistance in series with this sensor. When connected to power (5V), with the change in light the LDR sensor resistance will change causing current ampere change in circuit. According to"Ohm's law" when resistance in circuit changes, the current flowing will change in inverse proportion.

Ohm's law states-  

V = I X R,

Where V is supply voltage, I is current in ampere and resistance in "Ohms ( A unit of resistance named after German scientist Georg Simon Ohm)"

Look at the typical voltage divider circuit.

Ldr voltage divider circuit with Arduino MCU

In the electronics world, we can't directly read current(amperes), but can read voltages. A voltage divider circuit helps in reading changing current as a changing voltage across a variable resistance when another fixed value resistance is attached in series. When light intensity changes, LDR resistance changes inversely. With this change overall resistance of circuit changes. With change in total resistance, the total current will also change as per Ohm's law as power supply voltage is constant. For two resistance in series, the current flowing through both is the same. 

Let's take an example.

V= 5V = 5000 mV (milli volt)

LDR resistance in one light condition is RL = 3000 Ohms,

Fixed Value resistance Rf = 2000 Ohms

Total resistance R1 in one light condition is = Rf+ Rl = 3000+2000 = 5000 Ohms

Current I1 = V/R = 5000/5000 = 1 mA (milli amperes),

With 1 mA current in the circuit, the voltage difference across fixed value resistance Rf is Vl = I1 X Rf = 1 X 2000 mV = 3000 mV

if light becomes brighter, LDR resistance changes from 3000 to 2000 Ohms.

Now the Current I2 = V/R2 = 5000/(2000+2000) = 1.2 mA,

With 1.2 mA current, te voltage difference across fixed value resistor Rf is V2 = I X Rf = 1.2 X 2000 = 2400 mV

We use this difference if voltage change across Rf to measure light changes and use it in our application.

Let's write our froboduino code for reading values from LDR sensor with changes in ambient light.

/* On your froboduino board, connect one of the  LDRs to LD1 +ve and -ve ports. LDR is non-directional, so you can put any leg to +ve or -ve.

LD1 port is connected on control PCB A4 pin. A4 is is an analog pin abd can read voltage as values.  Let us first define the pin for LDR.*/

const int LDR = A4;

//We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts.

void setup()

{

// Let us open the serial ports with data rate 9600 bits /second

Serial.begin(9600);

}

//This loop code runs repeatedly

void loop()

{

//Record the analog value at pin A4

int ldrValue = analogRead(A4);

// Froboduino analog reference voltage is 5V.

// Analog pins read voltage between 0 to 5V. Arduino 328P has an 10 bit analog to digotal converter (ADC) on 6 pins from A0 to A5. 

// The ADC converts voltage from 0 to 1023, i.e. 2^10-1 (Being a 10 bit ADC), so that each value corresponds to 5000mV/1024 = 4.88mV

// Let us convert ldrValue to voltage. We will use "float" variable for the decimal value.

float ldrVoltage = ldrValue*4.88 ;

// We use Characters within "Characters" in serial.print to show characters in serial monitor as "Characters"

Serial.print("LDR analog reading is ");

Serial.print(analogRead(A4));

Serial.print("LDR volatge is ");

Serial.print(ldrVoltage);

Serial.println("milli volts");

}

Copy the code above and paste it in Arduino text editor and upload to froboduino as explained earlier in programming lesson part 1.

Now, Let us try to use LDR sensor in our Frobo to do something useful. We will use two LDR sensors, mounted right in the front. How about making Frobo follow light! Can you imagine, how we can do that?

There are two LDRs hole mounts in the front part of Frobo. See picture. Connect the left LDR legs to LD1+ and LD1-  and right LDR legs to LD2+ and LD2- ports on the Froboduino board.

We will compare the light falling on each LDR and use left and right LEDs to indicate the  direction of more light. Interesting! isn't it!

Here is the Froboduino/Arduino code for making a light indicating robot.

 

const int LD1 = A4;
const int LD2 = A5;
const int leftLed = 9;
const int rightLed = 10;
// accelerator is used to control Frobo speed. Remember PWM!
const int accelerator = 5;
const int leftMotorFwd = A0;
const int leftMotorRev = A1;
const int rightMotorFwd = A2;
const int rightMotorRev = A3;;
// Setup code runs only once when program starts.

void setup()

{
//declare Leds as outputs.
pinMode(leftLed, OUTPUT);
pinMode(rightLed, OUTPUT);
pinMode(accelerator, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorRev, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorRev, OUTPUT);
//We need not define LD1 and LD2 as inputs pinMode as all pins are taken as INPUT by default
// Let us open the serial ports with data rate 9600 bits /second

Serial.begin(9600);

}

//This loop code runs repeatedly

void loop()

{

//Record the analog value at pin A4 and A5.

int ldrValue1 = analogRead(A4);
int ldrValue2 = analogRead(A5);

// Let us try to do something useful with light reading of both LDR sensors.

// LDR value will go down when it sees more light.
//Let us compare both readings of LDRs and then light up left or right Leds if left and right LDR shows
// this code will light up left Led if value of left LDR is lower.
//Frobo will turn where it sees more light,until both LDrs stsrt getting close enough values.
if (ldrValue1 < ldrValue2 + 50){
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, LOW);
digitalWrite (accelerator, HIGH);
//Frobo sees more ligh on left side, so we will turn it leftwards.
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
}
// this code will light up right Led if value of right LDR is higher.

if (ldrValue2 < ldrValue1 + 50){
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, HIGH);
//Frobo sees more ligh on right side, so we will turn it leftwards.
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, LOW);
}
// this code will light up blink left and right Led if value of of both LDR is almost equal.
else {
//Frobo sees almost equal ligh on both side, so we will it forwards.
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, LOW);
delay(300);
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, HIGH);
delay(300);
}

}

Copy and upload this code in your Froboduino and test it by flashing a torch. Is your Frobo now seeing light? We are excited to see you bringing more and more sense and action to your Frobo. It will get more and more exciting from here onwards.

Our Frobo is now doing multiple tasks all at once. It is time that we learn to make it even smarter. In next lesson, we will combine a few more features to make Frobo a great robot.

If any questions, please write in comment section below.

Frobo V3 Programming Lesson- Part 6

Welcome to part 5 of Froboduino/Arduino programming lessons. In the previous lessons, we learnt about controlling LEDs as output device, and reading values from Ultrasonic sensors and LDR light sensors. You made quite a progress. Congratulations!

In this lesson, we will learn about  IR (Infrared) proximity sensor.

What is Infrared?

Infrared is a part of light spectrum, but human eye cannot see this light. Infrared wavelength is from 800 nanometers (nm) to 1 millimeter. Our eyes can see light with wavelength between 400 nm to 700 nm. Almost all heated objects emit some amount of infrared light. Do you know about nighvision cameras. These cameras are designed to react to infrared. At night, when all lights are off, these cameras can see all objects wchich emit infrared and take photographs. Try looking for night vison photographs of humans. Human body temperature is 98.4 F and our bodies emit infrared in the range .75 to 1000 micron ( 1 micron = 10^-6 meters) wavelength.

IR proximity sensor

This sensor is pair of IR Led and IR sensitive diode. This works by emitting IR light in a direction, if there is an object nearby, the IR gets reflected and IR sensitive diode senses the reflected IR and passes the data to the micro controller( Froboduino/Arduino). Actually, IR diode when connected in reverse bias (Cathode to +5V and anode to GND through a resistor of 300 Ohm), creates small current when exposed to light. This current is then flown through a high value resistance (We are using 10000 ohm). This creates a small voltage difference across the resistance. This voltage is still too small to be directly read by Froboduino. This voltage is fed to a voltage amplifier (Op Amp) LM358. LM358 then sends a signa to micro controller, when the diode current crosses a certain value. See the picture below of IR pair. The white is IR Led and the darker is IR photo sensitive diode. Frobo V3 kit contains two such pairs.

IR photo sensitive proximity pair sensor

As you can notice, both the Led and diode have two legs each of different lengths. The longer one is Anode (normally connected to +ve voltage) and the shorter one is cathode (normally connected to -ve or ground of power supply).

Mount both IR pairs to the hole mounts in lower chassis of Frobo frame in the front. so that both face downwards. Please see picture.

Connect left IR led anode to I1+ and cathode to I1-. Connect left IR diode cathode (remember reverse bias for diodeto D1+ and anode to D1-. Similarly connect right IR to I2 and D2 ports.

Are you also thinking what we are going to do with this IR pair on Frobo? Actually, on the Frobo chassis, we have provided 4 locations for the IR pairs to mount. Two in the front on lower frame and one on middle top of top frame and one at the back of lower chassis. But we can use only two at a given time because our Froboduino op-amp LM358 can be conncted to maximum two. In this lesson, we will only talk about the front two pair usage.

Can you believe that we can use this two pairs to detect colour difference? Well, yes, but not really very correctly, but we can certainly detect light on dark or dark on light. We use the reflection properties of light and dark surface. Let us try to make a line following or rather colour contrast indicating robot with these IR pairs. Excited?? Use your imagination for other uses for this. What we are teaching here is just the beginning, we trust that you will create your own masterpiece of a robot.

So lets get going. Below is the code to train frobo to look for a black line on a white floor (surface). Draw is thick (about 15mm line with ablack marker) line on a light colour surface. The code below is meant for setting your frobo to detect black from white.

// On your froboduino board, connect one of the left IR LED to to I1 +ve and -ve ports, IR Diode to D1+ and D1- in reverse bias. Similarly connect right IR LED and diode to I2 and D2.
// Connect left motor +ve terminal to M1+ and -ve to M1- ports and similarly connect M2.
//Connect Leds to L1+ and L1-, L2+ and L2-.
// Declare constants.

const int IRD1 = A6;
const int IRD2 = A7;
const int leftLed = 9;
const int rightLed = 10;
// accelerator is used to control Frobo speed. Remember PWM!
const int accelerator = 5;
const int leftMotorFwd = A0;
const int leftMotorRev = A1;
const int rightMotorFwd = A2;
const int rightMotorRev = A3;
//Define vaiables left and right states as boolean ( 0 or 1 state) reading of IR diodes.
int leftState = 0;
int rightState =0;
//Define states for different stages of searching.
//First step is to search left for 5 seconds. So, we define if left search has been done.
int searchLeft = 0;
//After left search, we move right for the line and keep searching until line is found. so we define right search here.
int searchRight = 0;
//Here we define line found.
int lineFound = 0;
// Here we define time variable to track search time. Time variables are usually defined as unsigned long.
unsigned long searchStartTime = 0;
unsigned long searchNowTime = 0;
unsigned long searchTime = 0;
// Define Led state to light up Leds.
int ledState = 0;
//Define led time as unsigned long.
unsigned long ledStartTime = 0;
unsigned long ledNowTime = 0;
unsigned long ledTime = 0;
//We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts.

void setup()

{
// Define LEDs and Motors as outputs. Inputs are defined by default.
pinMode(leftLed, OUTPUT);
pinMode(rightLed, OUTPUT);
pinMode(accelerator, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorRev, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorRev, OUTPUT);
// Let us open the serial ports with data rate 9600 bits /second

Serial.begin(9600);

}

//This loop code runs repeatedly

void loop()

{
leftState = analogRead(IRD1);
rightState = analogRead(IRD2);
//Draw a black thick line on white surface and leave robot over the line.
//Bring frobo left side over the black line.
Serial.print("left ");
Serial.println(leftState);
Serial.print("right ");
Serial.println(rightState);
if (leftState <= 699){
digitalWrite(leftLed, HIGH);
Serial.println("Left side sees black line");
}
// Move frobo away from line and put it white surface.
if (leftState >= 700){
digitalWrite(leftLed, LOW);
Serial.println("No black line on left side");
}
//The code above doesn't work as expected.
//Turn the small blue pot on LM293 pin3 froboduino board, till frobo starts working as adjusted.
// Now, let us repeat the same thing for right side.
if (rightState <= 699){
digitalWrite(leftLed, HIGH);
Serial.println("Right side sees black line");
}
// Move frobo away from line and put it white surface.
if (rightState >= 700){
digitalWrite(leftLed, LOW);
Serial.println("No black line on right side");
}
delay(1000);
}

After you have set your IR diode to detect black line on white surface. Let us complete the code to train Frobo to follow black line.

 

// On your froboduino board, connect one of the left IR LED to to I1 +ve and -ve ports, IR Diode to D1+ and D1- in reverse bias. Similarly connect right IR LED and diode to I2 and D2.
// Connect left motor +ve terminal to M1+ and -ve to M1- ports and similarly connect M2.
//Connect Leds to L1+ and L1-, L2+ and L2-.
// Place your Frobo over a thich black line facing any direction, Frobo will find the line and follow it.
// Declare constants.

const int IRD1 = A6;
const int IRD2 = A7;
const int leftLed = 9;
const int rightLed = 10;
// accelerator is used to control Frobo speed. Remember PWM!
const int accelerator = 5;
const int leftMotorFwd = A0;
const int leftMotorRev = A1;
const int rightMotorFwd = A2;
const int rightMotorRev = A3;
const int buzzer = 1;
//Define vaiables left and right states as boolean ( 0 or 1 state) reading of IR diodes.
int leftState = 0;
int rightState =0;
//Define states for different stages of searching.
//First step is to search left for 5 seconds. So, we define if left search has been done.
int searchLeft = 0;
//After left search, we move right for the line and keep searching until line is found. so we define right search here.
int searchRight = 0;
//Here we define line found.
int lineFound = 0;
// Here we define time variable to track search time. Time variables are usually defined as unsigned long.
unsigned long searchStartTime = 0;
unsigned long searchNowTime = 0;
unsigned long searchTime = 0;
// Define Led state to light up Leds.
int ledState = 0;
//Define led time as unsigned long.
unsigned long ledStartTime = 0;
unsigned long ledNowTime = 0;
unsigned long ledTime = 0;
//We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts.

void setup()

{
// Define LEDs and Motors as outputs. Inputs are defined by default.
pinMode(leftLed, OUTPUT);
pinMode(rightLed, OUTPUT);
pinMode(accelerator, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorRev, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorRev, OUTPUT);
pinMode(buzzer, OUTPUT);

// Let us open the serial ports with data rate 9600 bits /second

Serial.begin(9600);
//steeringServo.attach(6);

//steerinfServo.write(90);
}

//This loop code runs repeatedly

void loop()

{
// We use analogRead here as Pins A6 and A7 can only perform analogRead but not digitalRead as with other pins.
leftState = analogRead(IRD1);
rightState = analogRead(IRD2);
//Serial.println(leftState);
//Serial.println(rightState);
//Draw a black thick line with dull black electricity tape on white surface and leave robot over the line.
//we have divided our code into three parts lookLeft();. LookRight();, followLine(); for easier programming.
lookLeft();
lookRight();
followLine();
}
//This code will be first to run in the loop.
//This code will make Frobo search for black line on the left for 5 seconds. You can increase this, if Frobo is placed further away from line.
// After uploading the code, place Frobo on the right side of the black line you drew. See if it tracks the black line correctly.
void lookLeft()
{
// We define searchNow as time diffrence between Time now and time of start.
searchTime = searchNowTime - searchStartTime;


//the code below lets frobo search the line by turning left or right.Both Leds blinking.
// we start with lineFound as 0, as frobo starts searching. This state decides if thos programme will run or not.
if (lineFound ==0){
// if line is still not found. The Leds will keep blinking. See void ledBlink() below for understanding.
ledBlink();

// searchLeft variable has been used so that Frobo first looks left and then right.
// At the start of search, we first search left for 5 seconds.
if (searchLeft == 0){
//if leftState or rightState analog values are less than 699 (just a number less than 700. Value above 700 means that photodiode is getting enough reflection.
if (leftState >= 700 && rightState >= 700){
searchStartTime = millis();

Serial.println("lookLeft");
Serial.println(leftState);
Serial.println(rightState);


Serial.println("Start time");
Serial.println(searchStartTime);
// Run both motors leftwards.

digitalWrite(accelerator, HIGH);
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
//Here we change the searchState to record that left search has started and we record strating time of left search.
searchLeft =1;
}
}
if (searchLeft ==1){
// Here searchNowTime will continue to run if searchLeft = 1,
searchNowTime = millis();
//Serial.println("Now time");
//Serial.println(searchNowTime);
searchTime = searchNowTime - searchStartTime;
//After 10 seconds, we move Frobo forward by running both motors.
// Serial.println(searchTime);
if (searchTime >=10000){
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
}
//If either left or right photodiode sees black line, analog value leftState or/and rightState go over 700.
if (leftState <= 700 || rightState <= 700){
//We stop the motors, as line is found.
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, LOW);
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, HIGH);
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(300);
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
lineFound =1;

//Now line is found, so we reset all time values and searchLeft to 0.
searchNowTime = 0;
searchStartTime = searchNowTime;
searchTime = 0;
searchLeft =0;
// We set the Leds on and change lineFound to 1. Now ledBlink will not run. Why? Think and answer in the comment section.

}
//if time since left search started is more than 30 seconds, we change searchLeft to 2 so leftSearch will not run. Why? Think and answer in the comment section
if (searchTime >= 30000){
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, LOW);
searchNowTime = 0;
searchStartTime = searchNowTime;
searchTime = 0;
searchLeft =2;
}
}
}
}
//This code will be second to run in the loop after leftSearch has executed.
//This code will make Frobo search for black line on the right side for 5 seconds. You can increase this, if Frobo is placed further away from line.
// After uploading the code, place Frobo on the right side of the black line you drew. See if it tracks the black line correctly.
void lookRight()
{
// The code below will work after left side search has been done and no line found.
// This will continue until line is found by frobo on right side or 5 seconds are over.
if (searchLeft == 2){
if (searchRight == 0){
if (leftState >= 700 && rightState >=700){
searchStartTime = millis();

// Turn motor rightwards by moving left motor.

digitalWrite(accelerator, HIGH);
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, LOW);
Serial.println("look right");
searchRight =1;
}
}
if (searchRight ==1){
searchNowTime = millis();
//After 10 seconds, we move Frobo forward by running both motors.
if (searchTime >=10000){
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
}
if (leftState <= 699 || rightState <= 699){
digitalWrite(leftMotorFwd, LOW);
digitalWrite(rightMotorFwd, LOW);
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, HIGH);
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(300);
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
Serial.println("Line found right");
lineFound =1;

searchNowTime = 0;
searchStartTime = searchNowTime;
searchRight =0;
searchLeft =0;

}


}
}
}
//This part of the program will run when line is found by Frobo by setting lineFound to 1 as above.

void followLine()
{
if (lineFound == 0){
if(leftState <= 699 || rightState <= 699);
lineFound = 1;
}
if (lineFound == 1){
// When left photodiode sees black line, we turn Frobo left by runnong only right motor.
if (leftState <= 699 && rightState >= 700){
digitalWrite(accelerator, HIGH);
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
Serial.println("leftState");
Serial.println(leftState);
Serial.println("RightState");
Serial.println(rightState);
}
// When right photodiode sees black line, we turn Frobo right by running only left motor.

else if (leftState >= 700 && rightState <= 699){
digitalWrite(accelerator, HIGH);
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, LOW);
Serial.println("leftState");
Serial.println(leftState);
Serial.println("RightState");
Serial.println(rightState);
}
//When both left and right photodiodes see black line, Frobo moves forward.
else if (leftState <= 699 && rightState <= 699 ){
digitalWrite(accelerator, HIGH);
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
Serial.println("leftState");
Serial.println(leftState);
Serial.println("RightState");
Serial.println(rightState);
}
else if (leftState >= 700 && rightState >= 700 ){
//When none of the photodiodes see black line, we set lineFound to 0 and lookLeft() and lookRight() programs will run to find the line.
lineFound =0;
Serial.println("Can't see line");
}
}
}
void ledBlink(){
// This code uses timer function millis() to set time intervals for leds to switch on and off after 200 milli seconds.
// Notice how we manipulate ledState by assigning it different values to decide which led will go on and off after defined intervals.
ledTime = ledNowTime - ledStartTime;
if (ledState == 0){
ledStartTime = millis();
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, LOW);
ledState =1;

}
if (ledState == 1){
ledNowTime = millis();
if(ledTime >= 200){
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, HIGH);
ledNowTime = 0;
ledStartTime = millis();
ledState =2;
}
}
if (ledState == 2){
ledNowTime = millis();
if (ledTime >=200){
digitalWrite(rightLed, LOW);
ledNowTime = 0;
ledStartTime = ledNowTime;
ledState = 0;
}
}
}

//Improve thos code for different situations. Like when Frobo is not placed over the line drawn. Think and let us know.

Copy the codes above and paste it in your arduino IDE and compile. After compiling, you are niow ready to upload the code to the Froboduino board. Let us know how was your experience so far. If any questions, please write in the comment section below.

Frobo V3 Programming Lesson- Part 7

We hope that you have understood how to read sensors and control some output devices with Froboduino/Arduino programming.

This is now time for you to jump to the next level of professional programming methods. Now, your Frobo is connected with an Obstacle sensor, IR colour contrast detection module, light sensing LDRs, Motors, Servo and Led lights. For now, this is enough to make Frobo perform a few composite tasks. Let us define the functions that we want Frobo to perform. You can come up with your own functionalities later and develop code.

We will use all three sensor inputs and all output devices attached. The next code will enable Frobo with the following capabilities.

1-      When light goes below a certain level, Frobo will stop moving and its eyes will blink with slow rate.

2-      When light is normal (normal??, we define what is normal in our code by minimum LDR value), Frobo will move inside a circle drawn with black electrical tape or marker on the floor.

3-      When Frobo senses the boundary, it moves away from the line. There are three conditions, in which Frobo wil approach the line. A)- When it approaches the line at an angle from left side. B)- When it approaches the line at an angle from right side. C)- When it approaches the line straight.

3-      If Frobo detects obstacles from 2 feet, Frobo creates a sound and turn left slowly, till it stops seeing obstacle.

4-      Frobo will blink eyes and create sounds in different patterns to various sensor inputs.

So, let's get coding. Please read comments after "//" or between  "*/   /*" to understand the part of the code.

 

#include <Servo.h>
//declare inputs and output devices.
const int trigPin = 2;
const int echoPin = 3;
const int LD1 = A4;
const int LD2 = A5;
const int IRD1 = A6;
const int IRD2 = A7;
const int leftLed = 9;
const int rightLed = 10;
const int accelerator = 5;
const int leftMotorFwd = A0;
const int leftMotorRev = A1;
const int rightMotorFwd = A2;
const int rightMotorRev = A3;
const int buzzer = 0;
Servo steeringServo;
//Declare variables we are going to monitor.
int servoPos = 90;
int ldrValue1 = 0;
int ldrValue2 = 0;
int lightMode = 1;
//Define vaiables left and right states as boolean ( 0 or 1 state) reading of IR diodes.
bool leftState = 0;
bool rightState =0;
// Declare left and right Led States for controlling Leds.
int leftLedState = 0;
int rightLedState = 0;
int ledInterval = 500;
//We declare another set of variables to decide if Frobo moved over the boundary line.
int lineDetect =0;
// This one if Frobo dtected line on left side.
int lineDetectLeft = 0;
//This one if Frobo dtected line on right side.
int lineDetectRight = 0;
//Her ewe declare detect time variables as unsigned long types.
unsigned long detectStartTime = 0;
//We will define these variables in the code.
unsigned long detectNowTime = 0;
unsigned long detectOnTime = 0;
//Declare Led start time.
unsigned long ledStartTime = 0;
unsigned long ledNowTime = 0;
unsigned long ledOnTime = 0;

//We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts.

void setup()

{
// Define buzeer, LEDs, Motors and Servo as outputs. Inputs are defined by default.
pinMode(trigPin, OUTPUT);
pinMode(leftLed, OUTPUT);
pinMode(rightLed, OUTPUT);
pinMode(accelerator, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorRev, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorRev, OUTPUT);
pinMode(buzzer, OUTPUT);
// Let us open the serial ports with data rate 9600 bits /second
steeringServo.attach(6);
Serial.begin(9600);

}

//This loop code runs repeatedly

void loop()

{
// This code runs repeatedly.
//Read both LDR values.
int ldrValue1 = analogRead(A4);
int ldrValue2 = analogRead(A5);
/* Note down LDR values in different directions when light is on and repeat the same by turning off the light.
* we will use the lowest value when light is on as a threshold value to make Frobo to sense when light is on
* and when it is off.
*/

Serial.print("Left LDR value ");
Serial.println(ldrValue1);
Serial.print("Right LDR value ");
Serial.println(ldrValue2);
//Remove "//" from next line when taking LDR values.so you can record LDR values in Serial monitor for easy reading.
//delay(1000);
/*When light is off. We will see if ldrValues are lower than the minimum we recorded.
* Frobo will move only when lightMode =1.
* We are taking 300 here. Replace it with your recorded minimum. We will change int lightMode to 0.
*/
if (ldrValue1 <= 300 && ldrValue2 <= 300){
lightMode = 0;
}
//Program will run the below module repeatedly.
// We write these programs in different modules separately for easier programming and debugging.
myBoundary();
ledPattern1();
ledPattern2();
ledPattern3();
ledPattern4();


}


// The code below is to program Frobo to remain within the boundary drawn.
void myBoundary(){
//Read IR diode state to check if Frobo sees the boundary.
leftState = digitalRead(IRD1);
rightState = digitalRead(IRD2);
// If there is enough light.
if (lightMode == 1){
// Frobo keeps moving forward. Both motors move forward.
digitalWrite(accelerator, HIGH);
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);

digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);


// If both IR diodes sees line, move Frobo backwards and then turn left (or even right as you like but change code for right turn).
if (IRD1 == 0 || IRD2 == 0){
if(lineDetect == 0){
//Take time reading when line was detected.
detectStartTime = millis();

// Turn left motor backwards.
digitalWrite(leftMotorFwd, LOW);
digitalWrite (leftMotorRev, HIGH);
// Turn right motor backwards.
digitalWrite(rightMotorFwd, LOW);
digitalWrite (rightMotorRev, HIGH);

//Line found. So, we change the lineDetect to 1.

lineDetect = 1;
// We close the statement here as lineDetect state has changed.
}
}
if (lineDetect == 1){
// Take current time since detection.
detectNowTime = millis();
// Declare detectOnTime as time difference since line was detected.
detectOnTime = detectNowTime - detectStartTime;
// Now we want to Frobo to move backwards for 5 seconds (or you can decide) and then move forward while turning left.
if (detectOnTime >= 5000){
// Turn left motor forward.
servoPos = servoPos + 15;
steeringServo.write(servoPos);
digitalWrite(leftMotorFwd, HIGH);
digitalWrite (leftMotorRev, LOW);
// Turn right motor forward.
digitalWrite(rightMotorFwd, HIGH);
digitalWrite (rightMotorRev, LOW);
}
// Turn servo back by 15 degrees after 3 seconds more.
if (detectOnTime >=8000){
servoPos = servoPos - 15;
steeringServo.write(servoPos);

detectNowTime = 0;
detectStartTime = detectNowTime;
lineDetect =0;

}
}


// If right IR diode sees line, but left doesn't. Turn Servo left by 15 degrees for 5 seconds.
if (IRD1 == 1 || IRD2 == 0){
servoPos = servoPos - 15;
steeringServo.write(servoPos);
detectStartTime = millis();
lineDetectLeft = 1;
}
if (lineDetectLeft == 1){
detectNowTime = millis();
detectOnTime = detectNowTime - detectStartTime;
if (detectOnTime >= 5000);
//Turn servo back by 15 degrees after 5 seconds.
servoPos = servoPos +15;
steeringServo.write(servoPos);
detectNowTime = 0;
detectStartTime = detectNowTime;
lineDetectLeft = 0;

}

// If left IR diode sees line, but right doesn't. Turn Servo right by 15 degrees for 5 seconds.
if (IRD1 == 0 || IRD2 == 1){
servoPos = servoPos + 15;
steeringServo.write(servoPos);
detectStartTime = millis();
lineDetectRight = 1;
}
if (lineDetectRight == 1){
detectNowTime = millis();
detectOnTime = detectNowTime - detectStartTime;
if (detectOnTime >= 5000);
//Turn servo back by 15 degrees after 5 seconds.
servoPos = servoPos -15;
steeringServo.write(servoPos);
detectNowTime = 0;
detectStartTime = detectNowTime;
lineDetectRight = 0;

}
}
}
// This code will control Leds in a way so that Leds are On for 1.5 seconds and off for 100 mili seconds. Showing fear.
void ledPattern1(){
//This code will run only when light is normal and both IR diodes step on boundary.
if (lightMode == 1 && lineDetect == 1){
//define ledOnTime.

ledOnTime = ledNowTime - ledStartTime;

if (leftLedState == 0 && rightLedState == 0){
ledStartTime = millis();
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, HIGH);
//We use tone(pin, frequency) function to sound buzzer. Our buzzer is at pin 0 and frequency we have chsen here is 440 Hz.
tone(0, 440);
leftLedState = 1;
rightLedState = 1;
}
// This code lights up Leds for 1500 mili seconds (1.5 seconds)
if (leftLedState == 1 && rightLedState ==1){
ledNowTime = millis();
if (ledOnTime >= 1500){
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, LOW);
//Here we use noTone(pin) to silence the buzzer.
noTone(0);
leftLedState = 2;
rightLedState = 2;
}
}
if (leftLedState == 2 && rightLedState == 2){
if (ledOnTime >= 1600){
ledNowTime = ledStartTime;
leftLedState = 0;
rightLedState = 0;
}
}
}
}
//This code will control Leds when light is normal and when Frobo sees line on the left.
void ledPattern2(){
if (lightMode == 1 && lineDetectLeft == 1){
//We will light up right Led in this mode and blibk left.
digitalWrite(rightLed, HIGH);
//define ledOnTime.

ledOnTime = ledNowTime - ledStartTime;

if (leftLedState == 0){
ledStartTime = millis();
digitalWrite(leftLed, HIGH);
tone(0, 330);
leftLedState = 1;
}
// This code lights up left Led for 1500 mili seconds (1.5 seconds)
if (leftLedState == 1 ){
ledNowTime = millis();
if (ledOnTime >= 1500){
digitalWrite(leftLed, LOW);
noTone(0);
leftLedState = 2;
}
}
if (leftLedState == 2 ){
if (ledOnTime >= 1600){
ledNowTime = ledStartTime;
leftLedState = 0;
}
}
}

}
// This code runs whenFrobo see line on the right IR diode.
void ledPattern3(){
if (lightMode == 1 && lineDetectRight == 1){
//We will light up left Led in this mode and blink right.
digitalWrite(leftLed, HIGH);
//define ledOnTime.

ledOnTime = ledNowTime - ledStartTime;

if (rightLedState == 0){
ledStartTime = millis();
digitalWrite(rightLed, HIGH);
tone(0, 330);
rightLedState = 1;
}
// This code lights up left Led for 1500 mili seconds (1.5 seconds)
if (leftLedState == 1 ){
ledNowTime = millis();
if (ledOnTime >= 1500){
digitalWrite(rightLed, LOW);
noTone(0);
rightLedState = 2;
}
}
if (rightLedState == 2 ){
if (ledOnTime >= 1600){
ledNowTime = ledStartTime;
rightLedState = 0;
}
}
}

}

// This code runs when light level goes below normal.
void ledPattern4(){
if (lightMode == 0){
//define ledOnTime.

ledOnTime = ledNowTime - ledStartTime;

if (leftLedState == 0&& rightLedState == 0){
ledStartTime = millis();
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, HIGH);
tone(0, 31);

leftLedState = 1;
rightLedState = 1;
}
// This code lights up both Led for 100 mili seconds.
if (leftLedState == 1 && rightLedState == 1 ){
ledNowTime = millis();
if (ledOnTime >= 100){
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, LOW);
noTone(0);
// Let us sound the buzzer using tone() function.
leftLedState = 2;
}
}
//THis code keep Leds off for 80 mili seconds.
if (leftLedState == 2 && rightLedState == 2){
if (ledOnTime >= 180){
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, HIGH);
tone(0, 31);
leftLedState = 3;
rightLedState == 3;
}
}
//Keep Leds on for 100 ms.
if (leftLedState == 3 && rightLedState == 3 ){
if (ledOnTime >= 280){
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, LOW);
noTone(0);
leftLedState = 4;
rightLedState == 4;
}
}
//Keeps Leds off for 500 ms to create a Heartbeat kind of blinking effect.
if (leftLedState == 4 && rightLedState == 4 ){
if (ledOnTime >= 780){
digitalWrite(leftLed, HIGH);
digitalWrite(rightLed, HIGH);
tone(0, 31);
leftLedState = 5;
rightLedState == 5;
}
}
if (leftLedState == 5 && rightLedState == 5 ){
if (ledOnTime >= 880){
digitalWrite(leftLed, LOW);
digitalWrite(rightLed, LOW);
noTone(0);
leftLedState = 6;
rightLedState == 6;
}
}
if (leftLedState == 6 && rightLedState == 6 ){
if (ledOnTime >= 1000){

ledNowTime = ledStartTime;
leftLedState = 0;
rightLedState == 0;
}
}
}

}

Copy and pastre this code in IDE dialog box and compile for any errors. Take note of LDR values  and add the minimum value when Light is normal you note and change it accordingly in the code. Please read the code carefully. Please ask questions in "Comments" section below.

If you have successfully completed your training so far, you are already on your way to handle more complex tasks, you can create for your Frobo.

Try writing a program on your own for the following functions.

1- Frobo moves along the boundary when it see the line.

2- Frobo avoids the obstacle on the line and then tries to find the line back and follows it.

This is your first challenge on Froboduino. Let us know your questions and progress on this challenge in "Comments" section.

Introduction to Electricity Units

Introduction to electricity units

Electricity current  is created by the constant flow of electrons in a metal when a voltage potential applied across two points in the metal. When you switch on the lights of fan in your room, the electricity starts flowing and lights glow bright. This happens because the switch connects the lights to electricity circuit and electrons start flowing in the wires and powers the light. Do you waonder how electricity can create light in the bulbs and can also rotate the fan. How does this happen? The answer is that Electricity is a form of energy and it converts one energy type to another in different ways. For example, the light is one form of energy and fan rotation is another form.

We take all everyday things without even knowing how they work, but it took engineers and scientists a lot of efforts to understand and create the everyday gadgets for comforts. Do you also want to learn how things work? Let us begin learning and share what you already know.

Let us try to define a few things here which you will notice appearing repeatedly when you read about electricity.

Voltage or Volts-

Alessandro Giuseppe Antonio Anastasio Volta (Quite a name it is, Isn't it?) was an Italian scientist. He was born is 1745 (That's more than 250 years ago). This important unit 'Voltage' or simply 'Volt' of electricity is named after his contribution to the field of electricity. Imagine the overhead water tank at your home filled with water. When you open the water faucet, the water starts flowing. Way does the water flow from the tank to your faucet? This is because, the water in tank is situated at certain height from the ground, therefore, it has some amount of potential energy. When you open the water tap, the water from higher potential energy starts flowing to lower potential energy. That's a fundamental law of nature. The energy always flows naturally from higer potential to lower potential. Similarly, electricity also flows in the same way. As you apply higher voltage potantial between two points in a metal conductor (Wire), the current starts flowing from higher to lower potential. This potatial diffrence in electricity is called "Voltage" or "Volt".