Announcement

Collapse
No announcement yet.

4d Display controling a stepper motor with arduino

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 4d Display controling a stepper motor with arduino

    Hi,

    I doing a project where I have a 43PT-AR display controlling a stepper motor with a arduino and motor shield. To control the stepper motor I have a slider on the display which should control the position of the stepper motor. I'm having problems trying to write the arduino program in which it controls the motor. I got the display to show the value of the slider but I need to get that value sent to the stepper. Right now I'm getting an error that says 'class Stepper' has no member named 'write'. If anyone has any tips on has to get the display to control the stepper with the slider it would be greatly appreciated.

    Thanks








    #include <Stepper.h>

    #include <genieArduino.h>

    const int stepsPerRevolution = 100;
    Stepper myStepper(stepsPerRevolution, 12,13);


    // give the motor control pins names:
    const int pwmA = 3;
    const int pwmB = 11;
    const int brakeA = 9;
    const int brakeB = 8;
    const int dirA = 12;
    const int dirB = 13;


    int Slider0 ;
    int Stepperposition ;


    Genie genie;
    #define RESETLINE 4 // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
    void setup()
    {

    Serial.begin(9600); //
    genie.Begin(Serial); // Use Serial0 for talking to the Genie Library, and to the 4D Systems display

    genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

    pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
    digitalWrite(RESETLINE, 1); // Reset the Display via D4
    delay(100);
    digitalWrite(RESETLINE, 0); // unReset the Display via D4

    delay (3500); //let the display start up after the reset (This is important)

    }

    void loop()
    {
    static long waitPeriod = millis();
    myStepper.write(stepperposition);//


    genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

    if (millis() >= waitPeriod)
    {
    waitPeriod = millis() + 50; // rerun this code to update Cool Gauge and Slider in another 50ms time.
    }
    }




    void myGenieEventHandler(void)
    {
    genieFrame Event;
    genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below

    int slider_val = 0;

    //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)
    if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
    {
    if (Event.reportObject.object == GENIE_OBJ_SLIDER) // If the Reported Message was from a Slider
    {
    if (Event.reportObject.index == 0) // If Slider0 (Index = 0)
    {
    slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
    genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0
    Stepperposition = genie.GetEventData(&Event);

    }
    }
    }
    }



  • #2
    stepperposition vs Stepperposition, be careful as capitals do matter in programming

    Comment


    • #3
      Jacob,

      Please use myStepper.step(Stepperposition);

      instead of myStepper.write(stepperposition);

      Best regards,

      Comment


      • Jacob
        Jacob commented
        Editing a comment
        Thank you for you help, I changed it to myStepper.step and no longer get any errors. I uploaded the program and when I move the slider it still doesn't move the stepper motor. I was wondering you had any ideas on how to get the stepper to rotate from the slider.

        Thanks



        #include <Stepper.h>

        #include <genieArduino.h>

        const int stepsPerRevolution = 100;
        Stepper myStepper(stepsPerRevolution, 12,13);


        // give the motor control pins names:
        const int pwmA = 3;
        const int pwmB = 11;
        const int brakeA = 9;
        const int brakeB = 8;
        const int dirA = 12;
        const int dirB = 13;


        int Slider0 ;
        int Stepperposition ;


        Genie genie;
        #define RESETLINE 4 // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
        void setup()
        {

        Serial.begin(9600); //
        genie.Begin(Serial); // Use Serial0 for talking to the Genie Library, and to the 4D Systems display

        genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

        pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
        digitalWrite(RESETLINE, 1); // Reset the Display via D4
        delay(100);
        digitalWrite(RESETLINE, 0); // unReset the Display via D4

        delay (3500); //let the display start up after the reset (This is important)

        }

        void loop()
        {
        static long waitPeriod = millis();
        myStepper.step(Stepperposition);//


        genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

        if (millis() >= waitPeriod)
        {
        waitPeriod = millis() + 50; // rerun this code to update Cool Gauge and Slider in another 50ms time.
        }
        }




        void myGenieEventHandler(void)
        {
        genieFrame Event;
        genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below

        int slider_val = 0;

        //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)
        if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
        {
        if (Event.reportObject.object == GENIE_OBJ_SLIDER) // If the Reported Message was from a Slider
        {
        if (Event.reportObject.index == 0) // If Slider0 (Index = 0)
        {
        slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
        genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0
        Stepperposition = genie.GetEventData(&Event);

        }
        }
        }
        }

    • #4
      Hi Jacob,

      It looks to me that you are using the genie.GetEventData(&Event) once to get the slider_val and the calling it again for the Stepperposition when you already have the slider_val.

      slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
      genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0
      Stepperposition = genie.GetEventData(&Event);

      Maybe it should be like this,

      slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
      Stepperposition = slider_val;
      genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0

      I am not able to test it, but it might be worth a try.

      I hope this helps

      Best regards

      Paul

      Comment


      • Jacob
        Jacob commented
        Editing a comment
        Hi Paul,

        Thank you for your suggestion. I made the change and now when I move the slider, the motor moves a back and forth. So i'm still trying to make the motor do one full revolution when I move the slider up and the motor should move back when the slider is moved down. I you have any suggestions on how to make that possible I would appreciate it.

        Thanks



        #include <Stepper.h>

        #include <genieArduino.h>

        const int stepsPerRevolution = 100;
        Stepper myStepper(stepsPerRevolution, 12,13);


        // give the motor control pins names:
        const int pwmA = 3;
        const int pwmB = 11;
        const int brakeA = 9;
        const int brakeB = 8;
        const int dirA = 12;
        const int dirB = 13;


        int Slider0 ;
        int Stepperposition ;


        Genie genie;
        #define RESETLINE 4 // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
        void setup()
        {

        Serial.begin(9600); //
        pinMode(pwmA, OUTPUT);
        pinMode(pwmB, OUTPUT);
        pinMode(brakeA, OUTPUT);
        pinMode(brakeB, OUTPUT);
        digitalWrite(pwmA, HIGH);
        digitalWrite(pwmB, HIGH);
        digitalWrite(brakeA, LOW);
        digitalWrite(brakeB, LOW);

        genie.Begin(Serial); // Use Serial0 for talking to the Genie Library, and to the 4D Systems display

        genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

        pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
        digitalWrite(RESETLINE, 1); // Reset the Display via D4
        delay(100);
        digitalWrite(RESETLINE, 0); // unReset the Display via D4

        delay (3500); //let the display start up after the reset (This is important)

        }

        void loop()
        {
        static long waitPeriod = millis();
        myStepper.step(Stepperposition);//


        genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

        if (millis() >= waitPeriod)
        {
        waitPeriod = millis() + 50; // rerun this code to update Cool Gauge and Slider in another 50ms time.
        }
        }




        void myGenieEventHandler(void)
        {
        genieFrame Event;
        genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below

        int slider_val = 0;

        //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)
        if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
        {
        if (Event.reportObject.object == GENIE_OBJ_SLIDER) // If the Reported Message was from a Slider
        {
        if (Event.reportObject.index == 0) // If Slider0 (Index = 0)
        {
        slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
        Stepperposition = slider_val;
        genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0

        }
        }
        }
        }

    • #5
      Hi Jacob,

      To me everything looks right. If your stepper motor is 100 steps per revolution and your slider range is 0 to 100 then it should do a full revolution when the slider is at it's maximum. The only reason I could see that not working is if your slider wasn't from 0 to 100 or your stepper motor actually had more than 100 steps per revolution. How far around does the stepper motor move?.

      Best regards

      Paul

      Comment


      • Jacob
        Jacob commented
        Editing a comment
        Hi Paul,

        So when I move the slider the LED digits on the screen move from 0-100 but the the stepper motor only moves a few steps. When the slider is being moved the stepper will move one step forward and then it will move back. So when the slider is gets to 100 the stepper didn't rotate any.

    • #6
      Hi Jacob,

      We could try moving the myStepper.step out of the loop and put it in the events code in case it doesn't like constantly having a new step every loop.


      #include <Stepper.h>

      #include <genieArduino.h>

      const int stepsPerRevolution = 100;
      Stepper myStepper(stepsPerRevolution, 12,13);


      // give the motor control pins names:
      const int pwmA = 3;
      const int pwmB = 11;
      const int brakeA = 9;
      const int brakeB = 8;
      const int dirA = 12;
      const int dirB = 13;


      int Slider0 ;
      int Stepperposition ;


      Genie genie;
      #define RESETLINE 4 // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
      void setup()
      {

      Serial.begin(9600); //
      pinMode(pwmA, OUTPUT);
      pinMode(pwmB, OUTPUT);
      pinMode(brakeA, OUTPUT);
      pinMode(brakeB, OUTPUT);
      digitalWrite(pwmA, HIGH);
      digitalWrite(pwmB, HIGH);
      digitalWrite(brakeA, LOW);
      digitalWrite(brakeB, LOW);

      genie.Begin(Serial); // Use Serial0 for talking to the Genie Library, and to the 4D Systems display

      genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

      pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
      digitalWrite(RESETLINE, 1); // Reset the Display via D4
      delay(100);
      digitalWrite(RESETLINE, 0); // unReset the Display via D4

      delay (3500); //let the display start up after the reset (This is important)

      }

      void loop()
      {
      static long waitPeriod = millis();



      genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

      if (millis() >= waitPeriod)
      {
      waitPeriod = millis() + 50; // rerun this code to update Cool Gauge and Slider in another 50ms time.
      }
      }




      void myGenieEventHandler(void)
      {
      genieFrame Event;
      genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below

      int slider_val = 0;

      //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)
      if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
      {
      if (Event.reportObject.object == GENIE_OBJ_SLIDER) // If the Reported Message was from a Slider
      {
      if (Event.reportObject.index == 0) // If Slider0 (Index = 0)
      {
      slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
      Stepperposition = slider_val;
      myStepper.step(Stepperposition);// **** Moved to Events****
      genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0

      }
      }
      }
      }


      This way it will only move on an event. Not really sure if it will make a difference though but it would be good practice to only update the stepper motor if there is a new value.

      Best regards

      Paul

      Comment


      • #7
        Hi Jacob,

        I saw this command on a stepper motor sketch in setup.

        myStepper.setSpeed(60);

        Also the stepper motor may be cumulative steps so if you move the slider to 50 then the stepper moves 50, if you move the slider to 70 it might want to move another 70 steps. If this is the case then we would have to get the difference between slider positions. We could do that by creating another variable called lastStepperposition and having the line,

        slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
        Stepperposition = slider_val;
        myStepper.step(Stepperposition - lastStepperposition);// **** Moved to Events****
        lastStepperposition = Stepperposition;
        genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0

        The example I saw had to -200 steps to get back to zero.

        https://www.arduino.cc/en/Tutorial/StepperOneRevolution


        I hope this helps

        Best regards

        Paul

        Comment


        • Jacob
          Jacob commented
          Editing a comment
          Hi Paul,

          Thank you for all of you help, by adding myStepper.setSpeed(60) and moving the myStepper.step out of the loop I got the stepper to rotate. I tried using the code you posted above so the when the slider is move from 80 to 81 it will move one step instead of 81 but it didn't not work. Also when I move the slider back down the motor rotates in the same direction. If you have any suggestions on how the fix these problems it would be greatly appreciated.

          Thanks



          #include <Stepper.h>

          #include <genieArduino.h>

          const int stepsPerRevolution = 100;
          Stepper myStepper(stepsPerRevolution, 12,13);


          // give the motor control pins names:
          const int pwmA = 3;
          const int pwmB = 11;
          const int brakeA = 9;
          const int brakeB = 8;
          const int dirA = 12;
          const int dirB = 13;


          int Slider0 ;
          int Stepperposition ;
          int lastStepperposition ;

          Genie genie;
          #define RESETLINE 4 // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
          void setup()
          {

          Serial.begin(9600); //
          pinMode(pwmA, OUTPUT);
          pinMode(pwmB, OUTPUT);
          pinMode(brakeA, OUTPUT);
          pinMode(brakeB, OUTPUT);
          digitalWrite(pwmA, HIGH);
          digitalWrite(pwmB, HIGH);
          digitalWrite(brakeA, LOW);
          digitalWrite(brakeB, LOW);

          genie.Begin(Serial); // Use Serial0 for talking to the Genie Library, and to the 4D Systems display

          genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

          pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
          digitalWrite(RESETLINE, 1); // Reset the Display via D4
          delay(100);
          digitalWrite(RESETLINE, 0); // unReset the Display via D4

          delay (3500); //let the display start up after the reset (This is important)
          myStepper.setSpeed(50);
          }

          void loop()
          {
          static long waitPeriod = millis();



          genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

          if (millis() >= waitPeriod)
          {
          waitPeriod = millis() + 50; // rerun this code to update Cool Gauge and Slider in another 50ms time.
          }
          }




          void myGenieEventHandler(void)
          {
          genieFrame Event;
          genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below

          int slider_val = 0;

          //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)
          if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
          {
          if (Event.reportObject.object == GENIE_OBJ_SLIDER) // If the Reported Message was from a Slider
          {
          if (Event.reportObject.index == 0) // If Slider0 (Index = 0)
          {
          slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
          Stepperposition = slider_val;
          myStepper.step(Stepperposition);// **** Moved to Events****
          genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0
          slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0

          slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
          Stepperposition = slider_val;
          myStepper.step(Stepperposition - lastStepperposition);// **** Moved to Events****
          lastStepperposition = Stepperposition;
          genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0
          }
          }
          }
          }

      • #8
        Jacob,

        I notice you have set the pins of the motor driver you are using such as PWM, BRAKE and DIR. However it is not used in the Stepper Library.
        It is possible your wiring is not correct for the Stepper Library.

        Please check this link:
        https://www.arduino.cc/en/Reference/StepperConstructor

        https://www.arduino.cc/en/Reference/...nipolarCircuit

        https://www.arduino.cc/en/Reference/...BipolarCircuit

        Best regards,

        Comment


        • #9
          Hi Jacob,

          After checking the above, and as it performed better after setting the RPM, we could try putting this line,

          myStepper.step(Stepperposition - lastStepperposition);// **** Moved to Events****

          back to

          myStepper.step(Stepperposition);// **** Moved to Events****

          Just to see what happens.

          Best regards

          Paul

          Comment


          • #10
            Hi Jacob,

            Don't try what I said above. Leave the line as it is, that would be the right way to do it, It looks like it is accepting the value of -1 from 81 to 80 but the direction is wrong.

            Best regards

            Paul

            Comment


            • Jacob
              Jacob commented
              Editing a comment
              Hi Paul,

              Thank you for all of your help. I finally got the the stepper to do one full revolution forward and back with the slider.

              Thanks


              #include <Stepper.h>

              #include <genieArduino.h>

              const int stepsPerRevolution = 100;
              Stepper myStepper(stepsPerRevolution, 12,13);


              // give the motor control pins names:
              const int pwmA = 3;
              const int pwmB = 11;
              const int brakeA = 9;
              const int brakeB = 8;
              const int dirA = 12;
              const int dirB = 13;


              int Slider0 ;
              int Stepperposition ;
              int lastStepperposition ;

              Genie genie;
              #define RESETLINE 4 // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
              void setup()
              {

              Serial.begin(9600); //
              pinMode(pwmA, OUTPUT);
              pinMode(pwmB, OUTPUT);
              pinMode(brakeA, OUTPUT);
              pinMode(brakeB, OUTPUT);
              digitalWrite(pwmA, HIGH);
              digitalWrite(pwmB, HIGH);
              digitalWrite(brakeA, LOW);
              digitalWrite(brakeB, LOW);

              genie.Begin(Serial); // Use Serial0 for talking to the Genie Library, and to the 4D Systems display

              genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

              pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
              digitalWrite(RESETLINE, 1); // Reset the Display via D4
              delay(100);
              digitalWrite(RESETLINE, 0); // unReset the Display via D4

              delay (3500); //let the display start up after the reset (This is important)
              myStepper.setSpeed(50);
              }

              void loop()
              {
              static long waitPeriod = millis();



              genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

              if (millis() >= waitPeriod)
              {
              waitPeriod = millis() + 50; // rerun this code to update Cool Gauge and Slider in another 50ms time.
              }
              }




              void myGenieEventHandler(void)
              {
              genieFrame Event;
              genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below

              int slider_val = 0;

              //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)
              if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
              {
              if (Event.reportObject.object == GENIE_OBJ_SLIDER) // If the Reported Message was from a Slider
              {
              if (Event.reportObject.index == 0) // If Slider0 (Index = 0)
              {


              slider_val = genie.GetEventData(&Event); // Receive the event data from the Slider0
              Stepperposition = slider_val;
              myStepper.step(Stepperposition - lastStepperposition);// **** Moved to Events****
              lastStepperposition = Stepperposition;
              genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, slider_val); // Write Slider0 value to to LED Digits 0
              }
              }
              }
              }
          Working...
          X