control five step motor with five button

I need change my code serial to button control

if i press a 1st button, 1st motor have to rotate 45degree.
its same to other motors.

i have a serial control code but i don’t know what have to change for my purpose.

this is my serial control code

uint8_t step[4][4] = {{1, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 1}, {1, 0, 0, 1}};
uint8_t motor_step[5] = {0, 0, 0, 0, 0};
int16_t motor_count[5] = {0, 0, 0, 0, 0};

uint8_t parsing_step = 0;
uint8_t parsing_motor = 0;
int8_t parsing_pm = 0;
uint16_t parsing_num1 = 0;
uint16_t parsing_num2 = 0;

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

for (uint8_t i = 0; i < 5; i++)
{
pinMode(30 + 4 * i, OUTPUT);
pinMode(31 + 4 * i, OUTPUT);
pinMode(32 + 4 * i, OUTPUT);
pinMode(33 + 4 * i, OUTPUT);
}

Serial.println(“input command\nex)white,360*10\n”);
}

void loop()
{
if (Serial.available() != 0)
{
char temp = Serial.read();
switch (parsing_step)
{
case 0:
if (‘a’ <= temp && temp <= ‘z’)
{
switch (temp)
{
case ‘w’:
parsing_motor = 0;
break;
case ‘y’:
parsing_motor = 1;
break;
case ‘r’:
parsing_motor = 2;
break;
case ‘b’:
parsing_motor = 3;
break;
case ‘d’:
parsing_motor = 4;
break;
}
}
++parsing_step;
break;
case 1:
if (temp == ‘,’)
{
++parsing_step;
parsing_pm = 1;
parsing_num1 = 0;
parsing_num2 = 0;
}
else if (temp == ‘\n’)
{
parsing_step = 0;
}
break;
case 2:
if (temp == ‘-’)
{
parsing_pm = -1;
}
else if (‘0’ <= temp && temp <= ‘9’)
{
parsing_num1 = parsing_num1 * 10 + temp - ‘0’;
}
else if (temp == ‘*’)
{
++parsing_step;
}
else if (temp == ‘\n’)
{
motor_count[parsing_motor] += parsing_pm * (parsing_num1 / 1.8 + 0.5);
parsing_step = 0;
Serial.print("motor: “);
Serial.print(parsing_motor);
Serial.print(”, angle: ");
Serial.println(motor_count[parsing_motor]*1.8);
}
break;
case 3:
if (‘0’ <= temp && temp <= ‘9’)
{
parsing_num2 = parsing_num2 * 10 + temp - ‘0’;
}
else if (temp == ‘\n’)
{
motor_count[parsing_motor] += parsing_pm * (parsing_num1 * parsing_num2 / 1.8 + 0.5);
parsing_step = 0;
Serial.print("motor: “);
Serial.print(parsing_motor);
Serial.print(”, angle: ");
Serial.println(motor_count[parsing_motor]*1.8);
}
break;
}
}

for (uint8_t i = 0; i < 5; i++)
{
if (motor_count < 0)

  • {*
    ++motor_count*;
    digitalWrite(30 + 4 * i, step[motor_step][0]);
    digitalWrite(31 + 4 * i, step[motor_step][1]);
    digitalWrite(32 + 4 * i, step[motor_step][2]);
    digitalWrite(33 + 4 * i, step[motor_step][3]);
    --motor_step;
    if (motor_step == 255)
    _ {
    motor_step = 3;
    }
    }
    else if (motor_count > 0)
    {_

    --motor_count;
    digitalWrite(30 + 4 * i, step[motor_step][0]);
    digitalWrite(31 + 4 * i, step[motor_step][1]);
    digitalWrite(32 + 4 * i, step[motor_step][2]);
    digitalWrite(33 + 4 * i, step[motor_step][3]);
    ++motor_step;
    if (motor_step == 4)
    _ {
    motor_step = 0;
    }
    }
    }
    delayMicroseconds(1000);
    }*_

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code consistently for easier reading.

...R
Stepper Motor Basics
Simple Stepper Code

Serial Input Basics - simple reliable ways to receive data.