Help with programming servo and stepper motor at the same time

Hi, I'm new to arduino with very little programming experience, so please excuse horrible programming and bad copy pasting. So, I am working on a project that uses a unipolar stepper motor and a non-continuous servo. Here is what i want to happen in steps...

Step 1- Servo rotates a full 180 degrees in either direction slowly (10 seconds for 180 degrees).

Step 2-stepper motor rotates one step.

Step 3-Servo rotates 180 degrees in the opposite direction of step one.

Step 4- Repeat all steps continuously.

Here is my code:

#include <Servo.h>
class Sweeper
{
Servo servo;
int pos; // current servo position
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position

public:
Sweeper(int interval)
{
updateInterval = interval;
increment = 1;
}

void Attach(int pin)
{
servo.attach(pin);
}

void Detach()
{
servo.detach();
}

void Update()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
servo.write(pos);
Serial.println(pos);
if ((pos >= 180) || (pos <= 0)) // end of sweep
{
// reverse direction
increment = -increment;
}
}
}
};
Sweeper sweeper1(250); //inside parenthesis indicates speed, 10 would be about 4 seconds(not exactly)
#include <Stepper.h>
#include <LiquidCrystal.h>
#include <dht11.h>
dht11 DHT; //Note:DHT on behalf of the temperature and humidity sensor
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 13);
const int dht11_data = 6;
const int sensorPin = 0;
int stepCount = 0;
int temp=0;
int hum=0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long previousMillis = 0;
long interval = 500;
void setup()
{
sweeper1.Attach(A1);
lcd.begin(16,2);
lcd.print(" Welcome to ");
lcd.setCursor(0,1);
lcd.print(" DHT11");
delay(2000);
lcd.clear();
}
void loop()
{
sweeper1.Update();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
DHT.read(dht11_data);
temp=DHT.temperature * 1.8 + 32;
hum=DHT.humidity;
lcd.clear(); //clear display
lcd.print("Hum=%"); //display "Hum=%"
lcd.print(hum);
lcd.setCursor(10,0) ;
lcd.print("DHT11"); //display "Smraza"
lcd.setCursor(0,1) ; //Display position
lcd.print("Temp="); //display"Temp="
lcd.print(temp);
lcd.write(0xDF); //Display custom characters '°'
lcd.print("F");

}
// read the sensor value:
int sensorReading = 20;
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}

...obviously the timing doesn't match for the steps. But, my main problem is that the servo and motor are rotating slower than they should...(mainly the servo). Whenever, I adjust the speed of one, it affects both motor and servo speed.

Again, I'm new to arduino so please comment easy to understand stuff.

By the way, I forgot to say that you can ignore the DHT11, LCD, and potentimeter stuff.

try running with just the servo or the stepper motor;
if performance improves, then your problem is probably the power supply for the servo and stepper

note also that changing the servo's commanded position by only one degree / 200ms is slow regardless of any power problems.

Alright, I tested the program without the motor. The servo ran much faster and smoother. So, does anybody have any recommendations on how to keep enough power flowing?

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

...R
Stepper Motor Basics
Simple Stepper Code
Several Things at a Time

I'd merge them if IE11 would let me. Maybe another Mod can do it.

Corporate thing. Not allowed to add software to the machines.

Can't plug in non-approved USB anything either, nothing that has memory as it could contain virus, worm, malware, etc. Only keyboard & mouse. Anything else has to come from IT and only used in the corporate machines. Bring in a virus from home and you're in deep trouble.
On the road, I take my own laptop and leave the work machine at work.

CrossRoads:
Can't plug in non-approved USB anything either, nothing that has memory as it could contain virus, worm, malware, etc. Only keyboard & mouse. Anything else has to come from IT and only used in the corporate machines. Bring in a virus from home and you're in deep trouble.

Sounds very sensible from a corporate point of view, even if frustrating.

I think if I was the corporation I would put the PCs in welded steel boxes so the staff could not get near anything that could screw them up :slight_smile: (I used to be one of the adventurous users that was a PITA for support - especially as I was very friendly with the head of support)

Of course that does nothing to prevent time-wasting on mobile phones (speaking as an irritated customer).

...R

so the staff could not get near anything that could screw them up

Like the internet?

PaulS:
Like the internet?

Especially that :slight_smile:

...R