Code help for running DC motor (Arduino Nano)

Hi everybody. Can anybody help me verify this code is correct? I'm working on running a DC motor via an Arduino Nano, and cannot get it to run for the life of me. I have verified that the Nano works, the L298N works, and the rotary encoder works. I re-soldered everything and checked for proper continuity, so I am fairly confident that is not the issue. The only other thing I can think of is that my code is wrong. I am new to electronics, so I'm hoping this is a rookie mistake and not faulty components/bad connections.

[code]
const int pwm = 2 ;  //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 9 ;

//For providing logic to L298 IC to choose the direction of the DC motor

void setup()
{
  pinMode(pwm, OUTPUT) ;  //we have to set PWM pin as output
  pinMode(in_1, OUTPUT) ; //Logic pins are also set as output
  pinMode(in_2, OUTPUT) ;
}

void loop()
{
  //For Clock wise motion , in_1 = High , in_2 = Low

  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, LOW) ;
  analogWrite(pwm, 255) ;

  /*setting pwm of the motor to 255
    we can change the speed of rotaion
    by chaning pwm input but we are only
    using arduino so we are using higest
    value to driver the motor  */

  //Clockwise for 3 secs
  delay(3000) ;

  //For brake
  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, HIGH) ;
  delay(1000) ;

  //For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH
  digitalWrite(in_1, LOW) ;
  digitalWrite(in_2, HIGH) ;
  delay(3000) ;

  //For brake
  digitalWrite(in_1, HIGH) ;
  digitalWrite(in_2, HIGH) ;
  delay(1000) ;
}

[/code]

Hardware wise, one thing I'm unsure about is if these jumpers should be on or not. They are currently on as I am using a 12V power supply.

Here is the schematic, which I am using a perfboard. I can post a picture of the connections if needed, but obviously it's a bit messy and probably not helpful. I did make sure the encoder, arduino, and l298N share a common ground and then the one off the 10K resistor is grounded by itself. One question on this, is the schematic telling me that they all share a common ground, with ALSO needs to be grounded by itself (to the perfboard, in this case)?

First thing that seems strange to me is, in your circuit diagram you have the mechanical encoder switches into pins D2 and D3 and the on-off (I imagine) switch on pin D7 but nothing about it in the setup as inputs. I would imagine these should be INPUT_PULLUP also.

No idea what you mean about "one of the 10k resistors being grounded by itself.
Everything marked as ground is the one common ground.

The code seems to belong to something else.

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