Help with controling 4 geared DC motors with internal Pots

I have four small geared dc motors with internal pots . I just need help with my sketch. I can make the first motor run and stop where I want it by reading the pot.
What I need help in is how to set up these 4 motors to run one at a time. Run motor#1 to a point determined by the internal pot position. Then start motor#2 and so on. I am using the L298N to drive my motors with a external power supply.
Hear is code that I am using for my first motor. I have to learn how to upload a Sketch the proper way



const int analogPin = A5; // pin that the sensor is attached to
const int ledPinL = 3; // pin that the LED is attached to
const int ledPinR = 2;
//const int thresholdR = 20; // an arbitrary threshold level that's in the range of the analog input
void setup() {
// initialize the LED pin as an output:
pinMode(ledPinR, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
int analogValue = analogRead(analogPin);
}
void loop() {
int analogValue = analogRead(analogPin);

if (analogValue > 900) {Use code tags to format code for the forum
digitalWrite(ledPinR, LOW);
} else {
digitalWrite(ledPinR, HIGH);
}
// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
}

``Use code tags to format code for the forum* List item

Please post a link to the data sheet or product page for these items, and a wiring diagram showing how you propose to connect four of them. A photo of a hand drawn sketch is fine.

The code and comments indicate that you are controlling an LED, not a motor, and will not work with the L298 motor driver.

1 Like

Thank you for the comment. Just because it says LED the L298n driver just needs the 5 volt high signal from the Arduino to turn it on. I do not need any help with the electrical part. I just need help with the code in turning on the motors one at a time so that one motor waits for the other motor . Just like the code I have but I need the code to look at the first motor only pot and stop and then start the next motor and so on that each motor running in the Loop runs one at a time . The trouble I have sense the motor pots are in the same Loop they look at all the motor pots at once . The code I have uploaded works for the one motor. The motor runs and stops where I want it to. I hope that makes sense.

Right now, the loop function runs continuously, turning the motor on if analogValue>900, and off if it isn't.

To add more motors you need a different approach. I recommend using a "state machine", in which a state variable determines which of several actions is needed.

One place to learn about the basic idea is to study the Arduino example Files>Examples>StateChangeDetection.

The motor control in the posted code will not work if you ever need to move a motor in both directions. You need at least two digital pins controlling each L298 driver.

Thank you for the reply. Yes I understand I need another pin to turn the motor in the other direction. That is just the code I have so far. I just do not know how to make a sketch to turn motor on to the left, read the pot and stop, Pause turn the motor to the Rht ,read the same pot again and stop. What makes it hard for me all this is happening in the same Loop. When I used the Basic Stamp it was very easy for me to program because I could loop each event and go to the next. But I am trying to learn how to use the Arduino because it is so much better. The pots in the motor when read reads 0 to 1000 so it is easy to have it stop where I want it Thank so much for your help

On line you will find many Arduino/L298 motor tutorials covering the topic. I suggest the search phrase "arduino l298 motor control".

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