420498_412051402145496_642280519_n.jpg (165663Bytes)
I have a new video and pics that I will post soon of Amsterbot's new/temp body.
My questions on the code are simple I guess. I have gotten the HC-sr04 to work with example code and I can make use the servo code with ease, even playing around with it in lots of fun ways. My brick wall for now is how to put that together? I would like to start with simple code that triggers the head servo to rotate when something is 6 cm away? or something like this. After that I plan to build on this and in the end have a complete amsterbot. Many thanks in advance :D
I'm also using Duemilanove w/atmega 328p just in case.
If I knew where to start I would not be asking. I have been trying to figure this out myself by looking at example code and such. I do not understand how to tie together the two components through code. I am not asking someone to write the code for my whole robot I am just looking for help to break through this understanding issue. I have put off asking for help with this for months, I also run a business and have a life outside of robots so it's not something I get to work on everyday. The bad part about that is I end up with some other issue like a bad cable that takes me weeks to figure out. It was new lol.
Here is the code I am working with/on.. not much to look at.
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
*/
int echoPin = 13;
int trigPin = 12;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = duration / 29 / 2;
/* Sound velocity=29cm/microsec,
then divide by 2 because of the return time */
Serial.print(cm);
Serial.println(" cm");
delay(500);
}
Next bit of code is the servo code I am playing with for amsterbot. Hey he moves and does what I want so don't be to hard on me here. I also added some led blink action.
#include <Servo.h>
// amsterbot 2.0 3/1/12 new 4/18/12 joe chiefdadddy nyc
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
Servo servohead; // head
void setup()
{
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
servohead.attach(6); // head servo
servoxtra.attach(11); // xtra led servo
pinMode(13, OUTPUT); // leds
}
void loop() // Loop through motion tests
{
stopRobot();
robotlookforward();
delay(500);
robotlookleft();
delay(500);
robotlookforward();
delay(500);
robotlookright();
delay(500);
robotlookforward();
delay(500);
startRobot();
turnRight();
delay(600);
forward();
delay(2000);
}
// Motion routines for forward, reverse, turns, and stop head servo and xtraservo
void forward()
{
servoLeft.write(0);
servoRight.write(180);
servohead.write(90);
servoxtra.write(180);
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(250);
digitalWrite(13, HIGH);
}
void reverse()
{
servoLeft.write(180);
servoRight.write(0);
servohead.write(0);
servoxtra.write(90);
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(250);
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(250);
}
void turnRight()
{
servoLeft.write(180);
servoRight.write(180);
servoxtra.write(0);
digitalWrite(13, LOW);
}
void turnLeft()
{
servoLeft.write(0);
servoRight.write(0);
servoxtra.write(180);
digitalWrite(13, LOW);
}
void stopRobot()
{
servoLeft.detach();
servoRight.detach();
servohead.write(90);
servoxtra.write(90);
digitalWrite(13, LOW);
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
}
void startRobot()
{
servoLeft.attach(10);
servoRight.attach(9);
servohead.write(90);
digitalWrite(13, HIGH); //set led on
}
void robotlookforward()
{
servohead.write(90);
digitalWrite(13, HIGH); // set the LED on
}
void robotlookleft()
{
servohead.write(180);
digitalWrite(13, LOW); // set LED off
}
void robotlookright()
{
servohead.write(0);
digitalWrite(13, HIGH); // set the LED on
}
All I am looking for here is a simple way to make the head servo move when the HC-sr04 is triggered at a certain distance. I can continue to build my code skills from there. Like I said I would like to keep this simple for now.
and for your viewing pleasure Chiefdaddy's Beard and Chief the wonder dog! :D