Trying to control DC motor speed whilst controling servo sweep using Arduino Uno

Hello,

So this is the first time using the forum. I think I have this in the right topic area, but if not, sincere apologies. So the problem: for a project I am to set up my Uno to control DC motor speed using a potentiometer, while simultaneously controlling servo sweep from 0-180 degrees and back. The project is to use this setup to basically make an oscillating fan-Mechanical Engineering student, first year, in case anyone is wondering. We get extra credit for having "extras", i.g. for mine I have a red LED for when the motor is off, a green LED for when it is on, and will put a blue LED that fades with output from the potentiometer. Anyway, my code works, kind of. The DC motor speed is adjustable and the Red/Green LED come on as expected when its just the code for the motor, but when I add in the code for sweep control, the DC motor and LEDs only change after the servo has done its 180 sweep and back. Its super bizarre and I've been searching threads/scratching my head to figure it out. If anyone has any insight, please share it. I am using the Sparkfun Inventors Kit for all my hardware. My code is as follows:

int analogInPin = A0;
int sensorValue = 0;
int outputValue = 0;
int DC_pin = 11;
#include <Servo.h>
Servo myservo;
int pos=0;

void setup(){ 
pinMode(5, OUTPUT); 
pinMode(7, OUTPUT); 
pinMode(DC_pin, OUTPUT);
myservo.attach(2);
}
void loop() {

sensorValue = analogRead(analogInPin)/4; 
outputValue = map(sensorValue, 0, 1023, 0, 255); 
analogWrite(DC_pin, sensorValue); 
if (sensorValue >= 5)
{
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
}
else 
{ 
digitalWrite(5, HIGH); 
digitalWrite(7, LOW);
} 
 
 myservo.attach(2);  


   for (pos = 0; pos <= 180; pos += 1) {
     myservo.write(pos);              
     delay(15);                       
   }
 for (pos = 180; pos >= 0; pos -= 1) { 
     myservo.write(pos);             
     delay(15);                       
   
}
}

I know it is a little messy, I am still brand new to coding and learning all the ins and outs. Again, thank everyone so much for any help! I appreciate it all.

These should give you an idea of how to handle it.

multi-tasking the arduino part 1

Demonstration code for several things at the same time

That actually explains a lot and makes sense. Unfortunately, this code stuff still looks like Russian to me. :o I definitely don't expect anyone to write it for me, but how would I go about implementing the millis into my code. I don't know where or even how to approach this. This is literally my first taste of coding and I feel I am a bit over my head at the moment. Again, thank you, Arduino community.

study those links more but the basic concept is:

unsigned long starTime =0; // Use unsigned long when dealing with millis()
unsigned long interval = 1000 // 1000 millis = 1 second
void setup() {
 
}

void loop() {
        // do something you want to time then
 startime = millis();    // start the timer
        // do other stuff
 
if (millis()-starTime <= interval){ // if time is up
        // do stuff here
   starTime = 0; // reset the timer
 }
}

For another example similar to what Hutkikz posted, check the code in this post.

I moved the time check to a function but other than that, it's doing the same thing as Hutkikz's code.

The code I linked to controls a servo but uses an accelerating motion. If you need help converting to your linear sweep let us know.