I have built a Crane using 3 3V DC Motors using 2 L293D H-Brigdes, i've programmed a GUI in Python for controlling it, it works perfectly, but, when i want to make an specific routine, powering the Arduino with batteries or using the USB connection, it performs the fist step and then the Arduino resets constantly, i've put a decoupling capacitor, but it's same.
Sorry for my English,
Here's the schematic, the Sketch for the GUI and the sketch i would want to run
PS: The second one is for only 2 motors
const int motor1Pin = 3; // Puente H 1 entrada 1 (pin 3, 1A)
const int motor2Pin = 4; // Puente H 1 entrada 2 (pin 4, 2A)
const int enablePin = 9; // Pin de activacion 1 Puente H 1
const int enablePin2 = 10; // Pin de activacion 2 Puente H 1
const int enablePin3 = 11; // Pin de activacion 1 Puente H 2
const int motor1Pin2 = 6; // Puente H 1 entrada 3 (pin 6, 3A)
const int motor2Pin2 = 7; // Puente H 1 entrada 4 (pin 7, 4A)
const int motor1Pin3 = 5; // Puente H 2 entrada 1 (pin 5, 1A)
const int motor2Pin3 = 8; // Puente H 2 entrada 2 (pin 8, 2A)
int state = 0; // Salida digital, estado es 0, Apagado, bajo
int inState = 0; // Se ajusta el estado incial a 0
void setup() {
pinMode(motor1Pin, OUTPUT); // Se definen los pines definidos como salidas
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enablePin2, OUTPUT);
pinMode(motor1Pin3, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(enablePin3, OUTPUT);
delay(1000); // Esperar 1 seg
Serial.begin(9600); // Se configura la consola serial
digitalWrite(enablePin, HIGH); // Se establecen los pines activadores
digitalWrite(enablePin2, HIGH); // en estado alto
digitalWrite(enablePin3, HIGH);
}
void loop() {
char inVal = 0; // Valor entrante = 0
if (Serial.available() > 0) { // Mientras haya una comunicacion serial,
inVal = Serial.read() - '0'; // leer todos los valores entrantes y compararlos
if (inVal == 1) { // con un valor establecido y se configura el estado actual
inState = 1;
}
else if (inVal == 0) {
inState = 0;
}
else if (inVal == 2) {
inState = 2;
}
else if (inVal == 3) {
inState = 3;
}
else if (inVal == 4) {
inState = 4;
}
else if (inVal == 6) {
inState = 6;
}
else if (inVal == 5) {
inState = 5;
}
else {
Serial.println("Only 1 (on) and 0 (off) are supported.");
}
if (inState != state) {
state = inState; // Actual estado
if (state == 1) //Adelante
{
digitalWrite(motor1Pin3, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin3, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(motor1Pin2, HIGH); // set leg 1 motor 2 of the H-bridge low
digitalWrite(motor2Pin2, LOW); // set leg 2 motor 2 of the H-bridge high
}
else if (state == 2) //Reversa
{
digitalWrite(motor1Pin3, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin3, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, HIGH); // set leg 2 of the H-bridge low
}
else if (state == 3) //Izquierda
{
digitalWrite(motor1Pin3, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin3, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
}
else if (state == 4) //Derecha
{
digitalWrite(motor1Pin3, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin3, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
}
else if (state == 5) //Abrir/Cerrar
{
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin3, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin3, LOW); // set leg 2 of the H-bridge low
}
else if (state == 6) //Abrir/Cerrar
{
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin3, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin3, HIGH); // set leg 2 of the H-bridge low
}
else if (state == 0)
{
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
}
Serial.print("State is now: "); // Prints the current state
Serial.println(state); // to the serial monitor
}
}
}
const int switchPin = 2; // switch input
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
const int enablePin2 = 10;
const int motor1Pin2 = 6;
const int motor2Pin2 = 7;
void setup()
{
// set the switch as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enablePin2, OUTPUT);
Serial.begin(9600);
//pinMode(ledPin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin2, HIGH);
}
void loop()
{
int i = 0;
int j = 0;
int k = 0;
int g = 0;
// if the switch is high, motor will turn on one direction:
while (i != 1000)
{
Serial.println(i);
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(motor1Pin2, HIGH); // set leg 1 motor 2 of the H-bridge low
digitalWrite(motor2Pin2, LOW); // set leg 2 motor 2 of the H-bridge high
i = i + 1;
}
// if the switch is low, motor will turn in the other direction:
while (j != 1000)
{
Serial.println(j);
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, HIGH); // set leg 2 of the H-bridge low
j = j + 1;
}
while (k != 1000)
{
Serial.println(k);
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
k = k + 1;
}
while (g != 1000)
{
Serial.println(g);
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge low
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
g = g + 1;
}
}
