Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Stepper 5v 28YBJ-48 Stepper Motor

Stepper Motor

  • Please log in to reply
1 reply to this topic

#1 mr_squall

mr_squall

    New Member

  • Members
  • Pip
  • 1 posts

Posted 30 July 2014 - 07:30 AM

Hi! I will share working code for this step motor.

Class:

 

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace RadugaParnik
{
    public class StepMotorService
    {
        private OutputPort _pin0;
        private OutputPort _pin1;
        private OutputPort _pin2;
        private OutputPort _pin3;

        public StepMotorService(Cpu.Pin pin0, Cpu.Pin pin1, Cpu.Pin pin2, Cpu.Pin pin3)
        {
            _pin0 = new OutputPort(pin0, false);
            _pin1 = new OutputPort(pin1, false);
            _pin2 = new OutputPort(pin2, false);
            _pin3 = new OutputPort(pin3, false);
        }

        public void MoveForward(int stepsCount, bool fourSterp=false)
        {
            if (fourSterp)
            {
                Move4Step(true, stepsCount);
            }
            else
            {
               Move(true, stepsCount);
            }

        }

        public void MoveBackward(int stepsCount,bool fourStep = false)
        {
            if (fourStep)
            {
                Move4Step(false, stepsCount);
            }
            else
            {
                Move(false, stepsCount);
            }
        }

        private void Move(bool forward, int stepsCount)
        {
            int resCount = stepsCount*7;
            int step = 0;
            bool dir = !forward;

            for (int i = 0; i < resCount; i++)
            {
                switch (step)
                {
                    case 0:
                        _pin0.Write(false);
                        _pin1.Write(false);
                        _pin2.Write(false);
                        _pin3.Write(true);
                        break;
                    case 1:
                        _pin0.Write(false);
                        _pin1.Write(false);
                        _pin2.Write(true);
                        _pin3.Write(true);
                        break;
                    case 2:
                        _pin0.Write(false);
                        _pin1.Write(false);
                        _pin2.Write(true);
                        _pin3.Write(false);
                        break;
                    case 3:
                        _pin0.Write(false);
                        _pin1.Write(true);
                        _pin2.Write(true);
                        _pin3.Write(false);
                        break;
                    case 4:
                        _pin0.Write(false);
                        _pin1.Write(true);
                        _pin2.Write(false);
                        _pin3.Write(false);
                        break;
                    case 5:
                        _pin0.Write(true);
                        _pin1.Write(true);
                        _pin2.Write(false);
                        _pin3.Write(false);
                        break;
                    case 6:
                        _pin0.Write(true);
                        _pin1.Write(false);
                        _pin2.Write(false);
                        _pin3.Write(false);
                        break;
                    case 7:
                        _pin0.Write(true);
                        _pin1.Write(false);
                        _pin2.Write(false);
                        _pin3.Write(true);
                        break;
                    default:
                        _pin0.Write(false);
                        _pin1.Write(false);
                        _pin2.Write(false);
                        _pin3.Write(false);
                        break;
                }
                if (dir)
                {
                    step++;
                }
                else
                {
                    step--;
                }
                if (step > 7)
                {
                    step = 0;
                }
                if (step < 0)
                {
                    step = 7;
                }
                Thread.Sleep(1);
            }

            // ??????? ??? ? ?????
            _pin0.Write(false);
            _pin1.Write(false);
            _pin2.Write(false);
            _pin3.Write(false);
        }
    }
}
 

 

Usage:

StepMotorService motorServ = new StepMotorService(Pins.GPIO_PIN_D4,
                Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D7);

 

motorServ.MoveBackward(2000);
motorServ.MoveForward(5000)



#2 Rx7man

Rx7man

    New Member

  • Members
  • Pip
  • 8 posts
  • LocationInterior BC, Canada

Posted 09 May 2015 - 11:47 PM

Here's something (in VB) that will neaten the step logic coding.. it does it in halfsteps (which are the odd steps) or full steps if you skip the odds... Someone with more expertise than me can tell me if it's quicker.

 output.PhaseA = (stepnumber + 1) Mod 8 < 3
 output.PhaseB = (stepnumber + 5) Mod 8 < 3
 output.PhaseC = (stepnumber + 7) Mod 8 < 3
 output.PhaseD = (stepnumber + 3) Mod 8 < 3

outputs...  Listed as phase A, B, C, D

 

Step number : 0
True    False    False    False
Step number : 1
True    False    True    False
Step number : 2
False    False    True    False
Step number : 3
False    True    True    False
Step number : 4
False    True    False    False
Step number : 5
False    True    False    True
Step number : 6
False    False    False    True
Step number : 7
True    False    False    True


I'm at the point on this learning curve where I have more questions than I do answers.. sorry about that!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.