I am using an arduino uno and two DC motors to control some drawers.
There are 3 'templates' of moving the drawers, based on inputs received from relays.
The motors are controlled with a L298N module
and powered by a 12v power supply. The arduino is powered by a different supply via USB.
It usually works the way it should, except that sometimes (maybe 1 in 20) the arduino 'crashes' (when receving input1 or input2) and the motor keeps spinning in one direction and further inputs won't work. I get a "C" in serial monitor when this happens.
When controlling dc motors, i need to take the jumpers off and connect in the place 5v from arduino. Could the module be sending back some noise on the 5v wire and cause the arduino to crash?
Any idea what else could cause this?
Here is the code:
//Motor A
const int motorPin1 = 9; // Pin 14 of L293
const int motorPin2 = 10; // Pin 10 of L293
//Motor B
const int motorPin3 = 6; // Pin 7 of L293
#define input1 3
#define input2 2
#define input3 7
#define senzor 12
#define lock 4
void setup() {
Serial.begin(9600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(input1, INPUT_PULLUP);
pinMode(input2, INPUT_PULLUP);
pinMode(input3, INPUT_PULLUP);
pinMode(lock, OUTPUT);
pinMode(senzor, INPUT_PULLUP);
digitalWrite(lock, LOW);
}
void loop() {
if(digitalRead(input1)==0&&digitalRead(senzor)==0&&digitalRead(input2)==1)
{
Serial.println("open drawer");
digitalWrite(lock, HIGH);
analogWrite(motorPin1, 215);
delay(1600);
analogWrite(motorPin1, 0);
digitalWrite(lock, LOW);
}
else if(digitalRead(input2)==0&&digitalRead(senzor)==1&&digitalRead(input1)==1)
{
Serial.println("close drawer");
digitalWrite(lock, LOW);
analogWrite(motorPin2, 200);
delay(1600);
analogWrite(motorPin2, 0);
}
else if(digitalRead(input3)==0&&digitalRead(input1)==1)
{
Serial.println("vibrating drawer");
analogWrite(motorPin3, 255);
delay(1600);
analogWrite(motorPin3, 220);
delay(1500);
analogWrite(motorPin3, 255);
delay(1000);
analogWrite(motorPin3, 0);
}
}
The sensor tells me when the drawer is closed and the lock is locking the drawer when it's closed.