Advertisement
Victoryus82

Wemos rc car alfa

May 28th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3. #include <Adafruit_MotorShield.h>
  4. #include <Servo.h>
  5.  
  6. unsigned long StartMillis;
  7. unsigned long AktualisMillis;
  8. const unsigned long szunet = 500;
  9. const byte bal_index = 0;
  10. const byte jobb_index = 15;
  11. int UdpCsomag;
  12. int index_seged;  // ha 0 akkor off, 1-es balra, 2-es jobbra
  13.  
  14. //--wifi config
  15.   #ifndef APSSID
  16.   #define APSSID "D1R2"
  17.   #define APPSK  "nincsnincs"
  18.   #endif
  19.   const char *ssid = APSSID;
  20.   const char *password = APPSK;
  21.   IPAddress ip(192, 168, 1, 10); // Wemos ip címe
  22.   IPAddress netmask(255, 255, 255, 0); //netmask
  23.  
  24. //--udp config
  25.   WiFiUDP UdpServer;
  26.   unsigned int UDPPort = 4000; //udp szerver portja
  27.   const int packetSize = 20; // csomag mérete
  28.   char packetBuffer[packetSize];
  29.  
  30. //---SERVO motor megadása
  31.    Servo myservo;
  32.    
  33. //--MOTOR shield object
  34.    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  35.    Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
  36.  
  37. void setup()
  38. {
  39.   Serial.begin(115200);  //start Serial
  40.   pinMode(bal_index, OUTPUT);
  41.   pinMode(jobb_index, OUTPUT);
  42.   StartMillis = millis();  //initial start time
  43.  
  44.   //--WIFI
  45.   WiFi.softAPConfig(ip, ip, netmask);
  46.   WiFi.softAP(ssid, password, 8);
  47.   Serial.println("Indul...");
  48.   Serial.println("Wemos D1R2 UDP teszt 2019.05.22");
  49.   Serial.println((String)"SSID: " + ssid + "  PASS: " + password);
  50.   Serial.println((String)"RoboRemo app udp adatok " + ip.toString() + ":" + UDPPort);
  51.  
  52.   //--UDP start
  53.   UdpServer.begin(UDPPort);
  54.  
  55.   //--MOTOR
  56.   AFMS.begin();  // create with the default frequency 1.6KHz  AFMS.begin(1000); OR with a different frequency, say 1KHz
  57.   myMotor->setSpeed(150); // Set the speed to start, from 0 (off) to 255 (max speed)
  58.   //myMotor->run(FORWARD);  
  59.   myMotor->run(RELEASE); // turn on motor
  60.  
  61.   //--Servo motor GPIO
  62.     myservo.attach(13,600,2300);  //GPIO 13, meg min max, ezt nem értem, de ennyi volt a példában
  63. }
  64.  
  65. void loop()
  66.  
  67. {
  68.   AktualisMillis = millis();
  69.   int cb = UdpServer.parsePacket();
  70.   if (cb) {
  71.  
  72.     int len = UdpServer.read(packetBuffer, packetSize); //len változó a csomag hossza
  73.     if (len > 0) {
  74.  
  75.       packetBuffer[len - 1] = '\0'; //betesz az utolsó karakternek egy lezárást
  76.  
  77.       UdpCsomag = atoi(packetBuffer + 3);  //integert csinálok a packetbuffer tartalmából
  78.       Serial.println("Van udp kapcsolat!");
  79.       Serial.println(UdpCsomag);
  80.  
  81.       if (UdpCsomag == 501)
  82.       {
  83.         index_seged = 1;
  84.       }
  85.       else if (UdpCsomag == 502)
  86.       {
  87.         index_seged = 2;
  88.       }
  89.       else if (UdpCsomag == 503)
  90.       {
  91.         index_seged = 3;
  92.       }
  93.       else if (UdpCsomag == 500)
  94.       {
  95.         index_seged = 0;
  96.       }
  97.  
  98.       if (index_seged == 0)
  99.       {
  100.         digitalWrite(bal_index, LOW);
  101.         digitalWrite(jobb_index, LOW);
  102.       }
  103.       else if (index_seged == 1)
  104.       {
  105.         digitalWrite(jobb_index, LOW);
  106.         if (AktualisMillis - StartMillis >= szunet)
  107.         {
  108.           digitalWrite(bal_index, !digitalRead(bal_index));
  109.           StartMillis = AktualisMillis;
  110.         }
  111.       }
  112.       else if (index_seged == 2)
  113.       {
  114.         digitalWrite(bal_index, LOW);
  115.         if (AktualisMillis - StartMillis >= szunet)
  116.         {
  117.           digitalWrite(jobb_index, !digitalRead(jobb_index));  
  118.           StartMillis = AktualisMillis;
  119.         }
  120.       }
  121.       else if (index_seged == 3)
  122.       {
  123.         if (AktualisMillis - StartMillis >= szunet)
  124.         {
  125.           digitalWrite(jobb_index, !digitalRead(jobb_index));
  126.           digitalWrite(bal_index, !digitalRead(bal_index));
  127.           StartMillis = AktualisMillis;
  128.         }
  129.       }
  130.  
  131.   if (UdpCsomag <= 2300 && UdpCsomag >= 600)
  132.             {
  133.            
  134.               myservo.write(UdpCsomag);
  135.          
  136.              
  137.             } else
  138.               {
  139.          
  140.                 if (UdpCsomag >= 0 && UdpCsomag < 200)
  141.                   {
  142.                     myMotor->run(FORWARD);
  143.                     myMotor->setSpeed(UdpCsomag);
  144.                  
  145.                   }
  146.                 else if (UdpCsomag < 0 && UdpCsomag > -200)
  147.                   {
  148.                     myMotor->run(BACKWARD);
  149.                     myMotor->setSpeed(UdpCsomag*-1);
  150.                
  151.                   }
  152.        
  153.               }
  154.      
  155.     }
  156.  
  157.   }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement