Skip to content

Instantly share code, notes, and snippets.

@markiv25
Created March 4, 2018 16:43
Show Gist options
  • Save markiv25/c975fab317b14b50ccf61d9e28e0db4d to your computer and use it in GitHub Desktop.
Save markiv25/c975fab317b14b50ccf61d9e28e0db4d to your computer and use it in GitHub Desktop.
//TANKS MARK 2.0
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include<Servo.h>
Servo myservo;
#define m1 D0
#define m2 D1
#define m3 D2
#define m4 D3
#define b1 D6
#define H D4
#define V D5
Servo HORZ;
Servo VERT;
const char* ssid = "ByteCamp-3"; //Enter your wifi network SSID
const char* password ="Qwerty1!"; //Enter your wifi network password
const int SERVER_PORT = 1111;
const int BAUD_RATE = 115200;
byte incomingByte = 0;
bool forwardsPressed = false;
bool backwardsPressed = false;
bool rightPressed = false;
bool leftPressed = false;
bool forwardLeftPressed = false;
bool forwardRightPressed = false;
bool backwardLeftPressed = false;
bool backwardRightPressed = false;
// media edit
bool horzServo = false;
bool vertServo = false;
//
bool buttonPressed = false;
const int FORWARDS_PRESSED = 49; //1
const int FORWARDS_RELEASED = 50; //2
const int BACKWARDS_PRESSED = 51; //3
const int BACKWARDS_RELEASED = 52; //4
const int RIGHT_PRESSED = 55; //5
const int RIGHT_RELEASED = 56; //6
const int LEFT_PRESSED = 53; //7
const int LEFT_RELEASED = 54; //8
const int FORWARD_LEFT_PRESSED = 59; //9
const int FORWARD_LEFT_RELEASED = 60; //10
const int FORWARD_RIGHT_PRESSED = 57; //11
const int FORWARD_RIGHT_RELEASED = 58; //12
const int BACKWARD_LEFT_PRESSED = 63; //13
const int BACKWARD_LEFT_RELEASED = 64; //14
const int BACKWARD_RIGHT_PRESSED = 61; //15
const int BACKWARD_RIGHT_RELEASED = 62; //16
const int BUTTON_PRESSED = 9;
const char BUTTON_RELEASED = 0;
//Media Edit
const int HORZ_SERVO_PRESSED = ;
const int HORZ_SERVO_RELEASED = ;
const int VERT_SERVO_PRESSED = ;
const int VERT_SERVO_RELEASED = ;
//
int flagH == 0;
int flagV == 0;
//TP LOOP
if(incomingByte == HORZ_SERVO_PRESSED && flagH==0 )
{ int Hdeg == incoming Byte;
flagH==0;
}
if else (incomingByte == HORZ_SERVO_RELEASED )
{
flagH==1;
}
//VErtical
if(incomingByte == VERT_SERVO_PRESSED && flagV==0 )
{ int Vdeg == incoming Byte;
flagV==0;
}
if else (incomingByte == VERT_SERVO_RELEASED )
{
flagV==1;
}
//
byte packetBuffer[1048];
WiFiUDP Udp;
IPAddress ip;
void initOutputs() {
pinMode(m1,OUTPUT);
pinMode(m2,OUTPUT);
pinMode(m3,OUTPUT);
pinMode(m4,OUTPUT);
//Media Edit
Horz.attach(D4);
VERT.attach(D5);
//
pinMode(b1,OUTPUT);
digitalWrite(b1,LOW);
}
void connectWifi() {
Serial.println();
Serial.println();
Serial.print("Connecting to WIFI network");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Udp.begin(SERVER_PORT);
ip = WiFi.localIP();
Serial.println(ip);
}
void moveForwards() {
Serial.println("Forward");
analogWrite(m1,1048);
analogWrite(m2,0);
analogWrite(m3,1048);
analogWrite(m4,0);
}
void moveBackwards() {
Serial.println("Backwards");
analogWrite(m1,0);
analogWrite(m2,1048);
analogWrite(m3,0);
analogWrite(m4,1048);
}
void turnRight() {
Serial.println("Hard Right");
analogWrite(m1,1048);
analogWrite(m2,0);
analogWrite(m3,0);
analogWrite(m4,1048);
}
void turnLeft() {
Serial.println("Hard Left");
analogWrite(m1,0);
analogWrite(m2,1048);
analogWrite(m3,1048);
analogWrite(m4,0);
}
void softLeftF()
{
Serial.println("Soft Left F");
analogWrite(m1,0);
analogWrite(m2,0);
analogWrite(m3,1048);
analogWrite(m4,0);
}
void softLeftB()
{
Serial.println("Soft left B");
analogWrite(m1,0);
analogWrite(m2,0);
analogWrite(m3,0);
analogWrite(m4,1048);
}
void softRightF()
{
Serial.println("Soft Right F");
analogWrite(m1,1048);
analogWrite(m2,0);
analogWrite(m3,0);
analogWrite(m4,0);
}
void softRightB()
{
Serial.println("Soft Right B");
analogWrite(m1,0);
analogWrite(m2,1048);
analogWrite(m3,0);
analogWrite(m4,0);
}
void gunControl() {
Serial.println("Button On");
digitalWrite(b1,HIGH);
}
void gunReset(){
Serial.println("Button Off");
digitalWrite(b1,LOW);
}
void resetSteering() {
Serial.println("reset s");
}
void resetEngine() {
Serial.println("reset e");
analogWrite(m1,0);
analogWrite(m2,0);
analogWrite(m3,0);
analogWrite(m4,0);
}
//Media EDit
void Horz()
{
Horz.write(Hdeg);
}
void Vert()
{
Vert.write(Vdeg);
}
//
void detectKeyPresses() {
if (incomingByte == FORWARDS_PRESSED) {
forwardsPressed = true;
}
else if (incomingByte == BACKWARDS_PRESSED) {
backwardsPressed = true;
}
if (incomingByte == FORWARDS_RELEASED) {
forwardsPressed = false;
}
else if (incomingByte == BACKWARDS_RELEASED) {
backwardsPressed = false;
}
if (incomingByte == RIGHT_PRESSED) {
rightPressed = true;
}
else if (incomingByte == LEFT_PRESSED) {
leftPressed = true;
}
if (incomingByte == RIGHT_RELEASED) {
rightPressed = false;
}
else if (incomingByte == LEFT_RELEASED) {
leftPressed = false;
}
else if(incomingByte == FORWARD_LEFT_PRESSED) {
forwardLeftPressed = true;
}
else if(incomingByte == FORWARD_LEFT_RELEASED) {
forwardLeftPressed = false;
}
else if(incomingByte == BACKWARD_LEFT_PRESSED) {
backwardLeftPressed = true;
}
else if(incomingByte == BACKWARD_LEFT_RELEASED) {
backwardLeftPressed = false;
}
else if(incomingByte == FORWARD_RIGHT_PRESSED) {
forwardRightPressed = true;
}
else if(incomingByte == FORWARD_RIGHT_RELEASED) {
forwardRightPressed = false;
}
else if(incomingByte == BACKWARD_RIGHT_PRESSED) {
backwardRightPressed = true;
}
else if(incomingByte == BACKWARD_RIGHT_RELEASED) {
backwardRightPressed = false;
}
//Media Edit
if( incomingByte == VERT_SERVO_PRESSED)
{
vert = true;
}
else if(incomingByte == VERT_SERVO_RELEASED) {
vert = false;
}
if( incomingByte == HORZ_SERVO_PRESSED)
{
horz = true;
}
else if(incomingByte == HORZ_SERVO_RELEASED) {
horz = false;
}
//
if (incomingByte == BUTTON_PRESSED) {
buttonPressed = true;
}
else if (incomingByte == BUTTON_RELEASED) {
buttonPressed = false;
}
}
void handlePinOutputs() {
if (forwardsPressed == true)
{
moveForwards();
}
else if (backwardsPressed == true)
{
moveBackwards();
}
else if(rightPressed == true)
{
turnRight();
}
else if(leftPressed == true)
{
turnLeft();
}
else if(forwardLeftPressed == true)
{
softLeftF();
}
else if(backwardLeftPressed == true)
{
softLeftB();
}
else if(forwardRightPressed == true)
{
softRightF();
}
else if(backwardRightPressed == true)
{
softRightB();
}
else {
resetEngine();
}
//Media EDit
if( vert == true)
{
Vert();
}
if( horz == true)
{
Horz();
}
//
if(buttonPressed == true)
{
gunControl();
}
else if(buttonPressed == false)
{
gunReset();
}
}
void setup() {
Serial.begin(BAUD_RATE);
delay(10);
initOutputs();
connectWifi();
}
void loop() {
int noBytes = Udp.parsePacket();
String received_command = "";
if ( noBytes )
{
Serial.print(millis() / 1000);
Serial.print(":Packet of ");
Serial.print(noBytes);
Serial.print(" received from ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
Udp.read(packetBuffer,noBytes);
Serial.println();
if(noBytes >1 ) {
incomingByte = 0;
Serial.println("Greater than 1..");
//Serial.println(noBytes);
for(int i=0;i<noBytes ; i++) {
int flag;
if(i == 0)
flag = 10;
else
flag = 1;
incomingByte = incomingByte + ((packetBuffer[i] - 48)*flag);
}
incomingByte = incomingByte + 48;
Serial.println(incomingByte);
Serial.println();
} else {
Serial.println(packetBuffer[0]);
incomingByte = packetBuffer[0];
Serial.println();
}
detectKeyPresses();
handlePinOutputs();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment