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.