Need Programming Help


I am new to programming in general. For my project I want to run a DC motor for a set period of time then stop and then reverse direction for another interval. Reference the image for a diagram of the hardware being used. I think I get how to set up the initial conditions but there are always multiple code sequences that will do what I want I just dont know how to make a simple program I can change a few variables very quickly.

Which Arduino? Will the motor be variable speed or always full speed?

Show us what you have, and what you want. Code & schematic..

I’m sure we can point you in the right direction.

I have an UNO R3. I would like to run at different speeds. Here is the closest schematic I have.
image

So where are you stuck?

Your picture looks very similar to a picture on L298N Motor Driver - Arduino Interface, How It Works, Codes, Schematics under the heading Arduino and L298N Motor Driver and there is associated code.

All of those code sections are looking for an analog input. I dont know how to let the program state how long to run the motor for and in what direction.

Some advice. IF you want to modify a known good program, get it working as originally written, THEN change it to do what you want. So, did you get it to work as written?

I am not really trying to modify that code previously mentioned. I don't know what commands will tell the motor to run at a speed. How to define that motor speed. The final thing I don't understand is how to tell it to run for x milliseconds. That is what I need help with. That tutorial showed me how to tell the board where to send what signal and properly define it but did not have a more simplified command to just run the motor without the analog input.

From the above link

This sets the speed

void loop() {
  int potValue = analogRead(A0); // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

It reads the voltage provided by the potentiometer; the values can range from 0 to 1023. The map maps that to 0 to 255 (basically divide by 4) which can be used by the analogWrite (that only takes values from 0 to 255).

Ok so if there is no potentiometer how do I define the PWM signal to run? I would not be mapping a potentiometer I would be setting the value within the code. How would that be defined?

void loop() {
  int speed = 255;
  analogWrite(enA, speed);
  delay(1000);

  speed = 0;
  analogWrite(enA, speed);
  delay(1000);

  ...
  ...

How you calculate your speed is up to you; these are just two hard-coded values (full speed and stop). This does not cater for the direction.

Ok so this is much more simple than I thought. Now if I want a value between 255 and 0 is that possible with out other code? Not sure what drives the 0 to 255 range.

Ok with this I think I can get something running. I will check back in once I have something note worthy to share.

Final Solution:

#define enA 9
#define in1 6
#define in2 7

void setup() {
  // put your setup code here, to run once:
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
  // Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}

void loop() {
  int speed = 0;
  analogWrite(enA, speed);
  delay(1000);

  speed = 255;
  analogWrite(enA, speed);
  delay(10000);

  speed = 0;
  analogWrite(enA, speed);
  delay(1000);

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

  speed = 255;
  analogWrite(enA, speed);
  delay(10000);
  
  speed = 0;
  analogWrite(enA, speed);
  delay(1000);

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

Digital write controls pin orientation on the h bridge of the motor controller. Analog write establishes the speed of the motor. Verified this with all the right components. Only thing missing from the diagrams was power from the controller back to the Arduino so there is no need for a separate power source.

Edit: Thank you to everyone who helped!

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