Hi everyone. I seem to be having an issue with a pin on the ESP32. Everything works as expected in the following code, except the green led stays on. All the Leds are run through transistors with 2k resistors betweeen the pin and the transistor. Do some pins leak voltage? It comes on as soon as the esp32 is started. Thanks in advance.
#include <EEPROM.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//SDA -> D21
//SCL -> D22
#define EEPROM_SIZE 1
// ***************************** Set ADC pin for battery health *****
const int Analog_channel_pin= 32;
//********************************Buttons ***************************************
const int buttn1 = 15;
const int buttn2 = 18;
const int buttn3 = 4;
const int buttn4 = 25;
const int buttn5 = 33;
const int buttn6 = 32;
//************************************* LEDS ************************************
const int LedRed = 13;
const int LedYellow = 12;
const int LedBlue = 26;
const int LedGreen = 27;
//*************************************Stepper motor controller pins ************************
const int sleepPin = 5;
const int stepPin = 17;
const int dirPin = 16;
//************************************************ MQTT logon ***********************
const char* brokerUser = "sfdab";
const char* brokerPass = "Sfdab0001";
const char* broker = "192.168.200.100";
const char* mqTopic = "Jumps";
int heightStored;
int j;
int k;
//***************************************** Variables for handling large number of pulses**********
unsigned long int i;
unsigned long int heightActual = 1000;
unsigned long int heightMove = 0;
unsigned long int stepActual = 0;
unsigned long int heightVal = 0;
unsigned long startMillis = millis();
unsigned long elapsedMillis;
int adcVal = 0;
float voltVal = 0;
int setCal;
int direction = 0;
int LedBlink = 750;
int stepPulseTime = 575;
int buttnCalVal;
int upBttn;
LiquidCrystal_I2C lcd(0x27,16,2); // Set LCD screen parameters
WiFiClient espClient;
PubSubClient client(espClient);
//********************************************************************************************************************************************************************************
void setup()
{
pinMode(buttn1, INPUT_PULLUP);
pinMode(buttn2, INPUT_PULLUP);
pinMode(buttn3, INPUT_PULLUP);
pinMode(buttn4, INPUT_PULLUP);
pinMode(buttn5, INPUT_PULLUP);
pinMode(buttn6, INPUT_PULLUP);
pinMode(LedRed, OUTPUT);
pinMode(LedYellow, OUTPUT);
pinMode(LedBlue, OUTPUT);
pinMode(LedGreen, OUTPUT);
pinMode(sleepPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
//*********************************get previous stored value from non volatile memory *********
EEPROM.begin(EEPROM_SIZE);
//******************************** Set stepper driver pins *******************
digitalWrite(stepPin,0);
digitalWrite(dirPin,0);
digitalWrite(sleepPin, LOW); // only enable when ready to move to save power
//***************************** System 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 WiFi and LCD Screen ***********************************************
lcd.begin();
WiFi.disconnect();
WiFi.setHostname("Jump_1"); // XXXXXXXXXXX Set for each esp32 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
delay(100);
// *********************************************************
// ******************************* detect button push for calibration
elapsedMillis = millis();
while(elapsedMillis < 8000)
{
lcd.setCursor(2, 0);
lcd.print("Calibrate ?");
setCal = digitalRead(buttn1);
if(setCal == 0)
{
digitalWrite(LedRed, HIGH);
delay(2000);
digitalWrite(sleepPin, HIGH);
calibrate();
}
elapsedMillis = millis();
digitalWrite(LedRed, LOW);
}
digitalWrite(LedRed, LOW);
digitalWrite(sleepPin, LOW);
lcd.clear();
adcVal = analogRead(Analog_channel_pin); // read voltage on analog pin
lcd.setCursor(0, 0);
lcd.print("Battery Voltage");
voltVal = (adcVal * 17.1) / (4095); // get battery voltage
lcd.setCursor(5, 1);
//lcd.print(adcVal); // get ADC Value
lcd.print(voltVal); // get voltage value
delay(1000);
lcd.clear();
//************************************* Turn on WiFi **********************
lcd.setCursor(5, 0);
lcd.print("START");
WiFi.begin("SFDAB Agility","Sfdab0001");
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();
digitalWrite(LedGreen, LOW);
client.setServer(broker,1883);
client.setCallback(callback);
}
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("jump1", brokerUser, brokerPass))
{
lcd.setCursor(4, 1);
lcd.print("CONNECTED");
client.subscribe(mqTopic);
digitalWrite(LedBlue, HIGH); //Turn on LED to signify online
}
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
//***************** check previous position when shut down ***********
heightStored = EEPROM.read(0);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(heightStored); //Should be "MESSAGE" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
lcd.setCursor(4, 1);
//***************************************************** Set pulses to get correct height
lcd.print(heightVal);
if (heightVal == 1) {heightMove = 2000;}
if (heightVal == 2) {heightMove = 12250;}
if (heightVal == 3) {heightMove = 22500;}
if (heightVal == 4) {heightMove = 32750;}
if (heightVal == 5) {heightMove = 43000;}
if (heightStored == 1) {heightActual = 2000;}
if (heightStored == 2) {heightActual = 12250;}
if (heightStored == 3) {heightActual = 22500;}
if (heightStored == 4) {heightActual = 32750;}
if (heightStored == 5) {heightActual = 43000;}
if (heightMove > heightActual) // if its higher, go up
{
direction = 0;
stepActual = (heightMove - heightActual); // calculate how many steps to move between actual height of jump and new height
}
else
{
direction = 1; // if lower, go down
stepActual = (heightActual - heightMove); // calculate how many steps to move between actual height of jump and new height
}
if (heightMove == heightActual) // check if same as last height
{
loop(); // No movement. Back to loop
}
digitalWrite(sleepPin, HIGH); // only enable when ready to move to save power
delay(100);
digitalWrite(dirPin, direction);
for(i=1;i<stepActual;i++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepPulseTime);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepPulseTime);
}
digitalWrite(sleepPin, LOW); // only enable when ready to move to save power
heightActual = heightMove;
EEPROM.write(0, heightVal); // change value in non volatile memory for shutdown
EEPROM.commit();
//*********************************************** Battery check *******************************
delay(100);
adcVal = analogRead(Analog_channel_pin);
if(adcVal > 2400)
{
digitalWrite(LedGreen, HIGH);
digitalWrite(LedYellow, LOW);
digitalWrite(LedRed, LOW);
}
else if(adcVal <= 2399 and adcVal > 1900)
{
digitalWrite(LedGreen, LOW);
digitalWrite(LedYellow, HIGH);
digitalWrite(LedRed, LOW);
}
else
{
digitalWrite(LedGreen, LOW);
digitalWrite(LedYellow, LOW);
digitalWrite(LedRed, HIGH);
}
}
//***********************************************************************************************************
void calibrate()
{
lcd.setCursor(1, 1);
lcd.print("Calibrating..");
while (digitalRead(buttn1) == 1)
{
if (digitalRead(buttn2) == 0) // move up one pulse
{
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepPulseTime);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepPulseTime);
}
if (digitalRead(buttn3) == 0) // move down one pulse
{
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepPulseTime);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepPulseTime);
}
EEPROM.write(0, 1); // change value in non volatile memory for shutdown
EEPROM.commit();
heightVal = 1;
}
}