i need some suggestion code for bipolar Stepper motor with limit switch forward and reverse continues rotation until reach the limit switch in then rotate opposite..this is my current running code in my arduino:
const int ForwardLimitSwitchPin = 8;
const int ReverseLimitSwitchPin = 9;
const int StepperStepPin = 10;
const int StepperDirectionPin = 11;
const int LimitSwitchActivated = LOW; // Limit switch grounds pin
const int StepperMaxRPM = 100;
void loop() {
// Step forward until the limit switch is activated
while (digitalRead(ForwardLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(1);
}
// Step reverse until the limit switch is activated
while (digitalRead(ReverseLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(-1);
}
}
the code is running but the strength of the motor is not enough for my belt to carry the conveyor..im planning to build a automated plant watering system with conveyor... the stepper is one that will carry the sprinkler tube with holes by using belt. the concept is from the printer..
If that was my project I would have a variable to keep track of the required direction and it would be changed whenever a limit switch is pressed. Then the motor code would take account of that. Something like this pseudo code
void loop() {
checkSwitches();
moveMotor();
}
void checkSwitches() {
if (leftLimitSwitch is pressed) {
motorDirection = 'R';
}
if (rightLimitSwitch is pressed) {
motorDirection = 'L';
}
}
void moveMotor() {
if motorDirection == 'R'
stepRight
}
else {
stepLeft
}
}
#include <LiquidCrystal_I2C.h> // includes the LiquidCrystal Library
#include <dht.h>
#include <Stepper.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
dht DHT;
void setup()
{
lcd.begin(20,4); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
lcd.backlight();
}
void loop()
{
HumTemp();
soilMoisture();
stepperMotor();
}
void soilMoisture()
{
int sensor_pin = A0;
int moisture;
moisture= analogRead(sensor_pin);
moisture = constrain (moisture, 400,1023);
moisture = map(moisture,400,1023,100,0);
lcd.setCursor(0,3);
lcd.print("Soil Moisture: ");
lcd.print(moisture);
lcd.print("% ");
delay(2000);
if ( moisture < 50)
{
stepperMotor();
}
else if (moisture > 80)
{
}
}
void HumTemp()
{
#define dataPin 2
int readData = DHT.read11(dataPin);
int t = DHT.temperature;
int h = DHT.humidity;
lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Temp: "); // Prints string "Temp." on the LCD
lcd.print(t); // Prints the temperature value from the sensor
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(12,1);
lcd.print("Hum: ");
lcd.print(h);
lcd.print("% ");
if (h<50){
stepperMotor();
}
void stepperMotor()
{
// Stepper motor initialization
const int stepsPerRevolution = 48;
Stepper stepper(stepsPerRevolution, 8,9,10,11);
int stepCount = 0;
stepper.setSpeed(60);
stepper.step(stepsPerRevolution);
stepCount++;
}
@unsupported, why have you posted, without any explanation, a program in Reply #2 that seems to have nothing in common with the code in your Original Post or your question or my Reply #1.