Journal for the creation of the Prodigialis Robot – Day 3

The tweaking of the code and design begins

I’m finally caught up, and can write this as I go, providing updates as they happen. I tried tweaking the code a little to make the turns a little sharper, but ended up just going back to the default code. I originally had the rear of the robot resting on a 1×1 piece of carboard, but this was causing much too much drag and was seriously throwing the robot off. So I switched to using the Ball Caster that came with the kit (mentioned this on Day 1’s post) and it much improved the robot’s performance.

I’m currently working on setting up a kill button, so I can push a button to stop the robot without having to pull the plug. Here’s the breadboarding so far:

AWARE-killButton_bb

I have a Sketch, what the Arduino code projects are called, set aside with what works for sure. I named that AWARE_Stable. Now, the whole point of having a stable sketch is so that I can always revert to the stable sketch in case a new experiment fails. Thus, I created AWARE_Dev, the Development sketch.

So, now that I had breadboarded the button to the Arduino, I had to add the code.

I took the AWARE_Dev sketch and began to mess around with it, following this official Arduino tutorial: http://arduino.cc/en/tutorial/button. I know I was using Foxytronics, but this was official and came up first on the google search and Foxytronics has yet to add a button tutorial.

I created a constant called buttonPin to store the pin the button was connected to as well as an integer for the button state called buttonState.

Here’s the code (that’s failing):

// Version 2.1.1
// Major:        2 - My first robot code, copied mostly from Foxytronics, but with tweaks of my own, like the kill button
// Minor:        1 - Minor change is the adding of the custom functions for adding of kill button
// Micro:        1 - Micro change is the adding of the custom functions for adding of kill button

#include 

// create servo objects
Servo leftMotor;
Servo rightMotor;

// define the states
#define DRIVE_FORWARD            0
#define TURN_LEFT                1

int state = DRIVE_FORWARD;        // 0 = drive forward (DEFAULT), 1 = turn left

const int serialPeriod = 250;    // only print to the serial console every 1/4 second
unsigned long timeSerialDelay  = 0;

const int loopPeriod = 20;      // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay    = 0;

// specify the trig and echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 9;
const int ultrasonic2EchoPin = 8;

// set the pin numbers for the button
const int buttonPin = 2;

// state recorder for the button
int buttonState = 0;

int ultrasonic2Distance;
int ultrasonic2Duration;

void setup()
{
  Serial.begin(9600);
  
  // ultrasonic sensor pin configurations
  pinMode(ultrasonic2TrigPin, OUTPUT);
  pinMode(ultrasonic2EchoPin, INPUT);
  
  // attach the servos
  leftMotor.attach(13);
  rightMotor.attach(12);
  
  // Set up the button pin
  pinMode(buttonPin, INPUT);

}

void loop()
{
  debugOutput();     // prints debugging messages to the serial console
  
  killButton();      // checks for kill button push
  
  stateMachine();
  
  if(millis() - timeLoopDelay >= loopPeriod)
  {
    readUltrasonicSensors();  // read and store the measured distances
    
    timeLoopDelay = millis();
  }

}

void killButton()
{
  // read the state of the button
  buttonState = digitalRead(buttonPin);
  
  Serial.println(buttonState)
  
  // if the state of the button is HIGH (pressed)
  if(buttonState == HIGH)
  {
    rightMotor.write(90);
    leftMotor.write(90);
    endProgram();
  }
}

void endProgram()
{
  Serial.println("Program Killed by Physical switch");
  while(1) { }
}

void stateMachine()
{
  if(state == DRIVE_FORWARD) // no obstacles detected
  {
    if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // if there's nothing in front of us
    {
      // drive forward
      rightMotor.write(0);
      leftMotor.write(180);
    }
    else                                                  // there's an object in front of us
    {
      state = TURN_LEFT;
    }
  }
  else if(state == TURN_LEFT) // obstacle detected -- turning left
  {
    unsigned long timeToTurnLeft = 1100;                // it takes about 1.1 seconds to turn 90 degrees
    
    unsigned long turnStartTime = millis();             // save the time that we started turning
    
    while((millis()-turnStartTime)  serialPeriod)
  {
    Serial.print("ultrasonic2Distance: ");
    Serial.print(ultrasonic2Distance);
    Serial.print("cm: ");
    Serial.println();
    
    timeSerialDelay = millis();
  } 
}

I tried running this, but I didn’t quite work like it was supposed to. One motor shut down, but the other kept going, while the program infinitely looped around, constantly saying that the button had been pushed, when I had barely looked at it! So, I added an infinite loop

while(1) {}

and tried again. This time the program didn’t loop infinitely, but the motor kill still failed, so I had to try to calibrate the servos. I wrote some code to do that, but sadly, I had to go to bed. Bothersome bedtimes, always getting in the way of project completion.

Advertisements
Tagged , ,

Tell us what you thought!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: