newbie trouble with a nano and L298n

Hi everyone, hopefully I am improving with my ability to post…not so much on the coding but I’m trying,

It seems like it should be pretty simple to push a button and make a motor turn but it’s not working and I’m a bit stuck. would appreciate a point in the right direction…here is the sketch:

////
//////arduino nano with L298n
//////2 push button with 2 motor
//////push for on, release for off
//////potentiameter speed control
//////
////push button A pin d6
////push button B pin d5
////
////motor 1 enable pin d10
////motor 1 int 1 pin d7
////motor 1 int 2 pin d8
////
////motor 2 enable pin d9
////motor 2 int 3 pin d2
////motor 2 int 4 pin d4
//
//potentiometer pin a1

#define enA 10
#define in1 7
#define in2 8
#define buttonA 6

#define enB 9
#define in3 2
#define in4 4
#define buttonB 5

// Speed control potentiometers

int SpeedControl1 = A1;
int SpeedControl2 = A1;
// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(buttonA, INPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(buttonB, INPUT);

}

void loop() {

  // Set Motor A forward

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

  // If button is pressed - turn motor on
  
  if (buttonA == true) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    delay(20);

    // Set Motor B forward

    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    
    // If button is pressed - turn motor on
   
    if (buttonB == true) {
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      delay(20);
  }
      // Read the values from the potentiometers

      MotorSpeed1 = analogRead(SpeedControl1);
      MotorSpeed2 = analogRead(SpeedControl2);


      // Convert to range of 0-255

      MotorSpeed1 = map(MotorSpeed1, 0, 1023, 0, 255);
      MotorSpeed2 = map(MotorSpeed2, 0, 1023, 0, 255);

      // Adjust to prevent "buzzing" at very low speed

      if (MotorSpeed1 < 8)MotorSpeed1 = 0;

      if (MotorSpeed2 < 8)MotorSpeed2 = 0;


      // Set the motor speeds

      analogWrite(enA, MotorSpeed1);
      analogWrite(enB, MotorSpeed2);


    }

  }

Comparing buttonA or buttonB which are pin numbers won't do anything useful. Probably you meant digitalRead() of buttonA or buttonB.

Steve

slipstick:
Comparing buttonA or buttonB which are pin numbers won’t do anything useful. Probably you meant digitalRead() of buttonA or buttonB.

Steve

Thank you for the reply, I realized i messed up there and changed it to this:

////
//////arduino nano with L298n
//////2 push button with 2 motor
//////push for on, release for off
//////potentiameter speed control
//////
////push button A pin d6
////push button B pin d5
////
////motor 1 enable pin d10
////motor 1 int 1 pin d7
////motor 1 int 2 pin d8
////
////motor 2 enable pin d9
////motor 2 int 3 pin d2
////motor 2 int 4 pin d4
//
//potentiometer pin a1

#define enA 10
#define in1 7
#define in2 8
const int buttonPinA = 6;

#define enB 9
#define in3 2
#define in4 4
const int buttonPinB = 5;
// variables will change:
int buttonStateA = 0;         // variable for reading the pushbutton status
// variables will change:
int buttonStateB = 0;         // variable for reading the pushbutton status

// Speed control potentiometers

int SpeedControl1 = A1;
int SpeedControl2 = A1;
// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(buttonPinA, INPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(buttonPinB, INPUT);

}

void loop() {

// Read the values from the potentiometers

MotorSpeed1 = analogRead(SpeedControl1);
MotorSpeed2 = analogRead(SpeedControl2);


// Convert to range of 0-255

MotorSpeed1 = map(MotorSpeed1, 0, 1023, 0, 255);
MotorSpeed2 = map(MotorSpeed2, 0, 1023, 0, 255);

// Adjust to prevent "buzzing" at very low speed

if (MotorSpeed1 < 8)MotorSpeed1 = 0;

if (MotorSpeed2 < 8)MotorSpeed2 = 0;


// Set the motor speeds

analogWrite(enA, MotorSpeed1);
analogWrite(enB, MotorSpeed2);
 
  // Set Motor A forward
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

  // read the state of the pushbutton value:
  buttonStateA = digitalRead(buttonPinA);

  if (buttonStateA == HIGH) {
    // turn LED on:
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    delay(20);
  } else {

    
    // turn motor off:
  } digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  delay(20);

  // Set Motor B forward
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // If button is pressed - turn motor on

  // read the state of the pushbutton value:
  buttonStateB = digitalRead(buttonPinB);
  if (buttonStateB == HIGH) {
    // turn LED on:
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(20);
  } else {
    // turn motor off:

  } digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(20);





}

