Iâm still not sure why no one can admit that the new Joystick layout is completely different. If it was set up and worked the same, I wouldnât be having these issues, but since everyone seems to think that there will be a miraculous realization with the posting of the code, here it is:
/* JackCarterâs RC Comet Tank /
/ Created by Lucas âJack Carterâ Fierfort /
/ November 2019 */
/* Code made for a NodeMCU and using the Blynk app */
/* Thingiverse page for the plans : Lasercut RC Comet Tank by JackCarter - Thingiverse */
/* You will have to set your own Wi-Fi SSID / password and copy/paste your Blynk authentification token before compiling */
#define MAX_RPM 15
#define BLYNK_PRINT Serial
/The âStepper.hâ library may work instead of the âStepper2.hâ, but the Motor.setDirection() functions will no more be compatible/
//#include <Stepper.h>
#include <Stepper2.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_TEMPLATE_ID âTMPL4ROyKpIQâ
#define BLYNK_DEVICE_NAME âQuickstart Templateâ
#define BLYNK_AUTH_TOKEN âcULXmIvjhwYTvJBoJtVRUB9oIGuWjFa7â
int Right_Motor_pinOut[4] = {D1,D2,D3,D4};
int Left_Motor_pinOut[4] = {D5,D6,D7,D8};
Stepper2 Motor_Left(Left_Motor_pinOut);
Stepper2 Motor_Right(Right_Motor_pinOut);
int Actual_RightSpeed;
int Actual_LeftSpeed;
int Command_RightTrack;
int Command_LeftTrack;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = âXaz3rdkOIFnEZ7GSAsxMOHtz4I-M_V73â;
//char auth[] = â93CyRP7NMcUXpL7U4FA3W3ZVTgjunC1yâ;
// Your WiFi credentials.
// Set password to ââ for open networks.
char ssid[] = âXXXXXxâ;
char pass[] = âXXXXXXXXXXâ;
void setup()
{
Actual_RightSpeed = 0;
Actual_LeftSpeed = 0;
Command_RightTrack = 0;
Command_LeftTrack = 0;
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, âblynk-cloud.comâ, 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
//pinMode(LED_BUILTIN,OUTPUT);
}
void loop()
{
Blynk.run();
/The actual speed of the motors slowly increase/decrease to the command/
if(abs(Actual_LeftSpeed) < abs(Command_LeftTrack) ) Actual_LeftSpeed++;
if(abs(Actual_LeftSpeed) > abs(Command_LeftTrack) ) Actual_LeftSpeedâ;
if(abs(Actual_RightSpeed) < abs(Command_RightTrack) ) Actual_RightSpeed++;
if(abs(Actual_RightSpeed) > abs(Command_RightTrack) ) Actual_RightSpeedâ;
Serial.print(Actual_LeftSpeed);
Serial.print("\t\t");
Serial.println(Actual_RightSpeed);
/Setting the right direction according to the sign of the speed command/
if(Command_LeftTrack < 0) Motor_Left.setDirection(0);
if(Command_LeftTrack > 0) Motor_Left.setDirection(1);
if(Command_RightTrack < 0) Motor_Right.setDirection(1);
if(Command_RightTrack > 0) Motor_Right.setDirection(0);
if(Command_LeftTrack != 0 || Command_RightTrack != 0) {
Motor_Left.setSpeed(Actual_LeftSpeed);
Motor_Right.setSpeed(Actual_RightSpeed);
/*The two motors turn a step once one after the others, as two stepper can't turn simultaneously*/
for(int i=0 ; i<15 ; i++) {
Motor_Left.step(1);
Motor_Right.step(1);
}
}
}
/Everytime the joystick changes/
BLYNK_WRITE(V0) {
/Get the Blynk joystick values/
int Joystick_x = param[0].asInt();
int Joystick_y = param[1].asInt();
Serial.print(Joystick_x);
Serial.print("\t");
Serial.println(Joystick_y);
/Absolute track speed command. Tracks will actually accelerate until they reach this value/
Command_RightTrack = Joystick_y - Joystick_x ;
Command_LeftTrack = Joystick_y + Joystick_x ;
/The speed commands are capped/
if(Command_RightTrack < -MAX_RPM ) Command_RightTrack = -MAX_RPM ;
if(Command_RightTrack > MAX_RPM ) Command_RightTrack = MAX_RPM ;
if(Command_LeftTrack < -MAX_RPM ) Command_LeftTrack = -MAX_RPM ;
if(Command_LeftTrack > MAX_RPM ) Command_LeftTrack = MAX_RPM ;
}
Gary