how to fix the stepper motor to run normally

I have a problem in my hardware in the form of RFID, stepper motor and several other devices, where when I run my device using RFID as a command to move it, but my stepper motor does not run normally every time I move for the second time and to normalize it again then I pressed the reset button on my Arduino Mega is there another solution to my problem?

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press “Ctrl + A”. This will select all the text.
  • Press “Ctrl + C”. This will copy the selected text to the clipboard.
  • In a forum reply here, click the “Reply” button.
  • click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum’s code tags markup to your reply.
  • Press “Ctrl + V”. This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it’s OK to add it as an attachment. After clicking the “Reply” button, you will see an “Attachments and other settings” link.

When your code requires a library that’s not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.


Please provide a detailed description of what you mean by “my stepper motor does not run normally”.

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <Ethernet.h> // Library Ethernet Shiled
#include <Stepper.h>

#define SS_PIN 53
#define RST_PIN 46
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
#define RELAY1 24

#define SERVO_PIN 3
Servo myservo1;
byte mac[] = {
  0x6F, 0xCE, 0xAB, 0x51, 0x14, 0x0e};
//byte serv[] = "192, 168, 43, 13";

byte ip [] = {192, 168, 3, 119}; // ip Ethernet
byte gateway [] = {192,168,3,100};
byte server[] = {192,168,3,10}; //IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
EthernetClient client(80);

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 36, 37, 38, 39);

void setup()
{
  // set the speed at 60 rpm:
 
  // initialize the serial port:
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, server);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("COFFEE MACHINE");
  myservo1.attach(42);
  myStepper.setSpeed(200);
}

void loop() 
{
   //Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent() ) {
    return;
  }
  //Select one of the cards
  if ( !mfrc522.PICC_ReadCardSerial() ) {
    return;
  }
  String RFID = "";
  byte letter;
  for ( byte i = 0; i < mfrc522.uid.size; i++ ) {
    RFID.concat(String(mfrc522.uid.uidByte[i], HEX));
    if ( i < mfrc522.uid.size - 1 ) RFID += "-";
  }

    RFID.toUpperCase();
    Serial.println();
    Serial.println("RFID :'" + RFID + "'");
    int rfid=((mfrc522.uid.uidByte[1])+(25112017));
    Serial.println(rfid);
     
if ( RFID == "E0-64-A-1B" ){
  
    Serial.println("Sugar Coffee");
    
    myStepper.step(1300);
    delay(2000);

    pinMode(RELAY1, OUTPUT);
    digitalWrite(RELAY1, LOW);    // turn on relay 1
    delay(5000);                            // wait 2 seconds
    digitalWrite(RELAY1, HIGH);  // turn off relay 1
    
    delay(3000);
    myStepper.step(-900);
    
    delay(3000);
    myservo1.write(15);
    delay(2000);
    myservo1.write(90);
    delay(2000);
   
    myStepper.step(-950);
    delay(2000);
        
    if (client.connect(server,80)) {
      client.print("GET /kopi/get_data.php?");
      client.print("rfid=");
      client.print(rfid);
      client.println(" HTTP/1.1");
      client.println("Host:192.168.3.10");
      client.println("Connection: close");
      client.println();
      Serial.println("connected");
      } else {
        Serial.println("Failed"); 
      }
    }

   if ( RFID == "29-CB-51-98" ){
    Serial.println("Black Coffee");
    
 
    myStepper.step(1300);
    delay(2000);


    pinMode(RELAY1, OUTPUT);
    digitalWrite(RELAY1, LOW);    // turn on relay 1
    delay(5000);                            // wait 2 seconds
    digitalWrite(RELAY1, HIGH);  // turn off relay 1
    delay(2000);
    
    myStepper.step(-1700);
    delay(2000);
    
    if (client.connect(server, 80)) {
      client.print("GET /kopi/get_data.php?");
      client.print("rfid=");
      client.print(rfid);
      client.println(" HTTP/1.1");
      client.println("Host:192.168.3.10");
      client.println("Connection: close");
      client.println();
      Serial.println("connected");
      } else {
         Serial.println("Failed");
      }
    }
    delay(500);
}

before, i think i must explain the problem and i mean “my stepper motor does not run normally”. is I use two RFID as a command for my tool, so i tap first RFID to the tag for running the tool of arduino, for the first time stepper run as a normally i mean is it’s work corresponding with a code but when i run for secondly with other RFID the stepper not run corresponding with a code example in a code i give 1300 but stepper motor just run a half or a quarter from code(1300) if i whan to run the stepper motor normally or a work coressponding wiith a code i must push a button reset in a arduino mega so from this can help me everybody?

You have myStepper.step(1300); in two places Which part of your code works and which does not?

Please be more specific about what happens when it goes wrong.

...R

i use two code for each RFID, first RFID i use for all tool to motion and for second RFID i use not all tool to motion, so it's code i use for each RFID and it's code coressponding with RFID, when i test with RFID it's work coressponding the code, but i must push the button reset in Arduino mega, my problem is when i run the all tool of Arduino with first RFID it's work but when i run for secondly it's not work or not coressponding with code. But when i push the button reset it's run normally, Is this the power that is the cause or the driver, till here can be understood? thank's for attention

