arduino sketch for the domstic robot
/*THIS IS THE CODE FOR THE ROBOT TO MOVE*/
#include<NewPing.h>
#include <AFMotor.h>
#include<SoftwareSerial.h>
AF_DCMotor motor(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
float t1;
float t2;
float t3;
float t4;
#define MAX_DISTANCE 200
#define trig_pin A0
#define echo_pin A1
#define trig_pin2 A2
#define echo_pin2 A3
#define trig_pin3 A4
#define echo_pin3 A4
#define trig_pin4 13
#define echo_pin4 2
NewPing sonar1(trig_pin, echo_pin , MAX_DISTANCE);
NewPing sonar2(trig_pin2, echo_pin2 , MAX_DISTANCE);
NewPing sonar3(trig_pin3, echo_pin3 , MAX_DISTANCE);
NewPing sonar4(trig_pin4, echo_pin4 , MAX_DISTANCE);
int direction = 0;
//String cmd, status, start, dontstart;
void setup() {
Serial.begin(9600);
Serial.println("Motor party!");
// pinMode(status, OUTPUT);
pinMode(trig_pin, OUTPUT);
pinMode(trig_pin2, OUTPUT);
pinMode(trig_pin3, OUTPUT);
pinMode(trig_pin4, OUTPUT);
pinMode(echo_pin, INPUT);
pinMode(echo_pin2, INPUT);
pinMode(echo_pin3, INPUT);
pinMode(echo_pin4, INPUT);
motor3.setSpeed(200);
motor3.run(RELEASE);
// turn on motor #2
motor4.setSpeed(200);
motor4.run(RELEASE);
}
void movingf()
{
motor.run(FORWARD);
motor.setSpeed(255);
motor2.run(FORWARD);
motor2.setSpeed(255);
}
void movingb()
{
motor3.run(FORWARD);
motor3.setSpeed(200);
motor4.run(FORWARD);
motor4.setSpeed(200);
}
void movingr()
{
motor.run(RELEASE);
motor2.run(FORWARD);
motor2.setSpeed(200);
}
void movingl()
{
motor.run(FORWARD);
motor.setSpeed(200);
motor2.run(RELEASE);
}
void allstop()
{
motor.run(RELEASE);
motor.setSpeed(0);
motor2.run(RELEASE);
motor2.setSpeed(0);
motor3.run(RELEASE);
motor3.setSpeed(0);
motor4.run(RELEASE);
motor4.setSpeed(0);
}
int abs_follow()
{
motor.run(BACKWARD);
motor.setSpeed(200);
motor2.run(BACKWARD);
motor2.setSpeed(200);
}
void loop()
{
delay(50);
unsigned int uS1 = sonar1.ping();
pinMode(echo_pin, OUTPUT);
digitalWrite(echo_pin, LOW);
pinMode(echo_pin, INPUT);
t1 = (uS1 / US_ROUNDTRIP_CM);
delay(50);
unsigned int uS2 = sonar2.ping();
pinMode(echo_pin2, OUTPUT);
digitalWrite(echo_pin2, LOW);
pinMode(echo_pin2, INPUT);
t2 = (uS2 / US_ROUNDTRIP_CM);
unsigned int uS3 = sonar3.ping();
pinMode(echo_pin3, OUTPUT);
digitalWrite(echo_pin3, LOW);
pinMode(echo_pin3, INPUT);
t3 =( uS3 / US_ROUNDTRIP_CM);
unsigned int uS4 = sonar4.ping();
pinMode(echo_pin4, OUTPUT);
digitalWrite(echo_pin4, LOW);
pinMode(echo_pin4, INPUT);
t4 = (uS4 / US_ROUNDTRIP_CM);
if(t1 > 9)
{
Serial.print(t1);
movingf();
Serial.println("moving forward");
}
else if(t2 > 5)
{
Serial.print(t2);
movingl();
Serial.println("moving right");
}
else if(t3 > 5)
{
movingr();
Serial.print(t3);
Serial.println("moving left");
} else if (t1 <5 || t2<5 || t3<5 )
{
allstop();
Serial.println("every direction is blocked and stopped");
}
}
/*else if (cmd == dontstart)
{
Serial.print("type start to start");
} else if (cmd == start || cmd == dontstart)
{
Serial.print("don't type rather than start or dontstart");
}
*/
#include<NewPing.h>
#include <AFMotor.h>
#include<SoftwareSerial.h>
AF_DCMotor motor(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
float t1;
float t2;
float t3;
float t4;
#define MAX_DISTANCE 200
#define trig_pin A0
#define echo_pin A1
#define trig_pin2 A2
#define echo_pin2 A3
#define trig_pin3 A4
#define echo_pin3 A4
#define trig_pin4 13
#define echo_pin4 2
NewPing sonar1(trig_pin, echo_pin , MAX_DISTANCE);
NewPing sonar2(trig_pin2, echo_pin2 , MAX_DISTANCE);
NewPing sonar3(trig_pin3, echo_pin3 , MAX_DISTANCE);
NewPing sonar4(trig_pin4, echo_pin4 , MAX_DISTANCE);
int direction = 0;
//String cmd, status, start, dontstart;
void setup() {
Serial.begin(9600);
Serial.println("Motor party!");
// pinMode(status, OUTPUT);
pinMode(trig_pin, OUTPUT);
pinMode(trig_pin2, OUTPUT);
pinMode(trig_pin3, OUTPUT);
pinMode(trig_pin4, OUTPUT);
pinMode(echo_pin, INPUT);
pinMode(echo_pin2, INPUT);
pinMode(echo_pin3, INPUT);
pinMode(echo_pin4, INPUT);
motor3.setSpeed(200);
motor3.run(RELEASE);
// turn on motor #2
motor4.setSpeed(200);
motor4.run(RELEASE);
}
void movingf()
{
motor.run(FORWARD);
motor.setSpeed(255);
motor2.run(FORWARD);
motor2.setSpeed(255);
}
void movingb()
{
motor3.run(FORWARD);
motor3.setSpeed(200);
motor4.run(FORWARD);
motor4.setSpeed(200);
}
void movingr()
{
motor.run(RELEASE);
motor2.run(FORWARD);
motor2.setSpeed(200);
}
void movingl()
{
motor.run(FORWARD);
motor.setSpeed(200);
motor2.run(RELEASE);
}
void allstop()
{
motor.run(RELEASE);
motor.setSpeed(0);
motor2.run(RELEASE);
motor2.setSpeed(0);
motor3.run(RELEASE);
motor3.setSpeed(0);
motor4.run(RELEASE);
motor4.setSpeed(0);
}
int abs_follow()
{
motor.run(BACKWARD);
motor.setSpeed(200);
motor2.run(BACKWARD);
motor2.setSpeed(200);
}
void loop()
{
delay(50);
unsigned int uS1 = sonar1.ping();
pinMode(echo_pin, OUTPUT);
digitalWrite(echo_pin, LOW);
pinMode(echo_pin, INPUT);
t1 = (uS1 / US_ROUNDTRIP_CM);
delay(50);
unsigned int uS2 = sonar2.ping();
pinMode(echo_pin2, OUTPUT);
digitalWrite(echo_pin2, LOW);
pinMode(echo_pin2, INPUT);
t2 = (uS2 / US_ROUNDTRIP_CM);
unsigned int uS3 = sonar3.ping();
pinMode(echo_pin3, OUTPUT);
digitalWrite(echo_pin3, LOW);
pinMode(echo_pin3, INPUT);
t3 =( uS3 / US_ROUNDTRIP_CM);
unsigned int uS4 = sonar4.ping();
pinMode(echo_pin4, OUTPUT);
digitalWrite(echo_pin4, LOW);
pinMode(echo_pin4, INPUT);
t4 = (uS4 / US_ROUNDTRIP_CM);
if(t1 > 9)
{
Serial.print(t1);
movingf();
Serial.println("moving forward");
}
else if(t2 > 5)
{
Serial.print(t2);
movingl();
Serial.println("moving right");
}
else if(t3 > 5)
{
movingr();
Serial.print(t3);
Serial.println("moving left");
} else if (t1 <5 || t2<5 || t3<5 )
{
allstop();
Serial.println("every direction is blocked and stopped");
}
}
/*else if (cmd == dontstart)
{
Serial.print("type start to start");
} else if (cmd == start || cmd == dontstart)
{
Serial.print("don't type rather than start or dontstart");
}
*/
Comments
Post a Comment