I’m still struggling with making it work correctly. Can this be done with just 1 potentiometer?

I'm still struggling with making it work correctly.

Please take a moment to tell us what goes wrong.

jremington:
Please take a moment to tell us what goes wrong.

the Motor B is running when the power turned on and has no reaction to the buttonB.

Motor A only runs when you press the buttonA like it should be.

Both seem have speed cotrol from the one potentiometer.

Here is the code right now, I tried to fix some comments so it was easier to understand

////
//////arduino nano with L298n
//////2 push button with 2 motor
//////push for on, release for off
//////potentiameter speed control
//////
////push button A pin d6
////push button B pin d5
////
////motor 1 enable pin d10
////motor 1 int 1 pin d7
////motor 1 int 2 pin d8
////
////motor 2 enable pin d9
////motor 2 int 3 pin d2
////motor 2 int 4 pin d4
//
//potentiometer pin a1

#define enA 10
#define in1 7
#define in2 8
const int buttonPinA = 6;

#define enB 9
#define in3 2
#define in4 4
const int buttonPinB = 5;
// variables will change:
int buttonStateA = 0;         // variable for reading the pushbutton status
// variables will change:
int buttonStateB = 0;         // variable for reading the pushbutton status

// Speed control potentiometers

int SpeedControl1 = A1;
int SpeedControl2 = A1;
// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(buttonPinA, INPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(buttonPinB, INPUT);

}

void loop() {

// Read the values from the potentiometers

MotorSpeed1 = analogRead(SpeedControl1);
MotorSpeed2 = analogRead(SpeedControl2);


// Convert to range of 0-255

MotorSpeed1 = map(MotorSpeed1, 0, 1023, 0, 255);
MotorSpeed2 = map(MotorSpeed2, 0, 1023, 0, 255);

// Adjust to prevent "buzzing" at very low speed

if (MotorSpeed1 < 8)MotorSpeed1 = 0;

if (MotorSpeed2 < 8)MotorSpeed2 = 0;


// Set the motor speeds

analogWrite(enA, MotorSpeed1);
analogWrite(enB, MotorSpeed2);
 
  // Set Motor A forward
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

  // read the state of the pushbutton value:
  buttonStateA = digitalRead(buttonPinA);

  if (buttonStateA == HIGH) {
    // turn motor A on:
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    delay(20);
  } else {
    // turn motor A off:
  } digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  delay(20);

  // Set Motor B forward
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  // If button is pressed - turn motor on

  // read the state of the pushbutton value:
  buttonStateB = digitalRead(buttonPinB);
  if (buttonStateB == HIGH) {
    // turn Motor B on:
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(20);
  } else {
    // turn Motor B off:

  } digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(20);





}

This is so strange. Please look at the picture. If I remove the purple wire from the breadboard, nothing changes and the motor still turns without pushing the button. If I remove that same purple wire from the nano, the motor stops turning and of course still doesn’t work with the button…what on earth could be causing this??

OP picture:

It looks like the purple wire is connected to VCC (wrong). If you disconnect it, it is floating and the input is unpredictable (also wrong).

aarg:
OP picture:

what does that mean?

It means you buggered up the wiring.

what does that mean?

@aarg was showing you how to post your picture properly. Image posting guide.

OP means, “original poster”… you.

aarg:
It means you buggered up the wiring.

Haha! Thank you, fixed it…all good!

jremington:
@aarg was showing you how to post your picture properly. Image posting guide.

Thank for the link!

aarg:
OP means, “original poster”… you.

Appreciate the education folks, …slowly but surely, I’ll get there…