Friday, 17 April 2015

Robot Mania V: Code Breaker

Hi Guys…

C++ Programming language might be someone’s favorite or another one’s dreadful language. Personally, I think arduino programming language is not that hard to handle for a beginner. In this part, I would like to share about my way of programming a mobile robot.

The number of lines compiled and tested is about 300. Don’t get astounded by the number itself. In fact, it is not that long as you can imagine. 

Why? I made it with various repetitive statements and similar sub-routines. I believe there are few parameters is adjustable depends on user’s preference. For instance, intensity of light might vary in a great amount.

So, here is the lines of code:

//*********************************************************************************
//*********************************************************************************
//  COMPILED AND TESTED BY KEN OH
//  9TH FEBRUARY 2015
//  1. Bumper Sensor Robot
//  2. Bluetooth Remote Control Robot
//  3. Obstacle Avoidance Robot with Ultrasonic Sensor
//  4. Light Seeking Robot
//*********************************************************************************
//*********************************************************************************


#include <Servo.h>        
Servo myServo;

                                      //Digital Inputs and Outputs//
boolean SW  =        3;
int leftReverse =    4;
int leftForward =    5;
int rightForward =   6;
int rightReverse =   7;
boolean rightSW =    8;
boolean leftSW =     9;
int buzzer =        10;
int echoPin =       11;     
int triggerPin =    12;
boolean led =       13;               

int sensor();                           //Function to detect distance ahead//
double distance;

float rightLight = 1;                  //Analog Inputs and Outputs//
float leftLight = 0;             

int apps = 2;

void monitortest();                    //Displays every current state of all sensors and outputs via Serial Monitor//

