ive been trying months cannot figure simple thing out. beginner doing 7 year old , hoping stumped.
i followed instructable. motors work when test them battery
here code using
//since using servos , ultrasonic sensors in robot include libraries written make use easier
#include <servo.h>
#include <newping.h>
//below symbolic constants. instead of having type in non-sensical pin number each time want can write easy understand name represents pin, compiler replace names numbers
#define leftmotorforward 3
#define leftmotorbackward 6
#define rightmotorforward 11
#define rightmotorbackward 14
#define ustrigger 3
#define usecho 2
#define maxdistance 100
#define led 13
//here have created 2 'objects', 1 servo , 1 ultrasonic sensor
servo servo;
newping sonar(ustrigger, usecho, maxdistance);
//below creating unsigned integer variables use later on in code. unsigned have postive values
unsigned int duration;
unsigned int distance;
unsigned int frontdistance;
unsigned int leftdistance;
unsigned int rightdistance;
unsigned int time;
void setup() //this block happens once on startup
{
//serial.begin(115200); //i have included serial initialize commented out, if want debug , print information serial monitor uncomment
//here setting pin modes. sending out signals pins set them outputs
pinmode(leftmotorforward, output);
pinmode(leftmotorbackward, output);
pinmode(rightmotorforward, output);
pinmode(rightmotorbackward, output);
pinmode(led, output);
servo.attach(4); //the servo attached pin 4
}
void loop() //this block repeats while arduino turned on
{
delay(500);
servo.write(90); //rotate servo face front
scan(); //go scan function
frontdistance = distance; //set variable frontdistance value of distance returned scan function
if(frontdistance > 40 || frontdistance == 0) //if there nothing infront of robot within 40cm or distance value 0 (which newping libary means no ping returned) then...
{
moveforward(); //go moveforward function
}
else //else (if there infront of robot within 40cm) then...
{
movestop(); //go movestop function
servo.write(167); //move servo left (my little servos didn't going 180 played around value until worked nicely)
delay(500); //wait half second servo there
scan(); //go scan function
leftdistance = distance; //set variable leftdistance distance on left
servo.write(0); //move servo right
delay(500); //wait half second servo there
scan(); //go scan function
rightdistance = distance; //set variable rightdistance distance on right
if(rightdistance < leftdistance) //if distance on right less on left then...
{
moveleft(); //go moveleft function
delay(500); //pause program half second let robot turn
}
else if(leftdistance < rightdistance) //else if distance on left less on right then...
{
moveright(); //go moveright function
delay(500); //pause program half second let robot turn
}
else //if distance on left , right equal (highly unlikely put in none less) then...
{
movebackward(); //go movebackward function
delay(200); //pause program 200 milliseconds let robot reverse
moveright(); //go moveright function
delay(200); //pause program 200 milliseconds let robot turn right
}
}
}
void moveforward() //this function tells robot go forward
{
digitalwrite(leftmotorbackward, low);
digitalwrite(leftmotorforward, high);
digitalwrite(rightmotorbackward, low);
digitalwrite(rightmotorforward, high);
}
void movebackward() //this function tells robot move backward
{
digitalwrite(leftmotorforward, low);
digitalwrite(leftmotorbackward, high);
digitalwrite(rightmotorforward, low);
digitalwrite(rightmotorbackward, high);
}
void moveleft() //this function tells robot turn left
{
digitalwrite(leftmotorforward, low);
digitalwrite(leftmotorbackward, high);
digitalwrite(rightmotorbackward, low);
digitalwrite(rightmotorforward, high);
}
void moveright() //this function tells robot turn right
{
digitalwrite(leftmotorbackward, low);
digitalwrite(leftmotorforward, high);
digitalwrite(rightmotorforward, low);
digitalwrite(rightmotorbackward, high);
}
void movestop() //this function tells robot stop moving
{
digitalwrite(leftmotorbackward, low);
digitalwrite(leftmotorforward, low);
digitalwrite(rightmotorforward, low);
digitalwrite(rightmotorbackward, low);
}
void scan() //this function determines distance things away ultrasonic sensor
{
delay(50);
time = sonar.ping();
distance = time / us_roundtrip_cm;
}
the motors connected pin 3,6,11,14
i using 4 aa , 1 9v plugged arduino
my servo , ping sensor seem work. move , forth. not action outta motors
i hook 4aa ipositive nto pin 8 , ground arduino
please me son lol
thanks
if need more infor ask. grateful insight
i followed instructable. motors work when test them battery
here code using
//since using servos , ultrasonic sensors in robot include libraries written make use easier
#include <servo.h>
#include <newping.h>
//below symbolic constants. instead of having type in non-sensical pin number each time want can write easy understand name represents pin, compiler replace names numbers
#define leftmotorforward 3
#define leftmotorbackward 6
#define rightmotorforward 11
#define rightmotorbackward 14
#define ustrigger 3
#define usecho 2
#define maxdistance 100
#define led 13
//here have created 2 'objects', 1 servo , 1 ultrasonic sensor
servo servo;
newping sonar(ustrigger, usecho, maxdistance);
//below creating unsigned integer variables use later on in code. unsigned have postive values
unsigned int duration;
unsigned int distance;
unsigned int frontdistance;
unsigned int leftdistance;
unsigned int rightdistance;
unsigned int time;
void setup() //this block happens once on startup
{
//serial.begin(115200); //i have included serial initialize commented out, if want debug , print information serial monitor uncomment
//here setting pin modes. sending out signals pins set them outputs
pinmode(leftmotorforward, output);
pinmode(leftmotorbackward, output);
pinmode(rightmotorforward, output);
pinmode(rightmotorbackward, output);
pinmode(led, output);
servo.attach(4); //the servo attached pin 4
}
void loop() //this block repeats while arduino turned on
{
delay(500);
servo.write(90); //rotate servo face front
scan(); //go scan function
frontdistance = distance; //set variable frontdistance value of distance returned scan function
if(frontdistance > 40 || frontdistance == 0) //if there nothing infront of robot within 40cm or distance value 0 (which newping libary means no ping returned) then...
{
moveforward(); //go moveforward function
}
else //else (if there infront of robot within 40cm) then...
{
movestop(); //go movestop function
servo.write(167); //move servo left (my little servos didn't going 180 played around value until worked nicely)
delay(500); //wait half second servo there
scan(); //go scan function
leftdistance = distance; //set variable leftdistance distance on left
servo.write(0); //move servo right
delay(500); //wait half second servo there
scan(); //go scan function
rightdistance = distance; //set variable rightdistance distance on right
if(rightdistance < leftdistance) //if distance on right less on left then...
{
moveleft(); //go moveleft function
delay(500); //pause program half second let robot turn
}
else if(leftdistance < rightdistance) //else if distance on left less on right then...
{
moveright(); //go moveright function
delay(500); //pause program half second let robot turn
}
else //if distance on left , right equal (highly unlikely put in none less) then...
{
movebackward(); //go movebackward function
delay(200); //pause program 200 milliseconds let robot reverse
moveright(); //go moveright function
delay(200); //pause program 200 milliseconds let robot turn right
}
}
}
void moveforward() //this function tells robot go forward
{
digitalwrite(leftmotorbackward, low);
digitalwrite(leftmotorforward, high);
digitalwrite(rightmotorbackward, low);
digitalwrite(rightmotorforward, high);
}
void movebackward() //this function tells robot move backward
{
digitalwrite(leftmotorforward, low);
digitalwrite(leftmotorbackward, high);
digitalwrite(rightmotorforward, low);
digitalwrite(rightmotorbackward, high);
}
void moveleft() //this function tells robot turn left
{
digitalwrite(leftmotorforward, low);
digitalwrite(leftmotorbackward, high);
digitalwrite(rightmotorbackward, low);
digitalwrite(rightmotorforward, high);
}
void moveright() //this function tells robot turn right
{
digitalwrite(leftmotorbackward, low);
digitalwrite(leftmotorforward, high);
digitalwrite(rightmotorforward, low);
digitalwrite(rightmotorbackward, high);
}
void movestop() //this function tells robot stop moving
{
digitalwrite(leftmotorbackward, low);
digitalwrite(leftmotorforward, low);
digitalwrite(rightmotorforward, low);
digitalwrite(rightmotorbackward, low);
}
void scan() //this function determines distance things away ultrasonic sensor
{
delay(50);
time = sonar.ping();
distance = time / us_roundtrip_cm;
}
the motors connected pin 3,6,11,14
i using 4 aa , 1 9v plugged arduino
my servo , ping sensor seem work. move , forth. not action outta motors
i hook 4aa ipositive nto pin 8 , ground arduino
please me son lol
thanks
if need more infor ask. grateful insight
i hook 4aa ipositive nto pin 8 , ground arduinowhat mean?
Arduino Forum > Using Arduino > Project Guidance > James Robot dc motors wont work
arduino
Comments
Post a Comment