#include <ESP8266WiFi.h>

#include <WiFiClient.h>

int controlauto =D6;

int controlline =D7;

/* Set these to your desired credentials. */

const char *ssid = "HTC-TECH";

const char *password = "12345678";

WiFiServer server(80);

/* Just a little test message.  Go to http://192.168.4.1 in a web browser

 * connected to this access point to see it.

 */

void setup() {

  delay(1000);

  Serial.begin(115200);

  pinMode(controlauto,OUTPUT);

  pinMode(controlline,OUTPUT);

  digitalWrite(controlauto,LOW);

  digitalWrite(controlline,LOW);

  Serial.println();

  Serial.print("Configuring access point...");

  /* You can remove the password parameter if you want the AP to be open. */

  WiFi.softAP(ssid, password);

  WiFi.mode(WIFI_AP);

  IPAddress myIP = WiFi.softAPIP();

  Serial.print(WIFI_AP);

  Serial.println(myIP);

//server.on("/", handleRoot);

  server.begin();

  Serial.println("HTTP server started");

//  dichuyen(0,0);

}

void loop() {

  WiFiClient client = server.available();

  if (!client) {

    return;

  }

  Serial.println("new client");

  while(!client.available()){

    delay(1);

  }

  String request = client.readStringUntil('\r');

  Serial.println(request);

  client.flush();

if(request.indexOf("controlphone") != -1)

{

      digitalWrite(controlauto,LOW);

      digitalWrite(controlline,LOW);

  if (request.indexOf("tien") != -1)  {

      Serial.println("tien");

      //chaythuan();

      //dichuyen(500,500);

      client.stop();

      delay(1);

  }

  if (request.indexOf("lui") != -1)  {

      Serial.println("lui");

      //chaynghich();

      //dichuyen(500,500);

      client.stop();

      delay(1);

  }

   if (request.indexOf("trai") != -1)  {

      Serial.println("trai");

      //chaythuan();

      //dichuyen(600,50);

      client.stop();

      delay(1);

  }

    if (request.indexOf("phai") != -1)  {

      Serial.println("phai");

      //chaythuan();

      //dichuyen(50,600);

      client.stop();

      delay(1);

  }

      if (request.indexOf("dung") != -1)  {

      Serial.println("dung");

      //dichuyen(0,0);

      client.stop();

      delay(1);

  }

}

if(request.indexOf("controlauto") != -1)

{    

      digitalWrite(controlauto,HIGH);

      digitalWrite(controlline,LOW); 

      client.stop();

      delayMicroseconds(100); 

}

if(request.indexOf("controlline") != -1)

{    

      digitalWrite(controlline,HIGH);

      digitalWrite(controlauto,LOW); 

      client.stop();

      delayMicroseconds(100); 

}

}

/*****************************************************************/

  1. Code HTC-TECH UNO R3

//Khai báo chân tín hiệu motor A

#include <Servo.h>

int pos = 0;

//int enA = 3;

int in1 = 7;

int in4 = 6; 

//Khai báo chân tín hiệu cho motor B

int in2 = 5;

int in3 = 4;

//int enB = 9; 

int i=0;

int S6=10;

int S7=11;

int controlauto =12;

int controlline =8;

int S1 = A0;                      // định nghĩa các chân cảm biến

int S2 = A1;                      //************************

int S3 = A2;

int S4 = A3;

int S5 = A4;

int servoRC=2;

int action=0;

Servo myservo,tatservo;

unsigned long left_distance,right_distance,max_distance;

  #define TIME_OUT 5000

  float Getdistance()

  {                      // waits 15ms for the servo to reach the position

  long duration, distanceCm;

  digitalWrite(S6, LOW);

  delayMicroseconds(2);

    digitalWrite(S6, HIGH);

    delayMicroseconds(10);

    digitalWrite(S6, LOW);

 

    duration = pulseIn(S7, HIGH, TIME_OUT);

  // convert to distance

  distanceCm = duration /  2 / 29.1;

  Serial.print(distanceCm);

  Serial.println("cm");

  delay(500);

  return distanceCm;

  }

void kctrai();

void kcphai();

void reset();

void setup()

{

    Serial.begin(115200);

    myservo.attach(servoRC);

    pinMode(S6, OUTPUT);

    pinMode(S7, INPUT);

    pinMode(controlauto, INPUT);

    pinMode(controlline, INPUT);

    pinMode(in1, OUTPUT);

    pinMode(in3, OUTPUT);

    pinMode(in2, OUTPUT);

    pinMode(in4, OUTPUT);

 

   pinMode(S1, INPUT);         // định nghĩa các ngõ vào từ các cảm biến S1

    pinMode(S2, INPUT);

    pinMode(S3, INPUT);

    pinMode(S4, INPUT);

    pinMode(S5, INPUT);

    digitalWrite(in1,LOW);

    digitalWrite(in2,LOW);

    digitalWrite(in3,LOW);

    digitalWrite(in4,LOW);

    digitalWrite(controlauto,LOW);

    digitalWrite(controlline,LOW);

 

    chaythuan(0,0);

    myservo.write(90);

    delay(100);

 

 

}

 

