donmike25 icon

Donmike IR

donmike25 | PRO | 02/09/16 03:00:57 AM UTC | 0 ⭐ | 214 👁️ | Never ⏰ | []
text |

2.64 KB

|

None

|

0 👍

/

0 👎

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 = 6; // pin 12 on L293D IC
int EnPinR = 7; // pin 13 on L293D IC 
 int Sensor_1 = 0;
int Sensor_2 = 0;
 int pirPin = 13;
 int ledPir = 4;
 String readBT;
 void setup() {
    // sets the pins as outputs:
    Serial.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 loop() {
       Sensor_2 = analogRead(A0);
    Sensor_1 = analogRead(A1);
  while (Serial.available()){
   delay (3);
  char c = Serial.read();
  readBT += c;
  }
   if (readBT.length() >0) {
    Serial.println(readBT);
    if (readBT == "2ON")
    {
      digitalWrite(EnPinL, HIGH);
      digitalWrite(EnPinR, HIGH);
    }
    if (readBT == "2OFF")
    {
    digitalWrite(EnPinL, LOW);
   digitalWrite(EnPinR, LOW);
     }
    readBT ="";}
     if (digitalRead(pirPin) == HIGH){
      Serial.println("Motion Detected");
      digitalWrite(ledPir,HIGH);
     }
     if (digitalRead(pirPin) == LOW){
      Serial.println("No Motion");
      digitalWrite(ledPir,LOW);
    }
      if (Sensor_1 < 300 && Sensor_2 > 300)
      {
           Serial.println("Moving Backward");
      digitalWrite(motor1Pin1, LOW);
      digitalWrite(motor1Pin2, HIGH);
      digitalWrite(motor2Pin1, HIGH);
      digitalWrite(motor2Pin2, LOW);
      delay(1000);
        Serial.println("Right Turn");
      digitalWrite(motor1Pin1, HIGH);
      digitalWrite(motor1Pin2, LOW);
      digitalWrite(motor2Pin1, LOW);
      digitalWrite(motor2Pin2, LOW);
       delay(1000);
        }
       else if (Sensor_2 <300 && Sensor_1 > 300)
      {
           Serial.println("Moving Backward");
      digitalWrite(motor1Pin1, LOW);
      digitalWrite(motor1Pin2, HIGH);
      digitalWrite(motor2Pin1, HIGH);
      digitalWrite(motor2Pin2, LOW);
      delay(1000);
        Serial.println("Left Turn");
      digitalWrite(motor1Pin1, LOW);
      digitalWrite(motor1Pin2, LOW);
      digitalWrite(motor2Pin1, LOW);
      digitalWrite(motor2Pin2, HIGH);
       delay(1000);
        }
       else
      {
         Serial.println("Moving Forward");
      digitalWrite(motor1Pin1, HIGH);
      digitalWrite(motor1Pin2, LOW);
      digitalWrite(motor2Pin1, LOW);
      digitalWrite(motor2Pin2, HIGH);
      }
       }

Comments