// เผื่อใครใจร้อนอยากลองก่อนครับ ^_^
//Code by AndronoIDE
// I will explain in detail later ^_^
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "Your WiFi SSID";
const char* password = "Your WiFi Password";
const char* host = "IP your computer";
char message[255];
unsigned int localPort = 8080;
WiFiUDP Udp;
// Pin layout is NodeMCU V.2
int pin1_motor_R = 1; //gpio01 --> IN1 motor controller Right Motor
int pin2_motor_R = 2; //gpio02 --> IN2 motor controller Right Motor
int pin1_motor_L = 3; //gpio03 --> IN3 motor controller Left Motor
int pin2_motor_L = 4; //gpio04 --> IN4 motor controller Left Motor
// Direction
int FW=0,BK=1,ST=2;
int dir=FW;
// Turn Status
int t=0;
int TL=0, TR=1;
int tdir=0;
int tcount=0;
void setup() {
Serial.begin(115200);
pinMode(pin1_motor_R , OUTPUT);
pinMode(pin2_motor_R, OUTPUT);
pinMode(pin1_motor_L, OUTPUT);
pinMode(pin2_motor_L, OUTPUT);
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());
Udp.begin(localPort);
Stop();
Serial.println("Setup done");
delay(2000);
}
void loop(){
int packetSize = Udp.parsePacket();
if (packetSize)
{
IPAddress remoteIp = Udp.remoteIP();
int len = Udp.read(message, 255);
Serial.println(message);
checkcommand();
delay(5);
}
}
void forward()
{
Serial.println("forward");
dir=FW;
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void back()
{
Serial.println("back");
dir=BK;
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, HIGH);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, HIGH);
}
void left()
{
Serial.println("left");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void right()
{
Serial.println("Right");
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, LOW);
}
void turnR()
{
Serial.println("Turn0");
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, HIGH);
}
void turnL()
{
Serial.println("Turn1");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, HIGH);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void Stop()
{
Serial.println("Stop");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, LOW);
}
void checkcommand()
{
if (*message=='f')
{
forward();
}else if (*message=='t')
{
if(tdir==TL){
turnR();
}else{
turnL();
}
delay(50);
Stop();
}
else if (*message=='b')
{
back();
}
else if (*message=='r')
{
tdir=TR;
right();
delay(200);
if(dir==FW){
forward();
}else if(dir==BK){
back();
}else{
tcount++;
if(tcount!=5){
Stop();
}else{
turnR();
tcount=0;
}
}
}
else if (*message=='l')
{
tdir=TL;
left();
delay(200);
if(dir==FW){
forward();
}else if(dir==BK){
back();
}else{
tcount++;
if(tcount!=5){
Stop();
}else{
turnL();
tcount=0;
}
}
}
else
{
dir=ST;
tcount=0;
Stop();
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น