Hi all,
Can anyone tell me why this sketch keeps on rebooting when run? It compiles OK and when compiled it keeps rebooting. When the serial lead is removed and a 12V battery for the L298N is applied it still does nothing. I assume it still reboots. I initially had a brownout issue but I’ve included the few lines of code to eliminate that.
#include <Wire.h>
#include "PCF8575.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
// Define the custom I2C pins
#define I2C_SDA_PIN 21
#define I2C_SCL_PIN 22
// Initialize the PCF8575 at address 0x20
PCF8575 pcf8575(0x20);
// Motor A
int motor1Forward = 1;
int motor1Back = 0;
int EnableMoror1 = 8;
int motor2Forward = 3;
int motor2Back = 2;
int EnableMoror2 = 9;
int motor3Forward = 4;
int motor3Back = 5;
int EnableMoror3 = 10;
int motor4Forward = 6;
int motor4Back = 7;
int EnableMoror4 = 11;
//Set motor Speeds
int Motor1Speed = 0; // 0 (Off) to 255 (top speed)
int Motor2Speed = 0; // 0 (Off) to 255 (top speed)
int Motor3Speed = 0; // 0 (Off) to 255 (top speed)
int Motor4Speed = 0; // 0 (Off) to 255 (top speed)
// Setting PWM properties
const int freq = 30000;
const int pwmChannel0 = 0;
const int pwmChannel1 = 1;
const int pwmChannel2 = 2;
const int pwmChannel3 = 3;
const int resolution = 8;
int dutyCycle = 200;
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // Disable brownout detector
Serial.begin(115200);
delay(1000);
// testing
Serial.print("Testing DC Motor...");
Wire.end(); // End default I2C (optional but good practice)
Wire.setPins(I2C_SDA_PIN, I2C_SCL_PIN); // Set custom pins: SDA to GPIO 27, SCL to GPIO 22
Wire.begin();
delay(1000);
// sets the pins as outputs:
pcf8575.pinMode(motor1Forward, OUTPUT);
pcf8575.pinMode(motor1Back, OUTPUT);
pcf8575.pinMode(motor2Forward, OUTPUT);
pcf8575.pinMode(motor2Back, OUTPUT);
pcf8575.pinMode(motor3Forward, OUTPUT);
pcf8575.pinMode(motor3Back, OUTPUT);
pcf8575.pinMode(motor4Forward, OUTPUT);
pcf8575.pinMode(motor4Back, OUTPUT);
pcf8575.pinMode(EnableMoror1, OUTPUT);
pcf8575.pinMode(EnableMoror2, OUTPUT);
pcf8575.pinMode(EnableMoror3, OUTPUT);
pcf8575.pinMode(EnableMoror4, OUTPUT);
pcf8575.begin();
// Configure PWM for motor speed control using new API
ledcAttachChannel(EnableMoror1, freq, resolution, pwmChannel0);
ledcAttachChannel(EnableMoror2, freq, resolution, pwmChannel1);
ledcAttachChannel(EnableMoror3, freq, resolution, pwmChannel2);
ledcAttachChannel(EnableMoror4, freq, resolution, pwmChannel3);
// Set initial PWM duty cycle to 0 (motors off)
analogWrite(EnableMoror1, Motor1Speed);
analogWrite(EnableMoror2, Motor2Speed);
analogWrite(EnableMoror3, Motor3Speed);
analogWrite(EnableMoror4, Motor4Speed);
}
void loop() {
// Move the DC motor forward at maximum speed
Serial.println("Moving motor 1 Forward");
pcf8575.digitalWrite(motor1Forward, HIGH);
pcf8575.digitalWrite(motor1Back, LOW);
Motor1Speed = 100; //Set speed
analogWrite(EnableMoror1, Motor1Speed); //Run at Motor1Speed speed
delay(2000);
AllMotorsOff();
Serial.println("Moving motor 2 Forward");
pcf8575.digitalWrite(motor2Forward, HIGH);
pcf8575.digitalWrite(motor2Back, LOW);
Motor2Speed = 100; //Set speed
analogWrite(EnableMoror2, Motor2Speed); //Run at Motor2Speed speed
delay(2000);
AllMotorsOff();
Serial.println("Moving motor 3 Forward");
pcf8575.digitalWrite(motor3Forward, HIGH);
pcf8575.digitalWrite(motor3Back, LOW);
Motor3Speed = 100; //Set speed
analogWrite(EnableMoror3, Motor3Speed); //Run at Motor3Speed speed
delay(2000);
AllMotorsOff();
Serial.println("Moving motor 4 Forward");
pcf8575.digitalWrite(motor4Forward, HIGH);
pcf8575.digitalWrite(motor4Back, LOW);
Motor4Speed = 100; //Set speed
analogWrite(EnableMoror4, Motor4Speed); //Run at Motor3Speed speed
delay(2000);
AllMotorsOff();
}
void AllMotorsOff(){
pcf8575.digitalWrite(motor1Forward, LOW);
pcf8575.digitalWrite(motor1Back, LOW);
pcf8575.digitalWrite(motor2Forward, LOW);
pcf8575.digitalWrite(motor2Back, LOW);
pcf8575.digitalWrite(motor3Forward, LOW);
pcf8575.digitalWrite(motor3Back, LOW);
pcf8575.digitalWrite(motor4Forward, LOW);
pcf8575.digitalWrite(motor4Back, LOW);
}
ERROR:
rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4980
load:0x40078000,len:16612
load:0x40080400,len:3500
entry 0x400805b4
Testing DC Motor...ets Jul 29 2019 12:21:46
rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4980
load:0x40078000,len:16612
load:0x40080400,len:3500
entry 0x400805b4
It was working until I added the motor speed coding for each of the 4 motors. The way it is written is pretty long winded. Once working I’ll shorten it.