Project making us loose our minds

Hello, we are in high school and have a project to do.
We want to get 2 buttons and a H bridge working to change our 2 motors' direction of rotation.
However, our programm (on Tinkercad) doesn't work : we can't connect the 2nd button, and the motors work even when they are not supposed to.
We don't know if we did the connection correctly.
Can someone help us please?
Thanks!! :slight_smile:

Here is the link to our Tinkercad: Circuit design 2 moteur + bouton + peh +capteur | Tinkercad
Here is our programm:

//Connection bouton
const int bouton = 1;
int etatBouton;
//Connection capteur distance
int sinal=6;
float distance;
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;


void setup() {
    // Set up du capteur
    Serial.begin(9600); //on sait pas
    pinMode(sinal,OUTPUT);
    digitalWrite(sinal,LOW);
	
  // Set all the motor control pins to outputs
	pinMode(enA, OUTPUT);
	pinMode(enB, OUTPUT);
	pinMode(in1, OUTPUT);
	pinMode(in2, OUTPUT);
	pinMode(in3, OUTPUT);
	pinMode(in4, OUTPUT);
	
	// Turn off motors - Initial state
	digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
}

void loop() {
	directionControl();
	delay(1000);
    etatBouton = digitalRead(bouton); //lire état bouton
    read(); //lire la distance
    distance=distance/2;
    distance=distance*0.034029;
  
    Serial.println(distance);
    delay(100);
}  

// This function lets you control spinning direction of motors
void directionControl() {
	// Set motors to maximum speed
	// For PWM maximum possible values are 0 to 255
	analogWrite(enA, 255);
	analogWrite(enB, 255);
  
	//Conditions moteurs
    if(distance<100 && etatBouton == 1){ //bouton pas appuyé
    digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW); // les moteurs ne tournent pas
    }
    else if (distance>100 && etatBouton == 1){ // bouton pas appuyé
    digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW); // Le moteur ne tourne pas 
    }
    else if (distance<100 && etatBouton == 0){ // bouton appuyé
    digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW); // Le moteur ne tourne pas 
    }
    else if (distance>100 && etatBouton == 0){ // bouton appuyé
    digitalWrite(in1, HIGH);
	digitalWrite(in2, LOW);
	digitalWrite(in3, HIGH);
	digitalWrite(in4, LOW); // Le moteur tourne 
    }
  else{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); // Le moteur s'arrête de tourner
  }
}	
  void read(){
  pinMode(sinal,OUTPUT);
  digitalWrite(sinal,HIGH);
  delayMicroseconds(5);
  digitalWrite(sinal, LOW);
  
  pinMode(sinal,INPUT);
  distance=pulseIn(sinal,HIGH);
}

What's prevent you from doing that?

We tried connecting the 2nd button, but everytime we do so, it doesn't work/respond and when we change the programm to make it work, the motors start working on their own.

I clicked on your tinkercad link.
I can see your project. I can click on start simulation but then nothing happens

If running the simulation requires to register to tinkercad: I will NEVER register on tinkercad. Autodesk is a private owned company with a very aggressive marketing strategy to offer simulations for free for addicting people to autodesk.

What I discovered in your code you are using IO-pin 1.
Io-pin 0 and IO-pin 1 are reserved for the serial connection. You should never use IO-pin 0 and IO-pin 1 for driving any kind of hardware.

If you want real help post your code here in the forum as a code-section

best regards Stefan

Please show your efforts.
Asking about working with button without of button itself is pointless

Do you are able to provide a serious attempt to find a solution for leogal too?

I don't think it requires to register on TinkerCad.. Maybe you didn't click on the button after starting the simulation/used the captor..?

Also, we don't understand where you're advising us to post?

Anyway, thank you for your answer, we will try to solve the problem with the IO-pin 1.
Leo :slight_smile:

We have another tinkercad programm, but we changed so much that it's completely different and doesn't work at all anymore (because we took ligns out of our programm and put them back but not in the right place, we'll change it but right now it doesn't work)
We can still send it if you want/need though.

This is how your tinkercad sim looks like if a start the simulation
no motor-motion to seen
no reaction on button-presses

