moving motor with switches

Hi all

I got a servo motor and I am trying to move him by clicking switches (pins 3, 4).

it moves clockwise when I click on switch pin 3.
and counterclockwise when I click pin 4.

the first problem - the motor moves twice and not one time.
second problem - only the led on pin 7 turn on when either switch pin 3 or 4 is clicked the third problem - when one of the switches is clicked and stay that way the motor keeps moving, I would like that the motor will move only when I release the switch and click on it again.

Thanks for the help :slight_smile:

#include <Stepper.h> //stepper motor library
#define STEPS 2038 //define 28byj-48 stepper motor full range
Stepper stepper(STEPS, 8, 9, 10, 11); //define arduino pins for stepper motor


int SWITCH_PIN[] = {3, 4}; //define arduino pins for switchs 
int LED_PIN[] = {6, 7}; //define arduino pins for leds
int CurrentSwitchPin; //initialization for switch status
int CurrentLedPin; //initialization for led status
int MotionStatus[] = {0, 0}; //initialization for motion 
int z = 1; // will define the direction of the motor

void setup() 
{
  Serial.begin(9600); // open serial to show prints
  for (int i=0 ; i < 2; i++)
  {
    pinMode(LED_PIN[i], OUTPUT); //define led pins as outputs
    pinMode(SWITCH_PIN[i], INPUT); //define switch pins as inputs
  }
    Serial.println("START!");
}


int MoveStepper(int Ledpin, int z)
{
    Serial.println("MoveStepper");
    digitalWrite(Ledpin, HIGH); // turn on led
    stepper.setSpeed(10); // set motor speed to 10 rmp
    stepper.step(255 * z);  // move the motor 1/4 of the range
    delay(500);
    digitalWrite(Ledpin, LOW); // turn off after completing the motion led
    z = 1;
    Serial.println("stepper stop moving");

}

void loop() 
{
    for (int x = 0; x < 2; x++) 
    {
       CurrentSwitchPin = SWITCH_PIN[x]; //put swith_pin status to the arry start with pin 3
       CurrentLedPin = LED_PIN[x];//put led_pin status to the arry start with pin 6
       MotionStatus[x] = digitalRead(CurrentSwitchPin); //Checking input switchs

    }

     if (MotionStatus[0] == HIGH && MotionStatus[1] == LOW) //checking if switch one is pressed
     {
          MoveStepper(CurrentLedPin , -z); //moves motor clockwise
          MotionStatus[0] = LOW;

     }

     if (MotionStatus[1] == HIGH && MotionStatus[0] == LOW) //checking if switch two is pressed
     {
          MoveStepper(CurrentLedPin, z);  //moves motor counterclockwise
          MotionStatus[1] = LOW;
     }
   
}

How are the switch pins wired? Post a small wiring.

I would like that the motor will move only when I release the switch and click on it again.

Look into the state change detection tutorial and my update that shows active low switches.

added

Hi,
Can you post a circuit diagram?
Just a hand drawn diagram showing power supply and labelled buttons etc would be fine.

It looks like your switches switch to gnd, do you have pullup resistors to pull the digital input HIGH when the switch is open?
OR
use pinMode(pinname, INPUT_PULLUP)
to turn the internal pullup resistor ON.

Thanks.. Tom.. :slight_smile:

here

TomGeorge:
use pinMode(pinname, INPUT_PULLUP)
to turn the internal pullup resistor ON.

that's fixed the first problem after i change it to GND and not the 5v output.
do you have any ideas of the other 2?
thank you :slight_smile:

Hi,
You are using a stepper motor, what are you using between the UNO and the stepper?
Please include it in your circuit along with how you are powering it.

Tom... :slight_smile:

the stepper motor is connected directly to the Arduino

i fixed the second problem, I don't know if it real fix or just a bypass....

added to the code:
--CurrentLedPin;
--CurrentSwitchPin;

#include <Stepper.h> //stepper motor library
#define STEPS 2038 //define 28byj-48 stepper motor full range
Stepper stepper(STEPS, 8, 9, 10, 11); //define arduino pins for stepper motor


int SWITCH_PIN[] = {3, 4}; //define arduino pins for switchs 
int LED_PIN[] = {6, 7}; //define arduino pins for leds
int CurrentSwitchPin; //initialiMotionDirectionation for switch status
int CurrentLedPin; //initialiMotionDirectionation for led status
int MotionStatus[] = {0, 0}; //initialiMotionDirectionation for motion 
int MotionDirection = 1; // will define the direction of the motor

