hi im wondering how add servo sketch
thanks
#include <newping.h>
#define trigger_pin 3
#define echo_pin 2
#define max_distance 100
const int motor1pin1 = 8;
const int motor1pin2 = 9;
const int motor2pin2 =10;
const int motor2pin1 = 11;
newping sonar(trigger_pin, echo_pin, max_distance);
unsigned int time;
int distance;
int triggerdistance = 30;
int fdistance;
int ldistance;
int rdistance;
void setup()
{
pinmode(motor1pin1, output);
pinmode(motor1pin2, output);
pinmode(motor2pin1, output);
pinmode(motor2pin2, output);
}
void loop()
{
scan();
fdistance = distance;
if(fdistance < triggerdistance){
movebackward();
delay(1000);
moveright();
delay(500);
movestop();
scan();
rdistance = distance;
moveleft();
delay(1000);
movestop();
scan();
ldistance = distance;
if(ldistance < rdistance){
moveright();
delay(200);
moveforward();
}
else{
moveforward();
}
}
else{
moveforward();
}
}
void scan(){
time = sonar.ping();
distance = time / us_roundtrip_cm;
if(distance == 0){
distance = 100;
}
delay(10);
}
void movebackward(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void moveforward(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void moveright(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void moveleft(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void movestop(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, low);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, low);
}
thanks
#include <newping.h>
#define trigger_pin 3
#define echo_pin 2
#define max_distance 100
const int motor1pin1 = 8;
const int motor1pin2 = 9;
const int motor2pin2 =10;
const int motor2pin1 = 11;
newping sonar(trigger_pin, echo_pin, max_distance);
unsigned int time;
int distance;
int triggerdistance = 30;
int fdistance;
int ldistance;
int rdistance;
void setup()
{
pinmode(motor1pin1, output);
pinmode(motor1pin2, output);
pinmode(motor2pin1, output);
pinmode(motor2pin2, output);
}
void loop()
{
scan();
fdistance = distance;
if(fdistance < triggerdistance){
movebackward();
delay(1000);
moveright();
delay(500);
movestop();
scan();
rdistance = distance;
moveleft();
delay(1000);
movestop();
scan();
ldistance = distance;
if(ldistance < rdistance){
moveright();
delay(200);
moveforward();
}
else{
moveforward();
}
}
else{
moveforward();
}
}
void scan(){
time = sonar.ping();
distance = time / us_roundtrip_cm;
if(distance == 0){
distance = 100;
}
delay(10);
}
void movebackward(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void moveforward(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void moveright(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void moveleft(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, high);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, high);
}
void movestop(){
digitalwrite(motor1pin1, low);
digitalwrite(motor1pin2, low);
digitalwrite(motor2pin1, low);
digitalwrite(motor2pin2, low);
}
include servo library , create instance:
or wanting servo something?
code: [select]
#include <servo.h>
servo myservo;
or wanting servo something?
Arduino Forum > Using Arduino > Project Guidance > HOw DO I add servo to this sketch
arduino
Comments
Post a Comment