Advertisement
Guest User

Cytron Robo Pico

a guest
Jun 30th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.83 KB | Software | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #define LED_PIN 18
  3. #define LED_COUNT 2
  4.  
  5. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  6.  
  7. #include <WiFi.h>
  8. #include <Servo.h>
  9.  
  10.  
  11. Servo myservo;
  12. Servo myservo2;
  13.  
  14. // GPIO the servo is attached to
  15. static const int servoPin = 13;
  16.  
  17. // Replace with your network credentials
  18. const char* ssid     = "ssid network";
  19. const char* password = "ssid password";
  20.  
  21. // Set web server port number to 80
  22. WiFiServer server(80);
  23.  
  24. // Variable to store the HTTP request
  25. String header;
  26. unsigned long lastTimer = 0;
  27. unsigned long lastTimer2 = 0;
  28.  
  29. // Decode HTTP GET value
  30. String valueString = String(5);
  31. int pos1 = 0;
  32. int pos2 = 0;
  33.  
  34. // Current time
  35. unsigned long currentTime = millis();
  36. // Previous time
  37. unsigned long previousTime = 0;
  38. // Define timeout time in milliseconds (example: 2000ms = 2s)
  39. const long timeoutTime = 2000;
  40. int pos = 90;
  41.  
  42. void setup() {
  43.   Serial.begin(115200);
  44.   pinMode(0, OUTPUT);
  45.   pinMode(1, OUTPUT);
  46.   pinMode(2, OUTPUT);
  47.   pinMode(3, OUTPUT);
  48.   pinMode(4, OUTPUT);
  49.   pinMode(5, OUTPUT);
  50.   pinMode(6, OUTPUT);
  51.   pinMode(7, OUTPUT);
  52.   pinMode(16, OUTPUT);
  53.   pinMode(17, OUTPUT);
  54.   pinMode(26, OUTPUT);
  55.   pinMode(27, OUTPUT);
  56.  
  57.  
  58.   strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  59.   strip.show();            // Turn OFF all pixels ASAP
  60.   strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  61.  
  62.   myservo.attach(15);
  63.   myservo2.attach(14);
  64.  
  65.   // Connect to Wi-Fi network with SSID and password
  66.   Serial.print("Connecting to ");
  67.   Serial.println(ssid);
  68.   WiFi.begin(ssid, password);
  69.   while (WiFi.status() != WL_CONNECTED) {
  70.     delay(500);
  71.     Serial.print(".");
  72.   }
  73.   // Print local IP address and start web server
  74.   Serial.println("");
  75.   Serial.println("WiFi connected.");
  76.   Serial.println("IP address: ");
  77.   Serial.println(WiFi.localIP());
  78.   server.begin();
  79. }
  80.  
  81. void loop() {
  82.   WiFiClient client = server.available();   // Listen for incoming clients
  83.  
  84.   int tt = 50;
  85.   int color = map(pos, 0, 180, 0, 255);
  86.  
  87.   if ( pos <  20 ) {
  88.     //left signal
  89.     strip.setPixelColor(0, strip.Color(255, 0, 0));
  90.     strip.setPixelColor(1, strip.Color(0, 0, 0));
  91.  
  92.     digitalWrite(26, HIGH);
  93.     delay(tt);
  94.     digitalWrite(6, HIGH);
  95.     digitalWrite(26, LOW);
  96.     delay(tt);
  97.     digitalWrite(17, HIGH);
  98.     digitalWrite(6, LOW);
  99.     delay(tt);
  100.     digitalWrite(16, HIGH);
  101.     digitalWrite(17, LOW);
  102.     delay(tt);
  103.     digitalWrite(5, HIGH);
  104.     digitalWrite(16, LOW);
  105.     delay(tt);
  106.     digitalWrite(4, HIGH);
  107.     digitalWrite(5, LOW);
  108.     delay(tt);
  109.     digitalWrite(3, HIGH);
  110.     digitalWrite(4, LOW);
  111.     delay(tt);
  112.     digitalWrite(2, HIGH);
  113.     digitalWrite(3, LOW);
  114.     delay(tt);
  115.     digitalWrite(2, LOW);
  116.     delay(tt);
  117.  
  118.   } else if ( pos > 160 ) {
  119.  
  120.     // right signal
  121.     strip.setPixelColor(0, strip.Color(0, 0, 0));
  122.     strip.setPixelColor(1, strip.Color(0, 255, 0));
  123.  
  124.     digitalWrite(2, HIGH);
  125.     delay(tt);
  126.     digitalWrite(3, HIGH);
  127.     digitalWrite(2, LOW);
  128.     delay(tt);
  129.     digitalWrite(4, HIGH);
  130.     digitalWrite(3, LOW);
  131.     delay(tt);
  132.     digitalWrite(5, HIGH);
  133.     digitalWrite(4, LOW);
  134.     delay(tt);
  135.     digitalWrite(16, HIGH);
  136.     digitalWrite(5, LOW);
  137.     delay(tt);
  138.     digitalWrite(17, HIGH);
  139.     digitalWrite(16, LOW);
  140.     delay(tt);
  141.     digitalWrite(6, HIGH);
  142.     digitalWrite(17, LOW);
  143.     delay(tt);
  144.     digitalWrite(26, HIGH);
  145.     digitalWrite(6, LOW);
  146.     delay(tt);
  147.     digitalWrite(26, LOW);
  148.     delay(tt);
  149.   } else {
  150.     strip.setPixelColor(0, strip.Color(0, 0, 0));
  151.     strip.setPixelColor(1, strip.Color(0, 0, 0));
  152.   }
  153.  
  154.   strip.show();
  155.  
  156.  
  157.   if (client) {                             // If a new client connects,
  158.     currentTime = millis();
  159.     previousTime = currentTime;
  160.     Serial.println("New Client.");          // print a message out in the serial port
  161.     String currentLine = "";                // make a String to hold incoming data from the client
  162.     while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
  163.       currentTime = millis();
  164.       if (client.available()) {             // if there's bytes to read from the client,
  165.         char c = client.read();             // read a byte, then
  166.         Serial.write(c);                    // print it out the serial monitor
  167.         header += c;
  168.         if (c == '\n') {                    // if the byte is a newline character
  169.           // if the current line is blank, you got two newline characters in a row.
  170.           // that's the end of the client HTTP request, so send a response:
  171.           if (currentLine.length() == 0) {
  172.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  173.             // and a content-type so the client knows what's coming, then a blank line:
  174.             client.println("HTTP/1.1 200 OK");
  175.             client.println("Content-type:text/html");
  176.             client.println("Connection: close");
  177.             client.println();
  178.  
  179.             // Display the HTML web page
  180.             client.println("<!DOCTYPE html><html>");
  181.             client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  182.             client.println("<link rel=\"icon\" href=\"data:,\">");
  183.             // CSS to style the on/off buttons
  184.             // Feel free to change the background-color and font-size attributes to fit your preferences
  185.             client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial; margin-left:auto; margin-right:auto;}");
  186.             client.println(".slider { width: 300px; }</style>");
  187.             client.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>");
  188.  
  189.             // Web Page
  190.             client.println("</head><body><h1>Cytron ROBO PICO with Servo</h1>");
  191.             client.println("<p>Position: <span id=\"servoPos\"></span></p>");
  192.             client.println("<input type=\"range\" min=\"0\" max=\"180\" class=\"slider\" id=\"servoSlider\" onchange=\"servo(this.value)\" value=\"" + valueString + "\"/><P>");
  193.  
  194.             client.println("<script>");
  195.             client.println("var slider = document.getElementById(\"servoSlider\");");
  196.             client.println("var servoP = document.getElementById(\"servoPos\"); servoP.innerHTML = slider.value;");
  197.             client.println("slider.oninput = function() { slider.value = this.value; servoP.innerHTML = this.value; }");
  198.             client.println("$.ajaxSetup({timeout:1000}); function servo(pos) { ");
  199.             client.println("$.get(\"/?value=\" + pos + \"&\"); {Connection: close};}");
  200.             client.println("</script>");
  201.  
  202.             client.println("</body></html>");
  203.  
  204.             //GET /?value=180& HTTP/1.1
  205.             if (header.indexOf("GET /?value=") >= 0) {
  206.               pos1 = header.indexOf('=');
  207.               pos2 = header.indexOf('&');
  208.               valueString = header.substring(pos1 + 1, pos2);
  209.  
  210.               // Save pos for LED use
  211.               pos = valueString.toInt();
  212.  
  213.               //Rotate the servo
  214.               myservo.write(valueString.toInt());
  215.               myservo2.write(valueString.toInt());
  216.               Serial.println(valueString);
  217.             }
  218.             // The HTTP response ends with another blank line
  219.             client.println();
  220.             // Break out of the while loop
  221.             break;
  222.           } else { // if you got a newline, then clear currentLine
  223.             currentLine = "";
  224.           }
  225.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  226.           currentLine += c;      // add it to the end of the currentLine
  227.         }
  228.       }
  229.     }
  230.     // Clear the header variable
  231.     header = "";
  232.     // Close the connection
  233.     client.stop();
  234.     Serial.println("Client disconnected.");
  235.     Serial.println("");
  236.   }
  237. }
Tags: Arduino Pico
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement