Software > Software

Walbot Source code


So if anyone is interested here is the source code I use for Walbot:
edit: The reason why Walbot works so well is that she has Chuck Norris on her side, nothing gets in her way.

--- Code: ---/*This is a program by Brandon Sweeney to control the autonomous robot Walbot, it implements the
LV MAX EZ1 sonar rangefinder and outputs the voltage signal sent from the rangefinder and its
conversion in inches. This data is then used to control the actions of Walbot.
This material is free, just give me credit for using it*/

int Motor1A = 10;  // H-Bridge input 1             
int Motor1B = 11;  // H-Bridge input 2               
int enable = 3;     // H-Bridge motor 1 enable
int Motor2A = 9;  // H-Bridge input 3             
int Motor2B = 8;  // H-Bridge input 4               
int enable2 = 5;     // H-Bridge motor 2 enable
int ultraAn = 0;    // select the input pin for the ultrasonic sensor
int UltraEnable = 12;    // sets enable to pin 12
int ledPin = 13;    // sets the indicator led to pin 13
float val[10];       // array to store first 10 values coming from the sensor
float valSum=0;      // sum of first 10 readings
float valAvg=0;      // average of the first 10 readings
float dist = 0;      // variable to store the converted distance in inches

// ********************Guns don't kill people. Chuck Norris kills People. **************************//

void straight() // go straight function
    digitalWrite(enable, HIGH);
    digitalWrite(Motor1A, HIGH);
    digitalWrite(enable2, HIGH);
    digitalWrite(Motor2A, HIGH);

//************************ Chuck Norris does not sleep. He waits.********************************//

void turnRight() // turn right function         
    digitalWrite(enable, HIGH);
    digitalWrite(Motor1A, LOW);
    digitalWrite(enable2, HIGH);
    digitalWrite(Motor2A, HIGH);

//************When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris.****************//

float distCalc() // distance calculating function converts analog input to inches
 valSum = 0;                           // resets valSum to zero after every loop
  for (int i=0; i<=9; i++)              // for loop to gather first 10 int's
    val[i] = analogRead(ultraAn);       // read the value from the sensor
    valSum = (val[i]+valSum);           // adds the first 10 values for averaging
  digitalWrite(UltraEnable, LOW);            // disables sonar
  valAvg = (valSum/10);                 // calculates the average
  dist = ((valAvg/204.8)/.009765625);   // converts the analog value to distance in inches
  return dist;                         // return the distance values to the rest of the program

//********Chuck Norris is the reason why Waldo is hiding.********Chuck Norris counted to infinity - twice.********//

void avoideWalls()
    if(dist < 16)                            // if the distance is less than 20 inches
    digitalWrite(ledPin, HIGH);             // turn on the LED
    turnRight();                            // turns Walbot right using turnRight function
  else                                      // otherwise
    digitalWrite(ledPin, LOW);              // turn off the LED
    straight();                            // go straight using the straight function

//*****Chuck Norris can slam a revolving door.******Chuck Norris does not get frostbite. Chuck Norris bites frost*****//

void setup()
  pinMode(Motor1A, OUTPUT);      // sets motor pin 1A as output 
  pinMode(Motor1B, OUTPUT);      // sets motor pin 1B as output
  pinMode(Motor2A, OUTPUT);      // sets motor pin 2A as output
  pinMode(Motor2B, OUTPUT);      // sets motor pin 2B as output
  pinMode(enable, OUTPUT);       // sets enable2 as output
  pinMode(enable2, OUTPUT);      // sets enable as output
  pinMode(UltraEnable, OUTPUT);  // sets enable as output
  pinMode(ledPin, OUTPUT);       // sets ledPin as output
  straight(); // initializes Walbot to go straight

//*****Chuck Norris can win a game of Connect Four in only three moves.******Chuck Norris can divide by zero.*****//

void loop() {
  digitalWrite(UltraEnable, HIGH);           // turns on sonar
  distCalc();                                // gets the distance value from distCalc function
  avoideWalls();                             // tells Walbot not to run into inanimate objects by calling avoideWalls function
  delay(20);                                 // delay for 20 milliseconds
--- End code ---


[0] Message Index

Go to full version