hello
im new to arduino coding and i cant get this code to work.
can anyone see why on the loop scop the string "command" isnt declared when i try to use it in the if function.
//#include <WiFi.h>
#include<ESP8266WiFi.h>
#include <WiFiUDP.h>
int WDT_millis = 0;
// WiFi ---------------------------
char *ptr = NULL;
char packetBuffer[255];
WiFiUDP udp;
#define v1 16
#define v2 2
#define v3 12
#define v4 14
void setup() {
Serial.begin(115200);
Serial.setTimeout(5);
WiFi.begin("GL-MT300N-V2-b51", "goodlife");
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print(".");
}
IPAddress ip(192, 168, 8, 3); // the desired IP Address
IPAddress gateway(192, 168, 8, 1); // the router IP
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
WiFi.config(ip, gateway, subnet); // configuring to the desired local IP
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //prints my ip
udp.begin(8888);
WDT_millis = millis();
}
void loop() {
int packetSize = udp.parsePacket();
if (packetSize) {
int len = udp.read(packetBuffer, 255);
ptr = strtok(packetBuffer, ","); // takes a list of delimiters
// this is for 2 variables. repeat for the needed number.
String command;
command = ptr;
Serial.println("command=");
Serial.println(command);
ptr = strtok(NULL, ","); // takes a list of delimiters
float vel = atof(ptr);
Serial.println("vel=");
Serial.println(vel);
WDT_millis = millis();
}
if (command=="1") {
analogWrite(v1,175);
analogWrite(v2,0);
analogWrite(v3,175);
analogWrite(v4,0);
WDT_millis = millis();
}
if (command=="0") {
analogWrite(v1,0);
analogWrite(v2,0);
analogWrite(v3,0);
analogWrite(v4,0);
WDT_millis = millis();
}
if (command=="2") {
analogWrite(v1,0);
analogWrite(v2,175);
analogWrite(v3,0);
analogWrite(v4,175);
WDT_millis = millis();
}
if (millis() - WDT_millis > 1000)
{
analogWrite(v1,0);
analogWrite(v2,0);
analogWrite(v3,0);
analogWrite(v4,0);
while (Serial.available()) {
Serial.read();
WDT_millis = millis();
}
delay(10);
}
}
