close-circle
Close
0%
0%

HariFun #150 Playing with ESP32

Goofing around with the new ESP32 module

Similar projects worth following
close
Thanks to Daniel from EZSBC.com for sending me some ESP32 modules!
Giveaway coming soon. Stay tuned on my YouTube Channel:
https://www.youtube.com/user/hwiguna

  • Twelve WiFi controlled Servos on the ESP32

    Hari Wiguna04/09/2017 at 22:44 0 comments

    Please forgive this messy code, I was just goofing around and did not plan on sharing it, but here I am sharing it. LOL

    #include <WiFi.h>
    
    const char* ssid     = "(Your WiFi Access Point Name)";
    const char* password = "(Your WiFi Access Point Password)";
    int ledPin = 4;
    
    WiFiServer server(80);
    
    //----------------------------------
    #include "esp32-hal-ledc.h"
    
    uint32_t maxint = 65535;
    uint32_t servoMax = 8000;
    uint32_t servoMin = 1700;
    uint32_t servoLow = servoMin; //1700 + 1300;
    uint32_t servoHigh = 1700 + 1000 + 1300; //servoLow+1000;
    uint32_t servoCenter = (servoLow + servoHigh) / 2;
    
    const byte numberOfServos = 12;
    byte servoPins[numberOfServos] = {5, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23};
    char command = 0;
    
    void setupServo()
    {
      for (byte servoIndex = 0; servoIndex < numberOfServos; servoIndex++)
      {
        byte channel = servoIndex;
        byte servoPin = servoPins[servoIndex];
    
        ledcSetup(channel, 50, 16); // channel X, 50 Hz, 16-bit depth
        ledcAttachPin(servoPin, channel);   // GPIO servoPin on channel X
        ledcWrite(channel, servoHigh); //servoMin
      }
      delay(2000);
    }
    //----------------------------------
    
    void setup()
    {
      Serial.begin(115200);
      pinMode(ledPin, OUTPUT);      // set the LED pin mode
    
      delay(10);
    
      // We start by connecting to a WiFi network
    
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    
      server.begin();
    
      setupServo();
    }
    
    void Marching()
    {
      for (byte servoIndex = 0; servoIndex < numberOfServos; servoIndex++)
      {
        byte channel = servoIndex;
        ledcWrite(channel, servoHigh);
      }
      delay(500);
      for (byte servoIndex = 0; servoIndex < numberOfServos; servoIndex++)
      {
        byte channel = servoIndex;
        ledcWrite(channel, servoLow);
      }
      delay(500);
    }
    
    void LeftToRight(int servoValue, int dly)
    {
      for (int head = 0; head < numberOfServos; head++)
      {
        if (head >= 0 && head < numberOfServos) ledcWrite(head, servoValue);
        delay(dly);
      }
    }
    
    void RightToLeft(int servoValue, int dly)
    {
      for (int head = numberOfServos - 1; head >= 0 ; head--)
      {
        if (head >= 0 && head < numberOfServos) ledcWrite(head,  servoValue);
        delay(dly);
      }
    }
    
    void SpreadOut(int dly)
    {
      int half = (numberOfServos / 2);
      for (int i = 0; i <= half ; i++)
      {
        int left = half - i;
        int right = half + i;
        if (left >= 0 && left < numberOfServos) ledcWrite(left,  servoHigh);
        if (right >= 0 && right < numberOfServos) ledcWrite(right,  servoLow);
        delay(dly);
      }
    }
    void SpreadOutRestore(int dly)
    {
      int half = (numberOfServos / 2);
      for (int i = 0; i <= half ; i++)
      {
        int left = half - i;
        int right = half + i;
        if (left >= 0 && left < numberOfServos) ledcWrite(left,  servoCenter);
        if (right >= 0 && right < numberOfServos) ledcWrite(right,  servoCenter);
        delay(dly);
      }
    }
    void OutIn(int dly)
    {
      int half = (numberOfServos / 2);
      for (int i = 0; i <= half ; i++)
      {
        int left = (numberOfServos - 1) - i;
        int right = i;
        if (left >= 0 && left < numberOfServos) ledcWrite(left,  servoCenter);
        if (right >= 0 && right < numberOfServos) ledcWrite(right,  servoCenter);
        delay(dly);
      }
    }
    
    int value = 0;
    
    void SendHtmlResponse(WiFiClient client)
    {
      // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
      // and a content-type so the client knows what's coming, then a blank line:
      client.println("HTTP/1.1 200 OK");
      client.println("Content-type:text/html");
      client.println();
    
      // the content of the HTTP response follows the header:
      client.print("<h1 style='font-size:400%;'>");
      client.print("<a href='/'>Home</a><br>");
      client.print("Left to Right: <a href='/A'>&#92;&#92;&#92;</a>&nbsp;&nbsp;<a href='/B'>///</a><br>");
      client.print("Right to Left: <a href='/C'>&#92;&#92;&#92;</a>&nbsp;&nbsp;<a href='/D'>///</a><br>");
      client.print("All: <a href='/E'>&#92;&#92;&#92;</a>&nbsp;&nbsp;<a href='/H'>Center</a>&nbsp;&nbsp;<a...
    Read more »

View project log

Enjoy this project?

Share

Discussions

experimentool wrote 04/17/2017 at 15:01 point

Awesome demo Hari.  I have completed many projects with the esp8266 and look forward to working with the esp32

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates