Dc Encoder Back and forth

Hi there. I'm trying to do a code where I can make a dc motor go half a moon, then backwards half a moon, then loop it while also adding a potentiometer to control the speed of the motor. Can someone help me with the code?

Thanks

Show us how you would connect everything.

This is how it would look like. I'm open to suggestions.

(mod edit)

A stepper motor or servo would be easier to do.
Regular DC motors as shown would need end stops to mark the start and end position.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

Additionally you may want to consider a seperate PSU for all those devices with a common ground back to the Arduino

I think you are asking for position control of a DC motor. That means creating a servo loop to control the motor,
based on an encoder and a PID loop.

What are you trying to do? Perhaps a stepper would make sense (more details are needed to judge that though)

How many degrees in a moon? Maybe a servo, 0 to 180 degrees, back to 0?

The same way a servo would work, but using a DC Motor instead. I would need to add an H-bridge to help with this, but i was thinking 0-180 degrees, then 180 degrees-0

@codewriter123

A DC motor cannot act the same as a servo or stepper.
All three are different beasts with specific uses and applications.

You could use a CAM system as used in some wiper motors but it would still need additional sensors (encoder / switch etc. ) to tell it where to park at the end of a cycle.

For a stepper motor, can you do 100 steps (for the 180) and then 100 steps back? I thought a stepper motor, for a full revolution is 200 steps (so 100 would be 180). Is it possible to code this for arduino? make it go 100 steps forward, then 100 steps back, with a potentiometer that could control the pwm or speed of it?

The purpose of this is for a project: simulating a ventilator that will pump air into the lungs and sensors to measure the exhalations.
The original idea was to have a DC motor that would be connected to pistons, but the motion of the pistons is not linear, therefore the airflow is unstable and inconsistent. That is why I was trying to fix going from a DC motor to a stepper motor and make it go half a moon back and forth to compensate for the full motion of the piston (and its different pwm for each half of the circle), if that makes sense.

A piston is a linear device.
It is the arm at the back of the piston that is non linear which is in turn in most cases connected to a CAM movement to perform the forward and backward translation.

Angular to linear / planar movements.

All three motor types are capable of PWM in some way

A stepper motor is normally 1.8 degrees per step but you can also use microstepping with the correct drivers to divide those by 8 = 0.225 or 16 = 0.1125 per step.
There are other drivers that can take microstepping to crazy low numbers.

It can be a little harder with cheap servos to microstep.

If control is what you seek then a stepper motor for sure as it can do both rotary and cam type movements.

You really need to consider the types of connection from whatever motor you end up choosing to whatever type of piston, bellow, etc.

I made a simple code, can you tell me if this would work (since the simulation is not showing going back and forth)

#include <Stepper.h>
int enablePin = 11; //PWM1 which is A
int in1Pin = 10; //DIRB1
int in2Pin = 9; //DIRB2
//int switchPin = 7; //BUTTON
//int potPin = A0; //FO POTENTIOMETER
int enablePin2 = 12; //PWM2 which is B
//bool reverse = false;
Stepper stepper1(200, in1Pin, in2Pin);

void setup()
{
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(enablePin2, OUTPUT);
//pinMode(switchPin, INPUT_PULLUP);

digitalWrite(enablePin, HIGH);
digitalWrite(enablePin2, HIGH);
stepper1.setSpeed(100);
}
void loop()
{
// Turn the stepper 100 steps which means 180 degrees
stepper1.step(100);
// Wait half second
delay(500);

// Turn the stepper 100 steps back which means 180 degrees
stepper1.step(-100);
// Wait half second
delay(500);
}

I figure it out, Thanks!