DC Motor that changes direction with two different push buttons

Noobie here. I search several forums and videos but can't seem to find any answers. What I'm trying to do is: Modify the below code to move the motor clockwise and counterclockwise based on the press of two buttons. The motor should turn continuously until the direction is changed again.

Here is the code I need to modify:

#include <util/atomic.h> // For the ATOMIC_BLOCK macro

#define ENCA 2 // YELLOW
#define ENCB 3 // WHITE
#define PWM 5
#define IN2 7
#define IN1 8

#define clk 9
#define counterclk 10

int dir, pwmVal, pwm, pwr;

volatile int posi = 0; // specify posi as volatile: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/


void setup() {
  Serial.begin(9600);
  pinMode(ENCA,INPUT);
  pinMode(ENCB,INPUT);
  attachInterrupt(digitalPinToInterrupt(ENCA),readEncoder,RISING);
  
  pinMode(PWM,OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  
  Serial.println("Task2 L298N control DC motor");
  
}

void loop() {

  // Read the position in an atomic block to avoid a potential
  // misread if the interrupt coincides with this code running
  // see: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
  int pos = 0; 
  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
    pos = posi;
  }

  pwmVal = 120; // sample pwm value to move the motor

  /*
   * Probably you want add a button reading here and update 
   * pwm value
   */
  
  if(pwmVal>0) {
    dir = 1;
  }
  else if (pwmVal <0) {
    dir = -1;
  }
  pwr =fabs(pwmVal);
  // signal the motor
  setMotor(dir,pwr,PWM,IN1,IN2);
  Serial.print("PWM value: ");
  Serial.print(pwr);
  Serial.print(" | Directon: ");
  Serial.print(dir);
  Serial.println();
}

/*
 * Probably your function to read buttons and assign pwm values
 */

void setMotor(int dir, int pwmVal, int pwm, int in1, int in2){
  analogWrite(pwm,pwmVal);
  if(dir == 1){
    digitalWrite(in1,HIGH);
    digitalWrite(in2,LOW);
  }
  else if(dir == -1){
    digitalWrite(in1,LOW);
    digitalWrite(in2,HIGH);
  }
  else{
    digitalWrite(in1,LOW);
    digitalWrite(in2,LOW);
  }  
}

void readEncoder(){
  int b = digitalRead(ENCB);
  if(b > 0){
    posi++;
  }
  else{
    posi--;
  }
}

Below is my Circuit Diagram:
![F|690x422](upload://zyIcJyVfz9AQvB8kfCrnnRTowbQ.jpeg)

Any help would be gereatly appreciated!

Sorry...Circuit diagram didn't display.

Sorry but that's a coloured bird nest lacking most of value. Bread bord surface as well as the blue board of an UNO tells nothing.
That PP3 battery tells a lot. Get rid of it! It delivers 9 volt but not any Amps.

5v into the Arduino Vin pin will not work, need > 6.5v

Read this topic: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

Note the need for links to datasheet of especially motors, driver boards etc. and power supplies.

Sorry, but the above scematic is what I need to work with. I thought I posted everything correctly? Am I in the wrong forum?

is not a schematic. It's a Fritzing toy picture not showing things the way any engineer would use them. A desperate criminal detective solving a tricky murder would create a proper drawing of that mess. Helpers don't spend time on that.

Drop that battery. It's not enough for a motor.

Is this better?

Simplest I can think of assuming grounded switches and INPUT_PULLUP.

Change this:

To this:


if(digitalRead(button1 == LOW) {
    dir = 1;
  }
  else if (digitalRead(button2 == LOW) {
    dir = -1;

oops...missing a )

Good catch!  Yes, it should be:

if(digitalRead(button1) == LOW)

The OP seems to be AWOL. Nonetheless...

Why not just use one button that switches the direction?

Two may be needed so the initial direction can be selected, but the OP hasn't said that's a requirement.

Whether one or two, debouncing is necessary.

Maybe not.  Once one of the switches is made and the logic responds the output won't be bouncing even though the switch might.  It's basically a latch circuit.  Now, if malignant operators are involved that changes things.

Ah, right. With two buttons, no need to debounce.

Sorry, I'm oboviously a dunce. I did your recommendations above but nothing is happening. After rereading the instructions above, I need to use the "pwmVal" variable and can only update or just add to the existing code.

  pwmVal = 120; // sample pwm value to move the motor

  /*
   ** Probably you want add a button reading here and update *
*   * pwm value*
   */
  
  if(pwmVal>0) {
    dir = 1;

Well, I've been working on this problem for abopput 2-1/2 weeks now (on and off). At the beginning, I wasn't looking for you guys to simply give me the correct code because I want to learn this but this assignment is due in the next few days. Would it be to much of a problem for you show me the code if you had this assignment? My anxiety is starting to kick in. I'll understand if this isn't meeting the forum policy.
Thank you

Is it ethical to pass off someone else's work on a school assignment as your own?

(It sounds like that is what you are hoping to do. Forgive me if I'm wrong.)

It’s time you update us on the current wiring and the current sketch.

1 Like

No, no, no...I"m not asking for you to give the code. My appolgies if I'm coming across that way. I'm just looking for guidence if you were to complete this assignment. I would rather fail the assignment but learn little than pass and learn nothing.

Nowhere in this thread do I see where you are STOPPING the motor before changing direction.

It sounds like you asking how to modify the pwmVal variable based on button presses, and let the rest of the code use the pwmVal value to run the motor. Is that it?

You need to use digitalRead() - Arduino Reference to read a button, and if - Arduino Reference to do something conditionally based on the result.

Given the picture, you might have to figure out what digitalRead(button) gives you when the button is pressed and when it is not pressed.