void setup()
{
  pinMode(led,OUTPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(SW,INPUT_PULLUP);
  pinMode(leftSW,INPUT_PULLUP);
  pinMode(rightSW,INPUT_PULLUP);
  pinMode(triggerPin,OUTPUT);
  pinMode(echoPin,INPUT);
  myServo.attach(2);             
  Serial.begin(9600);
}

void loop()
{
 int sensor();
 if(check() == true)
 {
   beep();
   digitalWrite(13,HIGH);
   delay(2000);
   while(1)
   {
    duty();
   }
  }
 else
 {
   monitortest();
   sTop();
 }
}

void duty()                                //Selection of robot's duty//
{                                         
  int x = analogRead(apps);
  if ( x < 600)
  {
    return obstacleAvoidance();
  }
  else if ( x > 600 && x < 800)
  {
    return bluetooth();
  }
  else if ( x > 800 && x < 1000)
  {
    return lsr();
  }
  else if ( x > 1000)
  {
    return roaming();
  }
}

boolean check()                            //Standby Mode
{
 if (digitalRead(SW) == 1 )
 {
   delay(100);
   if (digitalRead(SW) == 1)
   {
     return 1;
   }
 }
   return 0;
}

void beep()
{
  int timer(100);
  digitalWrite(buzzer, HIGH);
  delay(timer);
  digitalWrite(buzzer, LOW);
  delay(timer);
  digitalWrite(buzzer, HIGH);
  delay(timer);
  digitalWrite(buzzer, LOW);
  delay(timer);
}

void bLink()
{
  digitalWrite(led,HIGH);
  delay(1000);
  digitalWrite(led,LOW);
  delay(1000);
}

void reverse()                                       //Motion Functions
{
  digitalWrite(leftForward,LOW);
  digitalWrite(rightForward,LOW);
  digitalWrite(leftReverse,HIGH);
  digitalWrite(rightReverse,HIGH);
}

void forward()
{
  analogWrite(leftForward,255);                      //Analog values used to calibrate the speed of each motors//
  analogWrite(rightForward,255); 
  digitalWrite(leftReverse,LOW);
  digitalWrite(rightReverse,LOW);
}

void rightturn()
{
  digitalWrite(leftForward,HIGH);
  digitalWrite(rightForward,LOW);
  digitalWrite(leftReverse,LOW);
  digitalWrite(rightReverse,HIGH);
 
}

void leftturn()
{
  digitalWrite(leftForward,LOW);
  digitalWrite(rightForward,HIGH);
  digitalWrite(leftReverse,HIGH);
  digitalWrite(rightReverse,LOW);
}

void sTop()
{
  digitalWrite(leftForward,LOW);
  digitalWrite(rightForward,LOW);
  digitalWrite(leftReverse,LOW);
  digitalWrite(rightReverse,LOW);
}

void roaming()                                      //Robot roams via bumper sensors. Time intervals depends on gear ratio of gearbox.//
{
   if (digitalRead(leftSW) == HIGH && digitalRead(rightSW) == HIGH)
 {
   forward();
 }
 else if (digitalRead(leftSW) == LOW && digitalRead(rightSW) == LOW)
 {
   sTop();
   delay(100);
   reverse();
   delay(300); 
   forward();
 }
  else if (digitalRead(leftSW) == HIGH && digitalRead(rightSW) == LOW)
 {
   reverse();
   delay(300);
   leftturn();
   delay(700);
   forward();
 }
  else if (digitalRead(leftSW) == LOW && digitalRead(rightSW) == HIGH)
 {
   reverse();
   delay(300);
   rightturn();
   delay(700);
   forward();
 }
}

void bluetooth()                                  //Bluetooth Remote Control//
{
  int val ;
  char inByte;
  if (Serial.available())
  {
    delay(100);
    while (Serial.available() > 0)
    {
      val = Serial.read();
      switch(val)
      {
        case '0' : rightturn(); break;
        case '1' : leftturn(); break;
        case '2' : reverse();  break;
        case '3' : forward(); break;
        default  : sTop();
      }
    }
  }
}

void obstacleAvoidance()                          //Robot's movement dependent on bumper sensors//
{                                                 //ultrasonic sensor                           //
  double criticalDistance = 50;                   //Critical distance depends on user's preference//
  myServo.write(90);
  while(1)
  {
    forward();
    double distance = sensor();
    roaming();
    if(distance < criticalDistance)
    {
      sTop();
      delay(100);
      reverse();
      delay(200);
     
      sTop();
      myServo.write(0);
      delay(500);
      double leftdistance = sensor();
      Serial.println(leftdistance);
      delay(500);
     
      myServo.write(180);
      delay(500);
      double rightdistance = sensor();
      Serial.println(rightdistance);
      delay(500);
     
      myServo.write(90);
     
      if (leftdistance > rightdistance)
      {
        leftturn();
        delay(700);
      }
      else if (rightdistance > leftdistance)
      {
        rightturn();
        delay(700);
      }
    }
    else forward(); 
  }
}

int sensor()
{
  double pulseTime = 0;
  digitalWrite(triggerPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin,LOW);
  pulseTime=pulseIn(echoPin,HIGH)  ;
  double distance = pulseTime/58;
  delay(100);
  return distance;
}

void lsr()                                        //Robot moves toward light source           //
{                                                 //Adjustment made by varying threshold value//
  int leftThreshold = 200;
  int rightThreshold = 600;
  int leftIntensity = analogRead(A0);
  int rightIntensity = analogRead(A1);
 
  if(leftIntensity > leftThreshold && rightIntensity > rightThreshold)
  {
    forward();
  }
  if(leftIntensity > leftThreshold && rightIntensity < rightThreshold)
  {
    leftturn();
  }
  if(leftIntensity < leftThreshold && rightIntensity > rightThreshold)
  {
    rightturn();
  }
  else if(leftIntensity < leftThreshold && rightIntensity < rightThreshold)
  {
    sTop();
  }
}

void monitortest()
{
  float distance = sensor();
  Serial.println("\nFuncs \tSW \tRSW \tLSW \tDistance(cm) \tLeftSensor \tRightSensor");
  Serial.println("----------------------------------------------------------------------------");
  for( int i = 0; i <10 ; i++)
  {
  Serial.print(analogRead(A2));
  Serial.print("\t");
  Serial.print(digitalRead(SW));
  Serial.print("\t");
  Serial.print(digitalRead(leftSW));
  Serial.print("\t");
  Serial.print(digitalRead(rightSW));
  Serial.print("\t");
  Serial.print(distance);
  Serial.print("\t\t");
  Serial.print(analogRead(A0));
  Serial.print("\t\t");
  Serial.print(analogRead(A1));
  Serial.print("\t");
  Serial.println();
  delay(10);
  }
}
//*********************************************************************************

//*********************************************************************************
 


The next one would be Android Apps developed by using MIT App Inventor. It will be used for the Bluetooth controlled mobile robot. I made the apk file with a little effort. This open source software development platform is quite user-friendly. I used few components to build up an ordinary graphical user interface(GUI). It only require user to connect his mobile phone to the robot. Instead of using button on screen, I’m using an accelerometer sensor to control its motion. Tilting in x-axis will steer its direction whether to the left or to the right. Meanwhile, tilting in y-axis will control its forward and backward motions.
The design blocks are shown in the following screenshots.          




                                                             
It’s going to complete the whole project soon after the code is done. I made some changes on the physical structure of the robot to improve its movement. In the finalized model, I will post some video over my little creation.


Thank you.

No comments:

Post a Comment