Collision Avoidance Robot making beepy noises?
hello all,
so here have pretty basic collision avoidance robot:
works alright....the problem is, moves fast...
so when try , set set using " analogwrite(3, 123); in place of digitalwrite(3,high); example make beeping noises not sound healthy .
i guess i'm not using pwm correctly. can see error?
please poor robot become best can be
so here have pretty basic collision avoidance robot:
code: [select]
#include <servo.h>
#include<afmotor.h>
//declare servos
servo scanservo; //ping sensor servo
const int motor1 = 12;
const int motor2 = 9;
const int steer1 = 13;
const int steer2 = 8;
const int = 800;
const int turntime = 500; //number of milliseconds turn when turning
const int pingpin = 4; //pin ping sensor attached to.
const int echopin = 5; //pin ping sensor attached to.
const int scanservopin = 7; // pin number scan servo
const int distancelimit = 10; //if gets many inched from
// robot stops , looks go.
//setup function. runs once when arduino turned on or restarted
void setup()
{
serial.begin (9600);
scanservo.attach(scanservopin); // attach scan servo
pinmode(motor1, output);
pinmode(motor2, output);
pinmode(steer1, output);
pinmode(steer2, output);
//variable inicialization
digitalwrite(motor1,low);
digitalwrite(motor2,low);
digitalwrite(steer1,low);
digitalwrite(steer2,low);
delay(2000); // wait 2 seconds
}
void loop(){
go(); // if nothing wrong go forward using go() function below.
int distance = ping(); // ping() function see if ahead.
if (distance < distancelimit){
stopmotors(); // if ahead, stop motors.
char turndirection = scan(); //decide direction turn.
switch (turndirection){
case 'l':
turnleft(turntime);
break;
case 'r':
turnright(turntime);
break;
case 'b':
backward(1500);
turnright(1500);
stopmotors();
break;
case 's':
forward(turntime);
break;
}
}
}
int ping(){
long distance, duration;
//send pulse
pinmode(pingpin, output);
digitalwrite(pingpin, low);
delaymicroseconds(2);
digitalwrite(pingpin, high);
delaymicroseconds(5);
digitalwrite(pingpin, low);
//read echo
pinmode(echopin, input);
duration = pulsein(echopin, high);
distance=(duration/2)*0.0341;
serial.print("ping: ");
serial.println(distance);
}
void go(){
digitalwrite(12, high);
digitalwrite(9, low);
digitalwrite(3, high);
delay(600);
}
void turnleft(int t){
digitalwrite (13, high);
digitalwrite (8, low);
digitalwrite (11, high);
delay(t);
}
void turnright(int t){
digitalwrite (13, low);
digitalwrite (8, low);
digitalwrite (11, high);
delay(t);
}
void forward(int t){
digitalwrite(12, high);
digitalwrite(9, low);
digitalwrite(3, high);
delay(600);
}
void backward(int t){
digitalwrite (9, low);
digitalwrite (12, low);
digitalwrite(3, high);
delay(t);
}
void stopmotors(){
digitalwrite (9, high);
digitalwrite (8, high);
digitalwrite (3, high);
digitalwrite (11, high);
delay(600);
}
char scan(){
int leftscanval, centerscanval, rightscanval;
char choice;
//look left
scanservo.write(30);
delay(300);
leftscanval = ping();
//look right
scanservo.write(150);
delay(1000);
rightscanval = ping();
//center scan servo
scanservo.write(88);
if (leftscanval>rightscanval && leftscanval>centerscanval){
choice = 'l';
}
else if (rightscanval>leftscanval && rightscanval>centerscanval){
choice = 'r';
}
else if (rightscanval<30 && leftscanval<30 && centerscanval<30){
choice = 'b';
}
else{
choice = 's';
}
serial.print("choice: ");
serial.println(choice);
return choice;
}
works alright....the problem is, moves fast...
so when try , set set using " analogwrite(3, 123); in place of digitalwrite(3,high); example make beeping noises not sound healthy .
i guess i'm not using pwm correctly. can see error?
please poor robot become best can be
do think possible answer question without knowing hardware you're using , how connected?
Arduino Forum > Using Arduino > Project Guidance > Collision Avoidance Robot making beepy noises?
arduino
Comments
Post a Comment