Hi, im doing a project where i'm using the data from a soil moisture to turn a stepper motor into 3 different positions. This is my first project and i'm kinda trash at coding. I coded made everything working on an Arduino Uno and now i need to transfer the code to an Esp32. I am well aware that the pins are different and i have changed them acordingly. But now my stepper motor wont work. I have tested if it was a power issue and it is now. so it have something to do with the pins and i have no clue why it is not working lmao.
here is the code, please ignore any weird parts of the code. I only need help with the stepper motor
#include <Stepper.h>
#include <EEPROM.h>
const int stepsPerRevolution = 2048;
int location = 0;
/* Change these values based on your calibration values */
#define soilWet 600 // Define max value we consider soil 'wet'
#define soilDry 800
#define soilTooDry 900 // Define min value we consider soil 'dry'
// Sensor pins
//#define sensorPower 7
#define sensorPin 26
Stepper motor(stepsPerRevolution, 35, 33, 32, 25);
void setup() {
//pinMode(sensorPower, OUTPUT);
motor.setSpeed(5);
// Initially keep the sensor OFF
// digitalWrite(sensorPower, LOW);
Serial.begin(9600);
pinMode(14, OUTPUT);
setupLocation(); // Initialize location from EEPROM
}
void loop() {
//get the reading from the function below and print it
int moisture = readSensor();
Serial.print("Analog Output: ");
Serial.println(moisture);
if (moisture < soilWet) {
Serial.println("Status: Soil is too wet");
if (location != 0){
Serial.end();
if (location == 1){
//digitalWrite(sensorPower, LOW);
updateLocation(0);
motor.step(-(stepsPerRevolution / 3));
}
if (location == 2){
//digitalWrite(sensorPower, LOW);
updateLocation(0);
motor.step((stepsPerRevolution / 3));
}
Serial.begin(9600);
}
while (moisture < soilWet) {
digitalWrite(14, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(14, LOW); // turn the LED off by making the voltage LOW
delay(1000);
int moisture = readSensor();
if (moisture > soilWet){
break;
}
}
}
if (moisture > soilTooDry) {
Serial.println("Status: Soil is very dry - time to water!");
if (location != 0){
Serial.end();
if (location == 1){
//digitalWrite(sensorPower, LOW);
updateLocation(0);
motor.step(-(stepsPerRevolution / 3));
}
if (location == 2){
// digitalWrite(sensorPower, LOW);
updateLocation(0);
motor.step((stepsPerRevolution / 3));
}
Serial.begin(9600);
}
}
else if (moisture >= soilWet && moisture < soilDry) {
Serial.println("Status: Soil moisture is perfect");
if (location != 1){
Serial.end();
if (location == 0){
//digitalWrite(sensorPower, LOW);
updateLocation(1);
motor.step(stepsPerRevolution / 3);
}
if (location == 2){
//digitalWrite(sensorPower, LOW);
updateLocation(1);
motor.step(-(stepsPerRevolution / 3));
}
Serial.begin(9600);
}
}
else {
Serial.println("Status: Soil is dry - time to water!");
if (location != 2){
Serial.end();
if (location == 0){
//digitalWrite(sensorPower, LOW);
updateLocation(2);
motor.step(-(stepsPerRevolution / 3));
}
if (location == 1){
// digitalWrite(sensorPower, LOW);
updateLocation(2);
motor.step(stepsPerRevolution / 3);
}
Serial.begin(9600);
}
}
delay(5000);
// Normally you should take reading perhaps once or twice a day
Serial.println();
}
// This function returns the analog soil moisture measurement
int readSensor() {
//digitalWrite(sensorPower, HIGH); // Turn the sensor ON
delay(10); // Allow power to settle
int val = analogRead(sensorPin); // Read the analog value form sensor
//digitalWrite(sensorPower, LOW); // Turn the sensor OFF
return val; // Return analog moisture value
}
void updateLocation(int newLocation) {
EEPROM.write(0, newLocation);
location = newLocation;
}
void setupLocation() {
int storedLocation = EEPROM.read(0);
if (storedLocation >= 0 && storedLocation <= 2) {
location = storedLocation;
}
}