Hi everyone. I have previously designed some agility jumps that use stepper motors to move the crossbar to the correct height using MQTT from my phone. That code works great, but I have some earlier prototype jumps that used a different system. They worked on a voltage divider using a 10 turn pot, geared to the motor that turns the screw, which moves the crossbar. The motor is controlled by a MAX14870 driver board. To debug while getting it working, I used an OLED screen so I could see what processes have actually worked Everything works fine up until the bar is supposed to move. The screen shows everything going as expected until the motor is supposed to move. Here is the code:
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//SDA -> D21
//SCL -> D22
//************************************** Set ADC pin for battery health and height actual ***************
const int BattCheckPin= 35;
const int ResPotPin= 34;
//****************************************** LEDS **********************
const int LedRed = 13;
const int LedYellow = 12;
const int LedBlue = 27;
const int LedGreen = 14;
//****************************************** DC motor controller pins *************************
const int MovePin = 17;
const int DirPin = 16;
//****************************************** MQTT logon *************************
const char* brokerUser = "Jump20"; //<<<< change this line and 32,77 and 142 >>>>
const char* brokerPass = "XXXXXXX";
const char* broker = "192.168.200.100";
const char* mqTopic = "Jumps";
//***************************************** Variables for handling height movement *************************
unsigned long int i;
unsigned long int heightActual = 0;
unsigned long int heightMove = 0;
unsigned long int heightVal = 0;
//***************************************** Variables for initial voltage check *************************
int adcVal = 0;
float voltVal = 0;
int LedBlink = 750;
//***************************************** Set LCD screen parameters *************************
LiquidCrystal_I2C lcd(0x27,16,2); //
WiFiClient espClient;
PubSubClient client(espClient);
//***********************************************************************************************************
void setup()
{
pinMode(LedRed, OUTPUT);
pinMode(LedYellow, OUTPUT);
pinMode(LedBlue, OUTPUT);
pinMode(LedGreen, OUTPUT);
//************************************* Set stepper driver pins ***********************************
pinMode(MovePin, OUTPUT);
pinMode(DirPin, OUTPUT);
digitalWrite(MovePin,0);
digitalWrite(DirPin,0);
//************************************* System LED check ***********************************
digitalWrite(LedBlue, HIGH);
delay(LedBlink);
digitalWrite(LedBlue,LOW);
digitalWrite(LedGreen, HIGH);
delay(LedBlink);
digitalWrite(LedGreen,LOW);
digitalWrite(LedYellow, HIGH);
delay(LedBlink);
digitalWrite(LedYellow,LOW);
digitalWrite(LedRed, HIGH);
delay(LedBlink);
digitalWrite(LedRed,LOW);
//************************************* Start LCD Screen ***********************************
lcd.begin();
delay(100);
//************************************* Battery check ************************************
digitalWrite(LedRed, LOW);
lcd.clear();
adcVal = analogRead(BattCheckPin);
lcd.setCursor(0, 0);
lcd.print("Battery Voltage");
voltVal = (adcVal * 12.73) / (4095);
lcd.setCursor(5, 1);
lcd.print(voltVal); // Get voltage value
delay(1000);
lcd.clear();
//*********************************** Turn on WiFi *********************************
lcd.setCursor(5, 0);
lcd.print("START");
WiFi.disconnect();
WiFi.setHostname("Jump_20"); // XXXXXXX Set for each esp32 XXXXXXX
WiFi.begin("SFDAB Agility","XXXXXXXXXXX");
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
}
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("CONNECTED");
lcd.setCursor(4, 1);
lcd.print("TO WIFI");
delay(1000);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("YOUR IP IS:");
lcd.setCursor(1, 1);
lcd.print((WiFi.localIP()));
delay(1000);
lcd.clear();
client.setServer(broker,1883);
client.setCallback(callback);
battCheck ();
}
void loop() //************************* Main loop to run MQTTsever *****************************
{
if (!client.connected())
{
reconnect();
}
client.loop();
}
void reconnect() // ****************** MQTT initialisation *****************************
{
while(!client.connected())
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("CONNECT TO MQTT");
if(client.connect("jump20", brokerUser, brokerPass))
{
lcd.setCursor(4, 1);
lcd.print("CONNECTED");
client.subscribe(mqTopic);
digitalWrite(LedBlue, HIGH);
}
else
{
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("CONNECTING...");
digitalWrite(LedBlue, LOW);
delay(1000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
payload[length] = '\0'; // Make payload a string by NULL terminating it.
int heightVal = atoi((char *)payload); //convert to variable
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(heightVal);
//***************************************************** Set pulses to get correct height
if (heightVal == 1) {heightMove = 200;}
if (heightVal == 2) {heightMove = 1000;}
if (heightVal == 3) {heightMove = 2000;}
if (heightVal == 4) {heightMove = 3000;}
if (heightVal == 5) {heightMove = 3900;}
lcd.setCursor(4, 1);
lcd.print(heightMove);
heightActual = analogRead(ResPotPin);
if (heightMove > heightActual) // if its higher, go up
{
digitalWrite(DirPin,HIGH);
while (heightMove < heightActual)
{
heightActual = analogRead(ResPotPin);
digitalWrite(MovePin,HIGH);
}
}
if (heightMove < heightActual)
{
digitalWrite(DirPin,LOW);
while (heightMove > heightActual)
{
heightActual = analogRead(ResPotPin);
digitalWrite(MovePin,HIGH);
}
}
else (heightMove == heightActual); // check if same as last height
{
loop(); // No movement. Back to loop
}
digitalWrite(MovePin,LOW);
battCheck ();
}
//*********************************************** Battery check *******************************
void battCheck ()
{
delay(100);
adcVal = analogRead(BattCheckPin);
if(adcVal > 3350)
{
digitalWrite(LedGreen, HIGH);
digitalWrite(LedYellow, LOW);
digitalWrite(LedRed, LOW);
}
else if(adcVal <= 3349 and adcVal > 3000)
{
digitalWrite(LedGreen, LOW);
digitalWrite(LedYellow, HIGH);
digitalWrite(LedRed, LOW);
}
else
{
digitalWrite(LedGreen, LOW);
digitalWrite(LedYellow, LOW);
digitalWrite(LedRed, HIGH);
}
}
Can anyone see what I havent done properly?
Thanks in advance.
Marz.