void setup() 
{
  Serial.begin(9600); // open serial to show prints
  for (int i=0 ; i < 2; i++)
  {
    pinMode(LED_PIN[i], OUTPUT); //define led pins as outputs
    pinMode(SWITCH_PIN[i], INPUT_PULLUP); //define switch pins as inputs
  }
    Serial.println("START!");
}


int MoveStepper(int Ledpin, int MotionDirection)
{
    Serial.println("MoveStepper");
    digitalWrite(Ledpin, HIGH); // turn on led
    stepper.setSpeed(10); // set motor speed to 10 rmp
    stepper.step(255 * MotionDirection);  // move the motor 1/4 of the range
//    delay(500);
    digitalWrite(Ledpin, LOW); // turn off after completing the motion led
    MotionDirection = 1;
    Serial.println("stepper stop moving");

}

void loop() 
{
    for (int x = 0; x < 2; x++) 
    {
       CurrentSwitchPin = SWITCH_PIN[x]; //put swith_pin status to the arry start with pin 3
       CurrentLedPin = LED_PIN[x];//put led_pin status to the arry start with pin 6
       MotionStatus[x] = digitalRead(CurrentSwitchPin); //Checking input switchs

    }

     if (MotionStatus[0] == HIGH && MotionStatus[1] == LOW) //checking if switch one is pressed
     {
          MoveStepper(CurrentLedPin , MotionDirection); //moves motor counterclockwise
//          Serial.println(CurrentSwitchPin);
//          Serial.println(CurrentLedPin);
          MotionStatus[0] = LOW;

     }

     if (MotionStatus[1] == HIGH && MotionStatus[0] == LOW) //checking if switch two is pressed
     {
          --CurrentLedPin;
          --CurrentSwitchPin;
          MoveStepper(CurrentLedPin , -MotionDirection);  //moves motor clockwise
//          Serial.println(CurrentSwitchPin);
//          Serial.println(CurrentLedPin);
          MotionStatus[1] = LOW;
     }
   
}

An Arduino handles at the most 5 volt and 20 milliAmps. That makes no stepper happy.

Hi,
You need at least one of these for that stepper motor.
ULN2003 driver.


To make your motor only step once per press, you need to detect the pressing of the button, the transition from HIGH to LOW, not the fact that it is pressed LOW.

Using little 2 step for..loops is not really worth the effort when checking your buttons.

Tom.... :slight_smile:

hi,

i got the driver you said.
now the motor moves real slow...

#include <Stepper.h> //stepper motor library
#define STEPS 2038 //define 28byj-48 stepper motor full range
Stepper stepper(STEPS, 8, 9, 10, 11); //define arduino pins for stepper motor


int SWITCH_PIN[] = {3, 4}; //define arduino pins for switchs 
int LED_PIN[] = {6, 7}; //define arduino pins for leds
int CurrentSwitchPin; //initialiMotionDirectionation for switch status
int CurrentLedPin; //initialiMotionDirectionation for led status
int MotionStatus[] = {0, 0}; //initialiMotionDirectionation for motion 
int MotionDirection = 1; // will define the direction of the motor
int MotionPreviousStatus;

void setup() 
{
  Serial.begin(9600); // open serial to show prints
  for (int i=0 ; i < 2; i++)
  {
    pinMode(LED_PIN[i], OUTPUT); //define led pins as outputs
    pinMode(SWITCH_PIN[i], INPUT_PULLUP); //define switch pins as inputs
  }
    Serial.println("START!");
}


int MoveStepper(int Ledpin, int MotionDirection)
{
    Serial.println("MoveStepper");
    digitalWrite(Ledpin, HIGH); // turn on led
    stepper.setSpeed(10); // set motor speed to 10 rmp
    stepper.step(255 * MotionDirection);  // move the motor 1/4 of the range
//    delay(500);
    digitalWrite(Ledpin, LOW); // turn off after completing the motion led
    MotionDirection = 1;
    Serial.println("stepper stop moving");

}

