Ok. I thought You would see what's missing so I tried to safe code scrolling.
//#include
#define fwd HIGH
#define bwd LOW
#define motorX 1
#define motorY 2
#define motorZ 3
void setup()
{
// put your setup code here, to run once:
pinMode(2, OUTPUT); //can be used as x-step
pinMode(3, OUTPUT); //y-step
pinMode(4, OUTPUT); //z-step
pinMode(5, OUTPUT); //can be used as x-dir
pinMode(6, OUTPUT); //y-dir
pinMode(7, OUTPUT); //z-dir
pinMode(8, OUTPUT); //enable active low
pinMode(13, OUTPUT);
digitalWrite(8, LOW);//enable motorpower
Serial.begin(9600);
}
//void stepmotor(int, int);
void stepmotor(int (motor), int(dir), int (dist))
{
if ( dir == fwd )
{
Serial.print("Fwd motor ");
Serial.println(motor);
}
else
{
Serial.print("Bwd motor ");
Serial.println(motor);
}
switch (motor)
{
case motorX:
{
digitalWrite( 5, dir);//digital dir does not owerride analoWrite(5, dir)
delayMicroseconds(100);
break;
}
case motorY:
{
digitalWrite( 6, dir);
delayMicroseconds(100);
break;
}
case motorZ:
{
digitalWrite( 7, dir);
delayMicroseconds(100);
break;
}
}
digitalWrite(8, LOW);//enable motorpower
for (int count = 0; count < dist; count++)
{
if (count == 7999) Serial.println(" 8000 ");
else if (count == 15999) Serial.println("16000 ");
else if (count == 23999) Serial.println("24000 ");
else if (count == 31999) Serial.println("32000 ");
if (motor == motorX )
{
digitalWrite( 2, HIGH);// digital x-step
delayMicroseconds(200);
digitalWrite( 2, LOW);// digital x-step
delayMicroseconds(200);
}
if (motor == motorY)
{
digitalWrite( 3, LOW);
delayMicroseconds(200);
digitalWrite( 3, HIGH);
delayMicroseconds(200
);
}
if (motor == motorZ)
{
digitalWrite( 4, LOW);
delayMicroseconds(200);
digitalWrite( 4, HIGH);
delayMicroseconds(200);
}
}
delay(1000);
digitalWrite(8, HIGH);//enable motorpower
}
void loop() {
// put your main code here, to run repeatedly:
// wait for command to start
while (!Serial.available()) {};
int dummy = Serial.read();// read command char
// analogWrite( 0, Low);//tst
// analogWrite( 1, Low);//tst
// analogWrite( 2, Low);// x-step analog KLAR
// analogWrite( 3, Low);// y-step analog KLAR
// analogWrite( 4, Low);// z-step analog¨KLAR
// analogWrite( 5, Low);// x-dir analog KLAR
// digitalWrite(2, LOW);// x-step digitalt
// digitalWrite(3, LOW);// y-step digitalt
// digitalWrite(4, LOW);// z-step digitalt
// digitalWrite(5,LOW); // x-dir digitalt
// digitalWrite(6,HIGH);// y-dir KLAR HIGH == CW LOW == CCW
// digitalWrite(7,HIGH);// z-dir KLAR HIGH == CW LOW == CCW KLAR
// digitalWrite(8,LOW);//
// digitalWrite(9,LOW);//
// digitalWrite(10,LOW);//
// digitalWrite(11,LOW);//
// digitalWrite(12,LOW);
stepmotor( motorX, fwd, 40000 );
delay(1000);
stepmotor( motorY, fwd, 40000 );
digitalWrite(13, LOW);
delay(1000);
// analogWrite( 0, High);//
// analogWrite( 1, High);//
// analogWrite( 2, High);//x-channel KLAR
// analogWrite( 3, High);//y-channel KLAR
// analogWrite( 4, High);//z-channel KLAR
// analogWrite( 5, High);//x-dir KLAR
// digitalWrite(2, HIGH); //x-step!!!
// digitalWrite(3, HIGH); //y-step!!!
// digitalWrite(4, HIGH); //z-step!!!
// digitalWrite(5, HIGH); //X-DIR CW ON HIGH CCW ON LOW
// digitalWrite(6, HIGH); //Y-DIR CW ON HIGH CCW ON LOW KLAR
// digitalWrite(7, LOW); //Z-DIR CW ON HIGH, CCW ON LOW KLAR
// digitalWrite(8,HIGH);//
// digitalWrite(9,HIGH);//
// digitalWrite(10,HIGH);//
// digitalWrite(11,HIGH);//
// digitalWrite(12,HIGH);//
stepmotor( motorX, bwd, 4000 );// går finfint +200, -200 steg
delay(1000);
stepmotor( motorY, bwd, 4000 );// går finfint +200, -200 steg
digitalWrite(13, HIGH); //
delay(1000);
}
groundFungus:
Have a look at Robin2's Serial input basics tutorial to see how to reliably read serial data in a non-blocking fashion.