You need to keep the button pressed.
If it still doesn't work, could you check this (Circuit design Copy of 2 moteur + bouton + peh +capteur | Tinkercad) programm/code, we know the code is wrong
(if it requires to create a tinkercad account, don't worry we will find someone else to help us)

Your button pin isn't set to "pinMode(INPUT_PULLUP)" or "pinMode(INPUT_PULLUP)".

you haven't even declared it as an input.

p.s. simulation doesn't work.

There is absolutely no visual feedback that would show the motors rotating
there is absolutely no visual feedback about clicking a button
there is absolutely no option to change what the ultrasonic sensor is measuring

There is another simulator which does not require registration for using it.
Though this simulator has no L293D -component

For testing how to use buttons it would be still useful.
anyway develping such a project as a beginner will promise more succes if you do it in small steps

This is the wokwi-simulator

a simple code that does nothing more than switching on of an LED
a simple code that does nothing more than switching on of an LED on a button-press
etc. etc.

Okay, then I think you'd need to register to see it :frowning:

We did, we started with LEDs and a button, then LEDs and a sensor, then we added the motor... Anyway we did a LOOOOT of small steps, which were succesful... until we started adding a second button to the motor..

Thank you for your help and for simulator recommandation

Post #11.

We tried this but it doesn't work. Here is the link to the updated programm : Circuit design Copy of 2 moteur + bouton + peh +capteur | Tinkercad
Thanks for your help :slight_smile:

It's not obligatory. All pins are initialized as inputs by default.

Here is a code-version with serial printing to make visible what your code is doing
Move your mouse to the upper-right corner of the code
Then a copy-symbol appears
simply click the copy symbol and then paste the code into tinkercad

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

//Bonne chance :)

//Connection bouton1&2
const int bouton = 10;
int etatBouton;
const int bouton2 = 2;
int etatBouton2;
//Connection capteur distance
int sinal = 6;
float distance;
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;


void setup() {
  // Set up du capteur
  Serial.begin(9600); //on sait pas
  Serial.println("Setup-Start");
  pinMode(sinal, OUTPUT);
  digitalWrite(sinal, LOW);

  pinMode(bouton, INPUT_PULLUP);
  pinMode(bouton2, INPUT_PULLUP);

  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("exiting Setup");
}


// This function lets you control spinning direction of motors
void directionControl() {
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  //analogWrite(enA, 0); // zero means no PWM at all
  //analogWrite(enB, 0);
  analogWrite(enA, 255);
  analogWrite(enB, 255);
}


void read() {
  pinMode(sinal, OUTPUT);
  digitalWrite(sinal, HIGH);
  delayMicroseconds(20);
  digitalWrite(sinal, LOW);

  pinMode(sinal, INPUT);
  distance = pulseIn(sinal, HIGH);
}

void loop() {
  etatBouton = digitalRead(10); //lire état bouton1
  dbgc("1:",etatBouton);
  
  etatBouton2 = digitalRead(2);
  dbgc("2:",etatBouton2);

  //Serial.println(etatBouton);
  //Serial.println(etatBouton2);

  //read(); //lire la distance
  //distance=distance/2;
  //distance=distance*0.034029;
  dbgcf("3:",distance);    

  directionControl();
  //delay(1000);

  //Serial.println(distance);
  //delay(100);


  //Conditions moteurs
  if (distance < 100 && etatBouton == LOW && etatBouton2 == LOW) { //boutons pas appuyés
    Serial.println("bouton12 LOW ");
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); // les moteurs ne tournent pas
  }
  else if (distance < 100 && etatBouton == LOW && etatBouton2 == HIGH) { // bouton1 pas appuyé, bouton2 appuyé
    Serial.println("B1 LOW B2 HIGH");
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); // Le moteur ne tourne pas
  }
  else if (distance < 100 && etatBouton == HIGH && etatBouton2 == LOW) { // bouton1 appuyé, bouton2 pas appuyé
    Serial.println("B1 HIGH B2 LOW");
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); // Le moteur ne tourne pas
  }
  else if (distance > 100 && etatBouton == LOW && etatBouton2 == LOW) { // boutons pas appuyés
    Serial.println("distance > 100 B1 LOW B2 LOW");
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); // Le moteur tourne pas
  }
  else if (distance > 100 && etatBouton == LOW && etatBouton2 == HIGH) { // bouton1 pas appuyé, bouton2 appuyé
    Serial.println("distance > 100 B1 LOW B2 HIGH");
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH); // Le moteur tourne en arrière

    analogWrite(enA, 255);
    analogWrite(enB, 255);
  }
  else if (distance > 100 && etatBouton == HIGH && etatBouton2 == LOW) { // bouton1 appuyé, bouton2 pas appuyé
    Serial.println("distance > 100 B1 HIGH B2 LOW");
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW); // Le moteur tourne en avant

    analogWrite(enA, 255);
    analogWrite(enB, 255);
  } else {
    Serial.println("else");
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); // Le moteur s'arrête de tourner
  }
}

I think you need to debounce your buttons.

It works otherwise. :thinking:

Here is a WoSim of your project
It has serial output to show what is going on in your code
instead of motors the project has LEDs

best regards Stefan

One of the buttons is not connected to anything.

The other button is connected to Uno Pin 1. This pin is used for Serial.print() which you are using to send distance readings to the Serial Monitor. Do not use Pin 0 or Pin 1.

1 Like