void loop() 
{
    for (int x = 0; x < 2; x++) 
    {
       CurrentSwitchPin = SWITCH_PIN[x]; //put swith_pin status to the arry start with pin 3
       CurrentLedPin = LED_PIN[x];//put led_pin status to the arry start with pin 6
       MotionStatus[x] = digitalRead(CurrentSwitchPin); //Checking input switchs

    }
     if (MotionStatus[0] == HIGH && MotionStatus[1] == LOW && (MotionPreviousStatus != MotionStatus[0])) //checking if switch one is pressed
     {
          MoveStepper(CurrentLedPin , MotionDirection); //moves motor counterclockwise
          Serial.println(CurrentSwitchPin);
          Serial.println(CurrentLedPin);
          MotionPreviousStatus = MotionStatus[0];
      }
       else
       {
        Serial.println("Not moving1");
        Serial.println(MotionPreviousStatus);
       }

     if (MotionStatus[1] == HIGH && MotionStatus[0] == LOW && (MotionPreviousStatus != MotionStatus[1])) //checking if switch two is pressed
     {
          --CurrentLedPin;
          --CurrentSwitchPin;
          MoveStepper(CurrentLedPin , -MotionDirection);  //moves motor clockwise
          Serial.println(CurrentSwitchPin);
          Serial.println(CurrentLedPin);
          MotionPreviousStatus = MotionStatus[1];
       }
       else
       {
        Serial.println("Not moving2");
        Serial.println(MotionPreviousStatus);
       }
     }

it move ones and stop moving,
i know i have problem with MotionPreviousStatus but cant find where to put it...

thank you

Hi,
Can you post a circuit diagram of how you have connected it?

Thanks.. Tom.... :slight_smile:

added

so i think i did it :slight_smile:

#include <Stepper.h> //stepper motor library
#define STEPS 2038 //define 28byj-48 stepper motor full range
Stepper stepper(STEPS, 8, 9, 10, 11); //define arduino pins for stepper motor


int SWITCH_PIN[] = {3, 4}; //define arduino pins for switchs 
int LED_PIN[] = {6, 7}; //define arduino pins for leds
int CurrentSwitchPin; //initialiMotionDirectionation for switch status
int CurrentLedPin; //initialiMotionDirectionation for led status
int MotionStatus[] = {0, 0}; //initialiMotionDirectionation for motion 
int MotionDirection = 1; // will define the direction of the motor
int MotionPreviousStatusLeft;
int MotionPreviousStatusRight;

void setup() 
{
  Serial.begin(9600); // open serial to show prints
  for (int i=0 ; i < 2; i++)
  {
    pinMode(LED_PIN[i], OUTPUT); //define led pins as outputs
    pinMode(SWITCH_PIN[i], INPUT); //define switch pins as inputs
  }
  delay(60000);
    Serial.println("START!");
}


int MoveStepper(int Ledpin, int MotionDirection)
{
    Serial.println("MoveStepper");
    digitalWrite(Ledpin, HIGH); // turn on led
    stepper.setSpeed(10); // set motor speed to 10 rmp
    stepper.step(255 * MotionDirection);  // move the motor 1/4 of the range
    delay(6000);
    digitalWrite(Ledpin, LOW); // turn off after completing the motion led
    MotionDirection = 1;
    Serial.println("stepper stop moving");

}

void loop() 
{
    for (int x = 0; x < 2; x++) 
    {
       CurrentSwitchPin = SWITCH_PIN[x]; //put swith_pin status to the arry start with pin 3
       CurrentLedPin = LED_PIN[x];//put led_pin status to the arry start with pin 6
       MotionStatus[x] = digitalRead(CurrentSwitchPin); //Checking input switchs

    }
     if (MotionStatus[0] == HIGH && MotionStatus[1] == LOW && (MotionPreviousStatusLeft != MotionStatus[0])) //checking if switch one is pressed
     {
          MoveStepper(CurrentLedPin , MotionDirection); //moves motor counterclockwise
          MotionPreviousStatusLeft = MotionStatus[0]; //save the motion status state
      }
       else if (MotionStatus[0] == LOW)
       {
        MotionPreviousStatusLeft = LOW;
       }

     if (MotionStatus[1] == HIGH && MotionStatus[0] == LOW && (MotionPreviousStatusRight != MotionStatus[1])) //checking if switch two is pressed
     {
          --CurrentLedPin;
          --CurrentSwitchPin;
          MoveStepper(CurrentLedPin , -MotionDirection);  //moves motor clockwise
          MotionPreviousStatusRight = MotionStatus[1];
       }
       else if (MotionStatus[1] == LOW)
       {
        MotionPreviousStatusRight = LOW;
       }
     }

does it make sense?

thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.