newbie30:
i use two code for each RFID, first RFID i use for all tool to motion and for second RFID i use not all tool to motion, so it's code i use for each RFID and it's code coressponding with RFID, when i test with RFID it's work coressponding the code, but i must push the button reset in Arduino mega, my problem is when i run the all tool of Arduino with first RFID it's work but when i run for secondly it's not work or not coressponding with code. But when i push the button reset it's run normally, Is this the power that is the cause or the driver, till here can be understood? thank's for attention

Sorry but I just don't understand that and it does not seem to be an answer to the questions I asked in Reply #3

...R

Robin2:
Sorry but I just don't understand that and it does not seem to be an answer to the questions I asked in Reply #3

...R

all is work, when i have uploaded the code, my stepper motor not work when i run with RFID for secondly, so my tool can work just one time, when i run for secondly it's not work(stepper motor) just half or quarter from 1300 and 1300 it's in a code of Arduino

newbie30:
all is work, when i have uploaded the code, my stepper motor not work when i run with RFID for secondly, so my tool can work just one time, when i run for secondly it’s not work(stepper motor) just half or quarter from 1300 and 1300 it’s in a code of Arduino

Robin2:
You have myStepper.step(1300); in two places Which part of your code works and which does not?

Please be more specific about what happens when it goes wrong.

…R

when i want to run normally i must pust button in Arduino mega, so that i can run my tool normally i mean is coressponding with code

newbie30:
all is work, when i have uploaded the code, my stepper motor not work when i run with RFID for secondly,

Does that mean that you have two different programs, one of them works and one of them does not?

If English is not your first language maybe you would find it easier to get help on one of the other language sections of the Forum.

...R

I use two RFID as a command for my tool, so i tap first RFID to the tag for running the tool of arduino, for the first time stepper run as a normally i mean is it's work corresponding with a code but when i run for secondly with other RFID the stepper not run corresponding with a code example in a code i give 1300 but stepper motor just run a half or a quarter from code(1300)

Does it matter which runs first? Or is it that the first RFID tag always works properly, whichever one it is, and the second does not?

if ( RFID == "29-CB-51-98" ){
    Serial.println("Black Coffee");

if ( RFID == "E0-64-A-1B" ){
    Serial.println("Sugar Coffee");

Robin2:
Does that mean that you have two different programs, one of them works and one of them does not?

If English is not your first language maybe you would find it easier to get help on one of the other language sections of the Forum.

...R

all of them work when i have uploaded the code and when i run for fisrtly, but when i run for secondly my stepper don't work normally as a code (1300), and when i whan to run normally i must push the button in arduino mega

cattledog:
Does it matter which runs first? Or is it that the first RFID tag always works properly, whichever one it is, and the second does not?

if ( RFID == "29-CB-51-98" ){

Serial.println(“Black Coffee”);

if ( RFID == “E0-64-A-1B” ){
    Serial.println(“Sugar Coffee”);

Basically, my stepper will run according to the code if I reset the arduino button first, if you don’t press the reset button it will not run normally or run according to the code (1300), is this the problem driver or what?

newbie30:
all of them work when i have uploaded the code and when i run for fisrtly,

All of what?

What does "run for firstly" mean and how does it differ from the second run?

Sorting out a programming problem requires very precise attention to detail.

...R

Robin2:
All of what?

What does "run for firstly" mean and how does it differ from the second run?

Sorting out a programming problem requires very precise attention to detail.

...R

"run for firstly" is i'm running my tool all of Arduino mega like a stepper motor and motor servo and motor DC. it makes no difference only when I run using RFID for the second time the stepper motor does not run completely only half or a quarter of what was ordered in the code (1300)

Robin2:
All of what?

What does "run for firstly" mean and how does it differ from the second run?

Sorting out a programming problem requires very precise attention to detail.

...R

they are all my tools in the form of stepper motors, servo motors and DC motors, when I upload them to arduino mega, all those devices can run normally but when I run for the second time my stepper motor only runs (rotates) half or quarter of the code command (1300)

newbie30:
they are all my tools in the form of stepper motors, servo motors and DC motors, when I upload them to arduino mega,

You do not upload stepper motors, servos or DC motors to an Arduino. You upload programs.

I'm sorry but we don't seem to be able to communicate in a way that would enable me to help you.

...R

One thing I notice is that after you perform your operation your stepper motor is not returned to or set as the "zero" position. This is important because the stepper moves relative to the zero position and can cause problems like the one I believe you are describing

I am not familiar with the stepper.h library as I prefer the Accelstepper library. In Accelstepper there is a function to set the zero position which would be done after completing an operation.
If stepper.h does not have this function you will have to track the stepper position and make future moves relative to that.

Hutkikz:
One thing I notice is that after you perform your operation your stepper motor is not returned to or set as the "zero" position. This is important because the stepper moves relative to the zero position and can cause problems like the one I believe you are describing

I am not familiar with the stepper.h library as I prefer the Accelstepper library. In Accelstepper there is a function to set the zero position which would be done after completing an operation.
If stepper.h does not have this function you will have to track the stepper position and make future moves relative to that.

ok thank you for the answers and suggestions i will try it, I this highway can be the cause of my problems

Hi,
How are you powering and controlling the stepper?
How are you powering the servo?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
How are you powering and controlling the stepper?
How are you powering the servo?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile:

how to provide the picture, aside from providing a link, because I don’t know and there is no tool to upload the image