I am a newbie in programming and need some help with my program.
Background: I am using arduino mega 2560 connected with PC. I send command strings from C# to arduino. I'm able to create a string array to save those received strings, then arduino will scan through those strings to do cnc program.
Now problem: When arduino doing a cnc program, when I press a button on C# program,it will send string "stop\n" to arduino and arduino will break out of the loop to stop step motor.
#include <math.h>
#include <EEPROM.h>
#include <avr/interrupt.h>
#define ARRAYSIZE 100
String results[ARRAYSIZE];
int pre_lineNum = 0;
int flag_lineNum = 0;
int flag_manual = 0;
int flag_homming = 0;
int flag_reset = 0;
int flag_stopx = 0, flag_stopy = 0, flag_stopz = 0;
int lineNum = 0;
String inputString = "";
String commandString = "";
boolean inputStringComplete = false;
boolean isConnected = false;
int count = 0, count_val = 0;
int cmStrLen;
int flag_mode = 0;
int flag_g;
float flag_x, flag_y, flag_z, flag_f, flag_i, flag_j;
int flag_count = 0;
int stop_x = 0, stop_y = 0, stop_z = 0;
void setup()
{
Serial.begin(9600);
//setup something
}
void getCommand()
{
commandString = inputString;
}
void serialEvent()
{
while (Serial.available())
{
char inChar = (char)Serial.read();
inputString += inChar;
count++;
if(inChar == '\n')
{
inputStringComplete = true;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void return_home()
{
//do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void manualMode()
{
//do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void getData()
{
if(commandString.equals("START\n"))
{
}
if(commandString.equals("STOP\n"))
{
}
else if(commandString.equals("STOPX\n"))
{
}
else if(commandString.equals("STOPY\n"))
{
}
else if(commandString.equals("STOPZ\n"))
{
}
else
{
for(int i = 0; i < cmStrLen; i++)
{
if(commandString[i] == ('G')||commandString[i] == ('X')||commandString[i] == ('Y')||commandString[i] == ('Z')||commandString[i] == ('F')||commandString[i] == ('I')||commandString[i] == ('J')||commandString[i] == ('F')||commandString[i] == ('M')||commandString[i] == ('P'))
{
count1 = i;
}
if(commandString[i] == (' ')|| i == (cmStrLen - 1))
{
count2 = i;
}
if(count2 > count1)
{
info++;
temp = commandString.substring(count1,count2);
if(temp.substring(0,1) == "G")
{
flag_g = temp.substring(1,count2).toInt();
}
if(temp.substring(0,1) == "X")
{
flag_x = temp.substring(1,count2).toFloat();
}
else if(temp.substring(0,1) == "Y")
{
flag_y = temp.substring(1,count2).toFloat();
}
else if(temp.substring(0,1) == "Z")
{
flag_z = temp.substring(1,count2).toFloat();
}
else if(temp.substring(0,1) == "I")
{
flag_i = temp.substring(1,count2).toFloat();
}
else if(temp.substring(0,1) == "J")
{
flag_j = temp.substring(1,count2).toFloat();
}
else if(temp.substring(0,1) == "F")
{
flag_f = temp.substring(1,count2).toFloat();
}
if (flag_g == 90)//absolute mode
{
flag_mode = 1;
}
else if (flag_g == 91)//relative mode
{
flag_mode = 2;
}
temp = "";
}
if(i == (cmStrLen-1))
{
temp = "";
count1 = 0;
count2 = 0;
doLine();
doCircle();
}
}
}
commandString = "";
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void doLine()
{
//do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void doCircle()
{
//do something
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void resetZero()
{
//do something
}
void loop()
{
if(inputStringComplete)
{
inputStringComplete = false;
results[lineNum] = inputString;
lineNum++;
/* Send from C# at least 2 line for 1 command then, check the line at the end of each command */
if(inputString == "STOP\n")
{
flag_stopx = 1;
}
if(inputString == "Manual\n")
{
flag_manual = 1;
}
if (inputString == "M30\n")
{
flag_lineNum = 1;
}
if (inputString == "Home\n")
{
flag_homming = 1;
}
if (inputString == "Reset\n")
{
flag_reset = 1;
}
commandString = "";
inputString = "";
}
///////////////////////////////////////
if(flag_stopx == 1)
{
//I tried to stop step motor by doing something in here but it's not working
}
if(flag_reset == 1)
{
commandString = results[0];//Get the first line to do command
cmStrLen = commandString.length();
resetZero();
lineNum = 0;
flag_reset = 0;
}
if(flag_homming == 1)
{
commandString = results[0];//Get the first line to do command
cmStrLen = commandString.length();
return_home();
lineNum = 0;
flag_homming = 0;
}
if(flag_manual == 1)
{
commandString = results[0];//Get the first line to do command
cmStrLen = commandString.length();
flag_mode = 2;
manualMode();
getData();
flag_manual = 0;
lineNum = 0;
}
if(flag_lineNum == 1)
{
for(int nums = 0; nums < lineNum; nums++)
{
commandString = results[nums];
cmStrLen = commandString.length();
getData();
}
commandString = "";
lineNum = 0;
flag_lineNum = 0;
}
flag_mode = 0;//reset mode
}