Need help with Stepper Proyect

Hey guys, im complety new in this world. Just few days working hard studying this and i got my first proyect. Today i´ve been +12h working on it but i cant make it work. I dont even know if this is where i should ask because it is UnoArduSim.
The thing is that i got a Stepper motor that must be working from a button for run and other for stop. And it must work with serial too, with 'm' for on and 'p' for stop. And some LED for run and off.

#define MARCHA 2
#define PARO 3
#define LEDM 22
#define LEDP 24
boolean ESTADO = false;
int FIN;
void setup()
{
	pinMode(22, OUTPUT);
	pinMode(24, OUTPUT);
	pinMode(8, OUTPUT);
	pinMode(9, OUTPUT);
	pinMode(10, OUTPUT);
	pinMode(11, OUTPUT);
	pinMode(MARCHA, INPUT);
	pinMode(PARO, INPUT);
	Serial.begin(9600);
}

void loop()
{

	if (digitalRead(MARCHA)==HIGH)
	{	do
		{	digitalWrite(22, HIGH);
			digitalWrite(24, LOW);
			digitalWrite(8, HIGH);
			digitalWrite(9, LOW);
			digitalWrite(10, LOW);
			digitalWrite(11, LOW);
			delay(50);
			digitalWrite(8, LOW);
			digitalWrite(9, HIGH);
			digitalWrite(10, LOW);
			digitalWrite(11, LOW);
			delay(50);
			digitalWrite(8, LOW);
			digitalWrite(9, LOW);
			digitalWrite(10, HIGH);
			digitalWrite(11, LOW);
			delay(50);
			digitalWrite(8, LOW);
			digitalWrite(9, LOW);
			digitalWrite(10, LOW);
			digitalWrite(11, HIGH);
			delay(50);
		}
		while(digitalRead(PARO)==LOW);
	}
	else
	{
		digitalWrite(22, LOW);
		digitalWrite(24, HIGH);
	}
	if (Serial.available())
	{
		char c = Serial.read();
		if (c=='m')
		{
			Serial.print("Motor encendido");
			do
			{	
				digitalWrite(22, HIGH);
				digitalWrite(24, LOW);
				digitalWrite(8, HIGH);
				digitalWrite(9, LOW);
				digitalWrite(10, LOW);
				digitalWrite(11, LOW);
				delay(50);
				digitalWrite(8, LOW);
				digitalWrite(9, HIGH);
				digitalWrite(10, LOW);
				digitalWrite(11, LOW);
				delay(50);
				digitalWrite(8, LOW);
				digitalWrite(9, LOW);
				digitalWrite(10, HIGH);
				digitalWrite(11, LOW);
				delay(50);
				digitalWrite(8, LOW);
				digitalWrite(9, LOW);
				digitalWrite(10, LOW);
				digitalWrite(11, HIGH);
				delay(50);
			}
			while((digitalRead(PARO)==LOW)||(ESTADO=true)); 
		}
		else
		{
			Serial.print("Motor parado");
		}

		if (Serial.available())
		{
			char b = Serial.read();
			if (c=='p')
			{
				ESTADO = true;
			}

		}

	}

}

Thats what i got, i dont even know if this is the place but thank you.. and sorry if im wrong! :slight_smile:

I works everything, just the 'p' doesnt work... i cant stop it with the Serial

c or b?

read b and test c?

Still nothing, i dont know how to stop the dowhile loop with 'p'
So lost already..

Post your code as it looks now.

If you ever get into this loop you will never exit it:

			do
			{	
				digitalWrite(22, HIGH);
				digitalWrite(24, LOW);
				digitalWrite(8, HIGH);
				digitalWrite(9, LOW);
				digitalWrite(10, LOW);
				digitalWrite(11, LOW);
				delay(50);
				digitalWrite(8, LOW);
				digitalWrite(9, HIGH);
				digitalWrite(10, LOW);
				digitalWrite(11, LOW);
				delay(50);
				digitalWrite(8, LOW);
				digitalWrite(9, LOW);
				digitalWrite(10, HIGH);
				digitalWrite(11, LOW);
				delay(50);
				digitalWrite(8, LOW);
				digitalWrite(9, LOW);
				digitalWrite(10, LOW);
				digitalWrite(11, HIGH);
				delay(50);
			}
			while((digitalRead(PARO)==LOW)||(ESTADO=true)); 

This because you have:

while((digitalRead(PARO)==LOW)||(ESTADO=true));

rather than:

while((digitalRead(PARO)==LOW)||(ESTADO==true));

Your code could be much more efficient if you didn't use the while loops. Since loop() gets called over and over again all you need to do is check for switches and serial changes.

Should your code work when starting with a button and stopping with serial and vice-versa? If so, you have not coded it that way.

Try this:

const byte MARCHA = 2;
const byte  PARO = 3;
const byte  LEDM = 22;
const byte  LEDP = 24;

byte ESTADO = PARO;

void setup()
{
  Serial.begin(9600);

  pinMode(LEDM, OUTPUT);
  pinMode(LEDP, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(MARCHA, INPUT);
  pinMode(PARO, INPUT);
}

void marcha()
{
  digitalWrite(LEDM, HIGH);
  digitalWrite(LEDP, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  delay(50);
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  delay(50);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  delay(50);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  delay(50);
}

void paro()
{
  digitalWrite(LEDM, LOW);
  digitalWrite(LEDP, HIGH);
}

void loop()
{
  if (ESTADO == MARCHA)
    marcha();
  else
    paro();

  if (digitalRead(MARCHA) == HIGH)
    ESTADO = MARCHA;

  if (digitalRead(PARO) == HIGH)
    ESTADO = PARO;

  if (Serial.available())
  {
    char c = Serial.read();
    if (c == 'm')
      ESTADO = MARCHA;
    else if (c == 'p')
      ESTADO = PARO;
  }
}

This one is perfectly working! Thank you so much guys this helped me a lot and now i understand the error, such a pleasure :slight_smile: