Mototres and buttons

I want to make a code with which through buttons I can control the two motors

int estado = 0;
int estadob1 = 0;
int estadob2 = 0;
int estadob3 = 0;
int estadob4 = 0;

void setup()
{
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT); 
  Serial.begin(9600);
}

void loop()
{
  leerInt();
  
  if (estado == 0){
    apagar();
  }else if (estado == 1){
    adelante();
  }else if (estado == 2){
    derecha();
  }else if (estado == 3){
    izquierda();
  }else if (estado == 4){
    atras();
  }
}
  
  void leerInt(){
    
  estadob1 = digitalRead(2);
  estadob2 = digitalRead(3);
  estadob3 = digitalRead(4);
  estadob4 = digitalRead(5);
    
  if (estadob1 == 0 && estadob2 == 0 
      && estadob3 == 0 && estadob4 == 0) {
    estado = 0;
  } else if (estadob2 == 1) {
    estado = 1;
  } else if (estadob3 == 1) {
    estado = 2;
  } else if (estadob3 == 1) {
    estado = 3;
  }else if (estadob3 == 1){
    estado = 4;
  }
}


void adelante()
{
  analogWrite(13, 255);
  analogWrite(12, 255);
  delay(500); 
  Serial.println("adelante");
}

void derecha()
{
  analogWrite(13, 255);
  analogWrite(12, 25);
  delay(500); 
  Serial.println("derecha");
}

void izquierda()
{
  analogWrite(13, 25);
  analogWrite(12, 255);
  delay(500); 
  Serial.println("izquierda");
}
void atras()
{
  analogWrite(13, 15);
  analogWrite(12, 15);
  delay(500); 
  Serial.println("atras");
}

void apagar()
{
  analogWrite(13, 0);
  analogWrite(12, 0);
  delay(500); 
  Serial.println("apagar");
}

Baded on your description, I don't think that you have a problem with the IDE. Hence your topic has been moved to a more suitable location on the forum.

You can't power a motor from an Arduino pin like that. It will damage the Arduino. It will be fried.

You need a proper motor driver for the motors you have.

  • When you close a switch, you will short 5v to GND !

:scream:

  • Non reversing motors need a kickback diode.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.