Go Down

Topic: Stepper motor & Button & L293ne (Read 147 times) previous topic - next topic

2-W1ND

Mar 25, 2015, 03:09 pm Last Edit: Mar 26, 2015, 03:15 pm by 2-W1ND
I use two circuit from arduino button and arduino L293d h-birdge to control the stepper motor. These circuit are all connected to MEGA 2560.

There are two questions.

1. The stepper motor buzz when I connect MEGA to computer.
2. I want to control the value of 'const int stepsPerRevolution'. However, it seems not available for the function, Stepper xxx(int,pin1,pin2,pin3.pin4). Is there any way to control the rotating step of stepping motor as a variable? The value of step should be modified by another function.


Code: [Select]

#include <Stepper.h>
#include <SoftwareSerial.h>
#include <Wire.h>

const int buttonPin = 2; // define which pin is for button
int ButtonState = 0; // set buttonstate as low
long Time = 0;
long Debounce = 200;

const byte cmmd = 32;
char ReceivedChars[cmmd];

char StepBuff[10];
const int Step0 = 200;
const int Step1 = 200;
char Step00;
char Step01;

boolean NewDataFlag = false;
boolean MappingFlag = false;
boolean IFlag = true;
boolean FFlag = false;

Stepper Stepper0(Step0, 4,5,6,7);
Stepper Stepper1(Step1, 8,9,12,13);
SoftwareSerial Bluetooth(10,11); // 10 for RX; 11 for TX

void setup()
{
  Serial.begin(9600); //Arduino starting baud rate 9600
  Bluetooth.begin(9600); //for JY-MCU HC6 bluetooth baud rate 9600
  pinMode(buttonPin, INPUT);
  Stepper1.setSpeed(60);
  Serial.begin(9600);
  Serial.println("<Ardunio & Display is ready>");
}

void loop()
{
  if (Bluetooth.available() > 0)
  {
  receiveNsave();
  ShowData();
  }
 
  if (MappingFlag == true)
  {
    MappingChars();
    LoadingDegree();
    MappingFlag = false;
    Serial.print("The value of StepBuff is ");
    Serial.println(StepBuff);
    Serial.println(Step00);
  }
 
  CheckButton();
}

void receiveNsave()
{
static boolean ReceivingFlag = false;
static byte CountNum = 0;
char StartMarker = '<';
char EndMarker = '>';
char RC;

        while (Bluetooth.available() > 0 && NewDataFlag == false) // check any data received
        {
RC = Bluetooth.read();

if (ReceivingFlag == true)
                {
if (RC != EndMarker)
                        {
ReceivedChars[CountNum] = RC;
CountNum++;
if (CountNum >= cmmd)
                                {
CountNum = cmmd - 1;
}
}
else
                        {
ReceivedChars[CountNum] = '\0'; // terminate the string
ReceivingFlag = false;
CountNum = 0;
NewDataFlag = true;
}
}

else if (RC == StartMarker)
                {
ReceivingFlag = true;
}
}
}
void ShowData()
{
       
if (NewDataFlag == true)
        {
Serial.print("Received characters are : ");
Serial.println(ReceivedChars);
NewDataFlag = false;
                MappingFlag = true;
}
}
void MappingChars()
{
   int j ;
   for(int i = 0; i < 2 ; ++i)
   {
      j = i*2 ;
      if (ReceivedChars[i] == 'a')
        {
           StepBuff[j] = '4';

           StepBuff[j+1] = '0';

         }
       else
       if (ReceivedChars[i] == 'b')
        {
            StepBuff[j] = '3';

            StepBuff[j+1] = '0';

        }
}
}
void LoadingDegree()
{
    for(int k=0; k<4; k++)
    {
      if (StepBuff[k] == 0 && k == 0)
      {
        Step00 = StepBuff[k];
      }
      if (StepBuff[k] == 4 && k == 0)
      {
        Step00 = StepBuff[k];
      }
    }
}
void CheckButton()
{
  ButtonState = digitalRead(buttonPin);
 
  if (ButtonState == HIGH && IFlag == true && millis() - Time > Debounce)
  {
    irotate();
  }
  else if (ButtonState == HIGH && FFlag == true && millis() - Time > Debounce)
  {
    frotate();
  }
  else
  {
  }
}
void irotate()
{
  Serial.println("clockwise");
  if (Step00 == '4')
  {
  Stepper0.step(25*4);
  }
  else if (Step00 == '0')
  {
  Stepper0.step(0);
  }
  else if (Step00 == '3')
  {
  Stepper0.step(25*3);
  }
  delay(500);

  IFlag = false;
  FFlag = true;
}
void frotate()
{
  Serial.println("counterclockwise");
  if (Step00 == '4')
  {
  Stepper0.step(-25*4);
  }
  else if (Step00 == '0')
  {
  Stepper0.step(0);
  }
  else if (Step00 == '3')
  {
  Stepper0.step(-25*3);
  }
  delay(500);

  IFlag = true;
  FFlag = false;
}

Go Up