I find myself puzzling over a project that just; well, isn’t working. Or at least it works sporadically as and when it feels like it. I have two 12v DC motors hooked up to an L298N (powered by 12v DC Supply). Its 5v in/out is then powering the Arduino and two micro servos. The speed of the motors is set by a 10k Potentiometer, and the servos controlled by a thumbstick.
Individually the two systems seem to function well, the potentiometer controls the motors, the thumbstick the servos, without any issue, but when I use a sketch that has both systems in place they all seem to behave with lives of their own, the potentiometer seems to control the motors but they don't come on together, the servos sit and chatter to themselves without any input from the thumbstick, until the potentiometer is turned up to full, then suddenly everything seems to behave perfectly fine. Motors and servos running together, until then the Arduino will reset and do so more or less continuously.
At first I thought this might be a current issue, so I upped it from 2A to 5A, but that hasn’t done anything to help at all and I can’t see any reason why the code would be doing this.
I have also tried isolating the servos and Arduino from the 12v and giving them their own 5v power source instead, with no change, and even powering the Arduino, servos and motors all separately, still with no luck
It has been suggested that perhaps there is a short somewhere, the potentiometer might need to be a higher resistance and even that the breadboard may not be able to supply enough current, and that I should just wire it up and see if it works then. So I'd love some other input is there something obvious I should be doing first that I haven't?
Any insight would be greatly appreciated.
#include "Servo.h"
//DC MOTOR PINS
#define enA 9
#define enB 8
#define in1 5
#define in2 4
#define in3 3
#define in4 2
//THUMB STICK
const int thumbX = A1;
const int thumbY = A2;
//const int thumbSW = 12;
//MICRO SERVOS
int left = 11;
int right = 10;
Servo leftServo;
Servo rightServo;
const int MaxReadings = 10;
int Xreadings[MaxReadings];
int XreadIndex = 0;
int Xtotal = 0;
int X_Pos = 0;
int Yreadings[MaxReadings];
int YreadIndex = 0;
int Ytotal = 0;
int Y_Pos = 0;
int Servo_Pos = 0, Pos = 0;
void Smoother(int x_pin, int y_pin)
{
Xtotal = Xtotal - Xreadings[XreadIndex];
Ytotal = Ytotal - Yreadings[YreadIndex];
delay(1);
Xreadings[XreadIndex] = analogRead(x_pin);
Yreadings[YreadIndex] = analogRead(y_pin);
delay(1);
Xtotal = Xtotal + Xreadings[XreadIndex];
Ytotal = Ytotal + Yreadings[YreadIndex];
delay(1);
XreadIndex = XreadIndex + 1;
YreadIndex = YreadIndex + 1;
if (XreadIndex >= MaxReadings) XreadIndex = 0;
if (YreadIndex >= MaxReadings) YreadIndex = 0;
delay(1);
X_Pos = Xtotal / MaxReadings;
Y_Pos = Ytotal / MaxReadings;
}
void setup() {
//Enable Motors
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
//Motor A
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
//Motor B
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
//Set Rotation
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.begin(9600);
leftServo.attach(left);
rightServo.attach(right);
for (int i = 0; i < MaxReadings; i++) {
Xreadings[i] = 0;
Yreadings[i] = 0;
}
}
void loop()
{
//Control speed of motors using potentiometer
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
analogWrite(enB, pwmOutput); // Send PWM signal to L298N Enable pin
// Control Servo motion
Smoother(thumbX, thumbY);
Servo_Pos = map(X_Pos, 0, 1023, 0, 180);
leftServo.write(Servo_Pos);
rightServo.write(Servo_Pos);
delay(20);
}