void chaythuan(int a,int b)

{   

        digitalWrite(in3, LOW);

        digitalWrite(in1,LOW);

        analogWrite(in4,b);

        analogWrite(in2,a);

}

void chaynghich(int a, int b)

{   

        digitalWrite(in3, HIGH);

        digitalWrite(in1,HIGH);

        analogWrite(in4,b);

        analogWrite(in2,a);

}

 

void motordung()

{

    digitalWrite(in1,LOW);

    digitalWrite(in2,LOW);

    digitalWrite(in3,LOW);

    digitalWrite(in4,LOW);

}

void loop()

{

    if(digitalRead(controlauto)==HIGH)

    {

          long distance = Getdistance();

          long check=45;

          delay(100);

 

          if(distance>check||distance==0)

           chaythuan(360,360);

          else if(distance<=check && distance>0)

          {

            chaynghich(400,400);

            delay(500);

            chaythuan(0,0);

            myservo.write(180);

            left_distance=Getdistance();

            delay(2000);

            myservo.write(0);          

            right_distance=Getdistance();           

            delay(2000);

            max_distance=max(left_distance,right_distance);

                if(max_distance==right_distance)

               {

                  Serial.print("right: ");

                  Serial.println(right_distance);

                  reset();

                  if(max_distance==0)

                  chaythuan(360,360);

                  chaythuan(400,0);

                  delay(600);

                  distance = Getdistance();

               if(distance>check||distance==0)

                    {

                    chaythuan(360,360);

 

               }

               }

               if(max_distance==left_distance)

               {

                   Serial.print("left: ");

                  Serial.println(left_distance);

                  reset();

                  if(max_distance==0)

                  chaythuan(360,360);

                  chaythuan(0,400);

                  delay(600);

                  distance = Getdistance();

 

                  if(distance>check||distance==0)

                    {

                      chaythuan(360,360);

                    }

               }

          }   

 

    }

    else if(digitalRead(controlline)==HIGH)

    {

      //5 cam bien tac dong

  bool S[5];

  S[0] = digitalRead(S1);

  S[1] = digitalRead(S2);

  S[2] = digitalRead(S3);

  S[3] = digitalRead(S4);

  S[4] = digitalRead(S5);

  if(S[0] && S[1] && !S[2] && S[3] && S[4]) {

    action = 0;

  } else if(S[0] && !S[1] && S[2] && S[3] && S[4] ) {

    action = -1;

  } else if(!S[0] && S[1] && S[2] && S[3] && S[4] ) {

    action = 1;

  } else if(S[0] && S[1] && S[2] && !S[3] && S[4] ) {

    action = -2;

  } else if(S[0] && S[1] && S[2] && S[3] && !S[4] ) {

    action = 2;

  }

   else if(S[0] && S[1] && S[2] && S[3] && S[4] ) {

    action = -3;

   }

    else if(!S[0] && !S[1] && !S[2] && !S[3] && !S[4] ) {

    action = 3;

  }

  Serial.println(action);

  switch(action) {

    dan:

   case 0: chaythuan(350, 350); break;

    case -1: {

      if(S[2])

      {

      chaythuan(380, 0);

      delay(150);

 

      }

      else goto dan;

 

 

    }break;

 

    case 1:

    {

      if(S[2])

      {

        chaythuan(400, 0);

        delay(150);

      }

        else goto dan;

    }

    break;

    case -2:

    {

      if(S[2])

      {

        chaythuan(0, 380);

      delay(150);

      }

      else goto dan;

    }

    break;

    case 2:

    {

      if(S[2])

      {

        chaythuan(0, 400);

      delay(150);

      }

      else goto dan;

    }

      break;

      case -3:

    {

        chaynghich(460, 400);

        delay(100);

 

    }

    break;

    default:

    chaythuan(0, 0);

 

}

    }

 

 else if(Serial.available()>0)

    {

      motordung();

      String data = Serial.readString();

      Serial.println(data);

      if(data.indexOf("tien") !=-1)

      {

        chaythuan(400,400);

      }

      if(data.indexOf("trai") !=-1)

      {

        chaythuan(500,100);

      }

      if(data.indexOf("phai") !=-1)

      {

        chaythuan(100,500);

      }

     if(data.indexOf("dung") !=-1)

      {

        motordung();

      }

      if(data.indexOf("lui") !=-1)

      {

        chaynghich(120,120);

      }    

    }

    }

 

void reset()

{

     myservo.write(90);

}

0
popup

Số lượng:

Tổng tiền:

Xin chào
close nav