My SERB code

My attempt at writing some code for my SERB. The SERB test program from the Instructable article makes up 90% of my code. I used the Arduino playground website and the Freeduino website for information on how to interface my Sharp IR sensors. I also added visual feedback using an led for each sensor when it detects an object. Please do not use my code in anything that real programmers will ever see…they will laugh hysterically! ūüôā

My SERB code: (cut and past into your Arduino environment)

/*
* Modified by WinfieldMike on December 18th, 2008
* Arduino Controlled Servo Robot (SERB) – Test Program
* For more details visit: http://www.oomlout.com/serb
*
* Behaviour: A test program that sends the robot forward until one of the sensors sees an object. I used Sharp
*¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† 2Y0A21 sensors. My sensors give a value of 300 on the analog pin when an object is about 6″ away.
*            You might need to adjust the valrange and valrange2 settings to get the distance you want.
*            My loop does not use all of the SERB functions, just goStop, goForward, goBackward, and turnRandom.
*
* Wiring: Right Servo Signal – pin 9
*         Left Servo Signal Рpin 10
*
* License: This work is licensed under the Creative Commons
*          Attribution-Share Alike 3.0 Unported License. To
*          view a copy of this license, visit
*          http://creativecommons.org/licenses/by-sa/3.0/
*          or send a letter to Creative Commons, 171 Second
*          Street, Suite 300, San Francisco, California 94105,
*          USA.
*
*/

//———————————————————————————
// START OF ARDUINO CONTROLLED SERVO ROBOT (SERB) PREAMBLE
#include <Servo.h>

#define LEFTSERVOPIN  10
#define RIGHTSERVOPIN  9

Servo leftServo;
Servo rightServo;

int speed = 100; //sets the speed of the robot (both servos) a percentage between 0 and 100
int valrange=0;  //variable for left sensor
int valrange2=0; //variable for right sensor
int potPin = 0;    // left sensor input
int potPin2=1; //right sensor input
int leftledPin=6; //left led lights when object detected
int rightledPin=7;  //right led lights when object detected

// END OF ARDUINO CONTROLLED SERVO ROBOT (SERB) PREAMBLE
//———————————————————————————

/*
* sets pins to appropriate states and attaches servos. Then pauses
* for 1 second before the program starts
*/
void setup()
{
serbSetup();                       //sets the state of all neccesary pins and adds servos to your sketch
randomSeed(analogRead(5));         //sets the random number seed with something mildly random
delay(1000);
pinMode (leftledPin, OUTPUT); //sets led pin to output mode
pinMode (rightledPin, OUTPUT); //sets led pin to output mode
}

/*
* turns the robot either left or right (randomly) for a period between
* 0.1 and 1 second, and then continues on forward if the sensors don’t detect anything
*/
void loop()
{

goForward();
valrange = analogRead(potPin);    // read the value from the right sensor
valrange2= analogRead(potPin2); //read the value from the left sensor

if (valrange >= 300) //waits for the sensor value to be more than 300
{
digitalWrite (leftledPin, HIGH); //turns the left led on
goStop();
delay(250);
goBackward();
delay(1000);
turnRandom(100,1000);
digitalWrite (leftledPin, LOW); //turns the left led off
}

if (valrange2 >= 300) //waits for the sensor value to be more than 300
{
digitalWrite (rightledPin, HIGH); //turns the right led on
goStop();
delay(250);
goBackward();
delay(1000);
turnRandom(100,1000);
digitalWrite (rightledPin, LOW); //turns the right led off
}
}
/*
* turns the robot randomly left or right for a random time period between
* minTime (milliseconds) and maxTime (milliseconds)
*/
void turnRandom(int minTime, int maxTime)
{
int choice = random(2);                     //Random number to decide between left (1) and right (0)
int turnTime = random(minTime,maxTime);     //Random number for the pause time
if(choice == 1){ goLeft();}                 //If random number = 1 then turn left
else {goRight();}                           //If random number = 0 then turn right
delay(turnTime);                            //delay for random time
}

//START OF ARDUINO CONTROLLED SERVO ROBOT (SERB) ROUTINES

/*
* sets up your arduino to address your SERB using the included routines
*/
void serbSetup(){

setSpeed(speed);
pinMode(LEFTSERVOPIN, OUTPUT);     //sets the left servo signal pin to output
pinMode(RIGHTSERVOPIN, OUTPUT);    //sets the right servo signal pin to output
leftServo.attach(LEFTSERVOPIN);    //attaches left servo
rightServo.attach(RIGHTSERVOPIN);  //attaches right servo
goStop();
}

/*
* sets the speed of the robot between 0-(stopped) and 100-(full speed)
* NOTE: speed will not change the current speed you must change speed
* then call one of the go methods before changes occur.
*/
void setSpeed(int newSpeed){
if(newSpeed >= 100) {newSpeed = 100;}     //if speed is greater than 100 make it 100
if(newSpeed <= 0) {newSpeed = 0;}         //if speed is less than 0 make it 0
speed = newSpeed * 0.9;                   //scales the speed to be between 0 and 90
}

/*
* sends the robot forwards
*/
void goForward(){
leftServo.write(90 + speed);
rightServo.write(90 – speed);
}

/*
* sends the robot backwards
*/
void goBackward(){
leftServo.write(90 – speed);
rightServo.write(90 + speed);
}

/*
* sends the robot right
*/
void goRight(){
leftServo.write(90 + speed);
rightServo.write(90 + speed);
}

/*
* sends the robot left
*/
void goLeft(){
leftServo.write(90 – speed);
rightServo.write(90 – speed);
}

/*
* stops the robot
*/
void goStop(){
leftServo.write(90);
rightServo.write(90);
}
//END OF ARDUINO CONTROLLED SERVO ROBOT (SERB) ROUTINES
//————————————————————————————————————

Advertisements
%d bloggers like this: