Home > Robots > 3D Printed Tank

09/06/2023

3D Printed Tank

Writing about 3D Printed Tank for educational and informational purposes only. however, do not hesitate to use this information on your own risk as we make no warranty of any kind.

3D Printed Tank

Original Design Credit To Tim Clark: 
Drogerdy - Raspberry Pi Controlled Tank Bot -  by timmiclark is licensed under the Creative Commons - Attribution - Share Alikelicense.
Profile: https://www.thingiverse.com/timmiclark/designs 
Original Files: https://www.thingiverse.com/thing:652851
Modified Track: https://www.thingiverse.com/thing:6165652
License: https://creativecommons.org/licenses/by-sa/3.0/

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
//To Access, use IP 192.168.4.1 for ESP8266 Access Point page
// Set your network credentials for the access point
const char* apSSID = "TechsPassion Tank";
const char* apPassword = "";

// Create an instance of the web server
ESP8266WebServer server(80);

// Define the GPIO pins for the motor driver
const int motor1A = 4; // GPIO4 (Input 1 of Motor 1)
const int motor1B = 5; // GPIO5 (Input 2 of Motor 1)
const int motor2A = 14; // GPIO14 (Input 1 of Motor 2)
const int motor2B = 16; // GPIO16 (Input 2 of Motor 2)

// Variables to store the motor states
int motor1Speed = 255; // Speed of Motor 1 (0 to 255)
int motor2Speed = 255; // Speed of Motor 2 (0 to 255)
bool buttonPressed = false;

void setup() {
  // Initialize Serial for debugging
  Serial.begin(115200);

  // Set the pins as outputs for the motor driver
  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2A, OUTPUT);
  pinMode(motor2B, OUTPUT);

  // Create an access point
  WiFi.softAP(apSSID, apPassword);

  // Define the server routes
  server.on("/", HTTP_GET, handleRoot);
  server.on("/forward", HTTP_GET, handleForward);
  server.on("/backward", HTTP_GET, handleBackward);
  server.on("/left", HTTP_GET, handleLeft);
  server.on("/right", HTTP_GET, handleRight);
  server.on("/stop", HTTP_GET, handleStop);

  // Start the server
  server.begin();
}

void loop() {
  // Check button states and update motor states
  if (digitalRead(motor1A) == HIGH || digitalRead(motor1B) == HIGH ||
      digitalRead(motor2A) == HIGH || digitalRead(motor2B) == HIGH) {
    buttonPressed = true;
  } else {
    if (buttonPressed) {
      // If no button is pressed now but was pressed before, stop the motors
      digitalWrite(motor1A, LOW);
      digitalWrite(motor1B, LOW);
      digitalWrite(motor2A, LOW);
      digitalWrite(motor2B, LOW);
      buttonPressed = false;
    }
  }

  // Handle client requests
  server.handleClient();
}

void handleRoot() {
  // HTML page with buttons to control the tank
  String html = "<html><head>";
  html += "<style>";
  html += "body { text-align: center; background-color: #f2f2f2; }";
  html += "h1 { font-size: 36px; color: #333; }";
  html += ".button-container { display: flex; justify-content: center; align-items: center; }";
  html += ".direction-button {";
  html += "  width: 100px;";
  html += "  height: 100px;";
  html += "  font-size: 18px;";
  html += "  margin: 10px;";
  html += "  border-radius: 50%;";
  html += "  background-color: #3498db;";
  html += "  color: white;";
  html += "  border: none;";
  html += "  cursor: pointer;";
  html += "}";
  html += ".forward-button { margin-bottom: 20px; }"; // Adjust the margin for Forward
  html += ".backward-button { margin-top: 20px; }";    // Adjust the margin for Backward
  html += "button:hover { background-color: #2980b9; }";
  html += "</style>";
  html += "</head><body>";
  html += "<h1>TechsPassion Tank Control</h1>";
  html += "<div class='button-container'>";
  html += "  <button class='direction-button forward-button' onmousedown='startForward()' onmouseup='stop()'>Forward</button>";
  html += "</div>";
  html += "<div class='button-container'>";
  html += "  <button class='direction-button' onmousedown='startLeft()' onmouseup='stop()'>Left</button>";
  html += "  <button class='direction-button' onmousedown='stop()'>Stop</button>";
  html += "  <button class='direction-button' onmousedown='startRight()' onmouseup='stop()'>Right</button>";
  html += "</div>";
  html += "<div class='button-container'>";
  html += "  <button class='direction-button backward-button' onmousedown='startBackward()' onmouseup='stop()'>Backward</button>";
  html += "</div>";
  html += "<script>";
  html += "function startForward() {";
  html += "  var xhr = new XMLHttpRequest();";
  html += "  xhr.open('GET', '/forward', true);";
  html += "  xhr.send();";
  html += "}";
  html += "function startBackward() {";
  html += "  var xhr = new XMLHttpRequest();";
  html += "  xhr.open('GET', '/backward', true);";
  html += "  xhr.send();";
  html += "}";
  html += "function startLeft() {";
  html += "  var xhr = new XMLHttpRequest();";
  html += "  xhr.open('GET', '/left', true);";
  html += "  xhr.send();";
  html += "}";
  html += "function startRight() {";
  html += "  var xhr = new XMLHttpRequest();";
  html += "  xhr.open('GET', '/right', true);";
  html += "  xhr.send();";
  html += "}";
  html += "function stop() {";
  html += "  var xhr = new XMLHttpRequest();";
  html += "  xhr.open('GET', '/stop', true);";
  html += "  xhr.send();";
  html += "}";
  html += "</script>";
  html += "</body></html>";
  server.send(200, "text/html", html);
}

void handleForward() {
  digitalWrite(motor1A, HIGH);
  digitalWrite(motor1B, LOW);
  digitalWrite(motor2A, HIGH);
  digitalWrite(motor2B, LOW);
  server.send(200, "text/plain", "Forward");
}

void handleBackward() {
  digitalWrite(motor1A, LOW);
  digitalWrite(motor1B, HIGH);
  digitalWrite(motor2A, LOW);
  digitalWrite(motor2B, HIGH);
  server.send(200, "text/plain", "Backward");
}

void handleLeft() {
  digitalWrite(motor1A, LOW);
  digitalWrite(motor1B, HIGH);
  digitalWrite(motor2A, HIGH);
  digitalWrite(motor2B, LOW);
  server.send(200, "text/plain", "Left");
}

void handleRight() {
  digitalWrite(motor1A, HIGH);
  digitalWrite(motor1B, LOW);
  digitalWrite(motor2A, LOW);
  digitalWrite(motor2B, HIGH);
  server.send(200, "text/plain", "Right");
}

void handleStop() {
  // Stop all motors
  digitalWrite(motor1A, LOW);
  digitalWrite(motor1B, LOW);
  digitalWrite(motor2A, LOW);
  digitalWrite(motor2B, LOW);
  server.send(200, "text/plain", "Stop");
}


3D Printed Tank
All content and information on this web site is intended for educational and entertainment purposes only.