Stepper motor control problem

Hi. I am doing the project about stepper motor. It includes a button, L293NE and Bluetooth module.

As the reference, I just combine the circuit design form button and L293d half bridge.

Function: First, it receives signals from the Bluetooth module. And then, the stepper motor rotate once after click the button.

Problem: So far, bluetooth can do its work, but the stepper motor do not rotate & keeps buzzing.

The following code is the test that when the board receives the character a

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

const int buttonPin = 2; // define which pin is for button
const int ledPin =  13; // checking button
int JudgeNumber = 0; // for choosing motor
int buttonState = 0; 
const byte cmmd = 32;
char ReceivedChars[cmmd];
char StepBuff[20];
int Baseval = 25;
int Step0;
int Step1;

boolean NewDataFlag = false;
boolean MappingFlag = 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(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Stepper1.setSpeed(60);
  Serial.begin(9600);
  Serial.println("<Ardunio & Display is ready>");
}

void loop() 
{
  receiveNsave();
  ShowData();
  
  if (MappingFlag == true)
  {
    MappingChars();
    LoadingDegree();
  }
  else
  {
  }
  
  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()
{
  if (MappingFlag = true)
  {
  for(int i=0; i<5; i++)
  {
    if (ReceivedChars[i] = 'a')
    {
      StepBuff[i] = 4;
      i++;
      StepBuff[i] = 0;
      i--;
    }
    else
    {
      
    }
  }
  }
}
void LoadingDegree()
{
  if(MappingFlag = true)
  {
  for(int k=0; k<10; k++)
  {
    if (StepBuff[k] == 0 && k == 0)
    {
      Step0 = Baseval * StepBuff[k];
    }
    
    if (StepBuff[k] == 4 && k == 0)
    {
      Step0 = Baseval * StepBuff[k];
    }
  }
  MappingFlag = false;
  }
}
void CheckButton()
{
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH && JudgeNumber == 0) 
  { 
    digitalWrite(ledPin, HIGH);
    Serial.println(Step0);
    delay(500);
    irotate();
  } 
  else if (buttonState == HIGH && JudgeNumber == 1) 
  { 
    digitalWrite(ledPin, HIGH);
    delay(500);
    frotate();
  } 
  else 
  {
    digitalWrite(ledPin, LOW); 
  }
}
void irotate()
{
  Serial.println("clockwise");
  Stepper0.step(Step0);
  Stepper1.step(Step1);
  digitalWrite(ledPin, LOW);
  JudgeNumber++;
}
void frotate()
{
  Serial.println("counterclockwise");
  Stepper0.step(-Step0);
  Stepper1.step(-Step1);
  digitalWrite(ledPin, LOW);
  JudgeNumber--;
}
Stepper Stepper0(Step0, 4,5,6,7);
Stepper Stepper1(Step1, 8,9,12,13);

When u run this, both Step0, and Step1 are zero. I believe this is not correct.

This line is wrong - it should be ==

if (ReceivedChars[i] = 'a')

Also, why are you trying 5 different characters to find a single 'a'?

And, if receivedChars contains, for example "aaa" your code will apply the included code 3 times.

Also, why are you doing i++ and i-- to the variable that is the index of the FOR loop? You may get away with it, but it would be much safer (and less confusing) to use a different variable.

So much for a few things in your code that caught my eye.

Looking at the bigger picture, have you written a very simple program just to make the stepper move properly. If not, you should do that first. The motor problems may be due to electrical issues rather than your code. Have a look at stepper motor basics.

...R

Robin2:
This line is wrong - it should be ==

if (ReceivedChars[i] = 'a')

Also, why are you trying 5 different characters to find a single 'a'?

And, if receivedChars contains, for example "aaa" your code will apply the included code 3 times.

Also, why are you doing i++ and i-- to the variable that is the index of the FOR loop? You may get away with it, but it would be much safer (and less confusing) to use a different variable.

So much for a few things in your code that caught my eye.

Looking at the bigger picture, have you written a very simple program just to make the stepper move properly. If not, you should do that first. The motor problems may be due to electrical issues rather than your code. Have a look at stepper motor basics.

...R

Thanks for your advises. Of course I have written a very simple program for the stepper and it works.
I don't know why it cannot work when I combine everything together... :cry:

#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;
}

However, it still not workz.
It that any code I should change?

2-W1ND:
Of course I have written a very simple program for the stepper and it works.

Can you post the working program so we can see how you have integrated it in the non-working program?

...R