I need to put a Push Button to change the direction of the engine - Arduino

Asked

Viewed 28 times

-2

This code below makes the engine go forward and after 3 seconds go back repetitively, but I would like it to go forward by clicking the push button and then go back by clicking the push button, I’m cracking my skull on this and I see it’s simple but I can’t get out of this part

I’m using H Bridge L293D circuit

# define velmotor 3
# define m1a 4
# define m1b 5
# define tmp 3000

int vel=0;


void setup() {
  pinMode(velmotor, OUTPUT);
  pinMode(m1a, OUTPUT);
  pinMode(m1b, OUTPUT);

  digitalWrite(m1a,LOW);
  digitalWrite(m1b,LOW);
  analogWrite(velmotor,vel);
}

void loop() {
  
  vel=255;
  analogWrite(velmotor,vel);
  digitalWrite(m1a,LOW);
  digitalWrite(m1b,HIGH);
  delay(tmp);
  
  digitalWrite(m1a,LOW);
  digitalWrite(m1b,LOW);
  delay(500);
  
  digitalWrite(m1a,HIGH);
  digitalWrite(m1b,LOW);
  delay(tmp);
  digitalWrite(m1a,LOW);
  digitalWrite(m1b,LOW);
  delay(500);

    
      }

inserir a descrição da imagem aqui

1 answer

0

Let’s go in pieces. I see you have some problems with the code, but let’s take it slow. The first step is to learn how the push button works, and then we mix it with the motor drive. Believe me, this whole thing is a little tricky for people who don’t have practice. I will leave a code and an address so that after reaching this step I can advance one more.

https://blogmasterwalkershop.com.br/arduino/arduino-utilizando-o-push-button/

const int pinoBotao = 7; //PINO DIGITAL UTILIZADO PELO PUSH BUTTON
const int pinoLed = 2; //PINO DIGITAL UTILIZADO PELO LED
 
void setup() {
pinMode(pinoBotao, INPUT_PULLUP); //DEFINE O PINO COMO ENTRADA / "_PULLUP" É PARA ATIVAR O RESISTOR INTERNO
  //DO ARDUINO PARA GARANTIR QUE NÃO EXISTA FLUTUAÇÃO ENTRE 0 (LOW) E 1 (HIGH)
pinMode(pinoLed, OUTPUT); //DEFINE O PINO COMO SAÍDA
digitalWrite(pinoLed, LOW); //LED INICIA DESLIGADO
}
void loop(){
  if(digitalRead(pinoBotao) == LOW){ //SE A LEITURA DO PINO FOR IGUAL A LOW, FAZ
      digitalWrite(pinoLed, HIGH); //ACENDE O LED
  }else{ //SENÃO, FAZ
    digitalWrite(pinoLed, LOW); //APAGA O LED
  }
}

I will mark this question to accompany your development. Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.