Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- int motor1Pin1 = 11; // pin 7 on L293D IC
- int motor1Pin2 = 10; // pin 8 on L293D IC
- int motor2Pin1 = 9; // pin 12 on L293D IC
- int motor2Pin2 = 8; // pin 13 on L293D IC
- int EnPinL = 2; // pin 12 on L293D IC
- int EnPinR = 3; // pin 13 on L293D IC
- int Sensor_1 = 0;
- int Sensor_2 = 0;
- int pirPin = 13;
- int ledPir = 4;
- SoftwareSerial mySerial(6, 7);
- char mode = -1;
- int timeflag = 0;
- int starttime = 0;
- void setup() {
- // sets the pins as outputs:
- Serial.begin(9600);
- mySerial.begin(9600);
- pinMode(motor1Pin1, OUTPUT);
- pinMode(motor1Pin2, OUTPUT);
- pinMode(motor2Pin1, OUTPUT);
- pinMode(motor2Pin2, OUTPUT);
- pinMode(EnPinL, OUTPUT);
- pinMode(EnPinR, OUTPUT);
- pinMode(pirPin, INPUT);
- pinMode(ledPir, OUTPUT);
- digitalWrite(ledPir, LOW);
- }
- void motorback() {
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- }
- void motorfront() {
- digitalWrite(motor1Pin1, HIGH);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, HIGH);
- }
- void motorleft()
- {
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, HIGH);
- }
- void motorright() {
- digitalWrite(motor1Pin1, HIGH);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, LOW);
- }
- void motorstop() {
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, LOW);
- }
- void readsensor() {
- }
- char getChar() {
- while (mySerial.available() == 0);
- return ((char) mySerial.read());
- }
- int getSerial() {
- if (mySerial.available()) {
- return getChar();
- }
- return -1;
- }
- void loop() {
- delay(100);
- char cmd = getSerial();
- //char cmd = getChar();
- Serial.print(cmd);
- if (cmd == 'M' || cmd == 'P' || cmd == 'Z') {
- mode = cmd;
- }
- if (mode == 'M') {
- //remote();
- Serial.println("Remote|");
- //remote();
- }
- if (mode == 'P') {
- //patrol();
- Serial.println("Patrol|");
- delay(200);
- patrol();
- }
- if (mode == 'Z') {
- //siram();
- Serial.println("Siram|");
- //siram();
- }
- }
- void patrol() {
- if(timeflag == 0){
- starttime = millis();
- Serial.print(starttime);
- timeflag = 1;
- }
- readsurface();
- Serial.print(millis());
- Serial.print("/n");
- if(millis() >= (starttime + 10000)){
- scanPIR();
- timeflag = 0;
- }
- }
- void scanPIR() {
- motorstop();
- delay(2000);
- if (digitalRead(pirPin) == HIGH) {
- Serial.println("Motion Detected");
- digitalWrite(ledPir, HIGH);
- delay(1000);
- } else {
- Serial.println("No Motion");
- digitalWrite(ledPir, LOW);
- }
- }
- void readsurface() {
- Sensor_2 = analogRead(A0);
- Sensor_1 = analogRead(A1);
- if (Sensor_1 < 300 && Sensor_2 > 300) {
- Serial.println("Moving Backward");
- motorback();
- delay(1000);
- Serial.println("Right Turn");
- motorright();
- delay(1000);
- } else if (Sensor_2 < 300 && Sensor_1 > 300) {
- Serial.println("Moving Backward");
- motorback();
- delay(1000);
- Serial.println("Left Turn");
- motorleft();
- delay(1000);
- } else {
- Serial.println("Moving Forward");
- motorfront();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment