Code revision and battery life…
Here is the latest revision of the robot software. It still can use some work. The actions of this tiny robot seem quite “biological” and it is a blast to watch. Keep close to it because it tends to get itself in “trouble”, a lot. It reminds me of the actions of a fly…
(I am using a 9 Volt Rechargable battery and it seems to have a quite usable life. Seems tiny bots with tiny motors take only a tiny amount of power. At least it lasts longer than I am willing to chase it! Now if I were a lot younger…)
/* tinybot_testx
this controls a tiny bot
vision is limited to forward
so the bot gets creative…
(this robot behaves quite biologically and
watching its “self-directed” motions can be
interesting! And fun!
By Will F. 7/11
This example code is in the public domain.
*/
long randNumber; // Randomness - Biological!
const int pingPin = 7;
const int fast = 220;
const int medium = 185;
const int slow = 65;
const int maximum = 250;
const int minimum = 50;
long distance;
void setup() {
// initialize serial communication:
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(13, OUTPUT);
}
int choice()
{
if (randNumber > maximum) // maybe go right
{
goRight();
}
if (randNumber < minimum) // maybe go left
{
goLeft();
}
}
int go()
{
analogWrite(5, medium);
analogWrite(6, medium);
}
int stop()
{
analogWrite(5,0);
analogWrite(6,0);
}
int goRight()
{
analogWrite(5, medium); // Turn!
analogWrite(6, slow);
delay (320); // Give it N msec to keep turning
}
int goLeft()
{
analogWrite(5, slow); // Turn!
analogWrite(6, medium);
delay (320); // Give it N msec to keep turning
}
// nice conversion to inches
long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
int Pinger() // Very sensitive object detection!
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long inches, duration;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
return inches = microsecondsToInches(duration);
}
void loop()
{
distance = Pinger();
Serial.print(distance ); // debug
Serial.print("in, ");
//test5: // Right Motor
//analogWrite (5, medium);
//delay(100);
//goto test5;
//test6: // Left Motor
//analogWrite (6, medium);
//delay(100);
//goto test6;
randNumber = random (300); // Lovely randomness
Serial.print("random, "); // more debug
Serial.print(randNumber);
Serial.println();
if (distance < 20) // too close!
{
digitalWrite(13, HIGH); // Blink
stop(); // Stop!
delay(1000); // Give stop a second
digitalWrite(13, LOW);
choice(); // Possibly chose another direction
}
if (distance < 5) // stop collision!
die:
{
analogWrite (5, 0); // STOP!
analogWrite (6, 0);
digitalWrite(13, HIGH); // Blink
delay(1000);
digitalWrite(13, LOW);
delay(1000);
goto die;
}
go(); // Move!
choice(); // Duh! Which way do I go?
}