CSV data Logging from stepper motor

I want the degree of rotation in stepper motor program to be logged in a CSV file. My program is as below

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

signed int p = 0, q = 0, aaa = 0, ttt = 0, stepC, bbb = 0, ccc = 0, ddd = 0, j = 0, n = 0, stepCount = 0, step_angle; // number of steps the motor has taken

float ppp = 0, angle, ang, angle2;

unsigned char prev , temp = ‘\0’, a[10], i, inbyte;

signed char angle2_flag = 0;

void setup()
{
// set the speed at 60 rpm:
myStepper.setSpeed(60);

// initialize the serial port:
Serial.begin(9600);

}

void loop()
{
if (Serial.available() > 0)
{
prev = temp;
inbyte = Serial.read();
temp = inbyte;

if (inbyte == ‘A’)
{
p = 1;
if (q == 1)
{
q = 0;
stepCount = ttt;
}
bbb = 0;
ccc = 0;
ddd = 0;
if (aaa == 0)
{
aaa = 1;
Serial.println(" -------------------------------");
Serial.println(" DIRECTION : CW MODE : DISCRETE");
Serial.println(" -------------------------------");
Serial.print(" POS STEP “); Serial.println(“POS DEGREE”);
Serial.print(”\r\n");

}
inbyte = ‘\0’;
if (prev != ‘A’)
{ n = 1;
j = 0;
}
else
n++;

while ((1.8 * j) < (9.37894738 * 1.8 * n))
{
j++;
myStepper.step(1);
}
stepCount++;
angle = ((float)stepCount * 1.8);

Serial.print(" “); Serial.print(stepCount); Serial.print(” "); Serial.print(angle);

Serial.print("\r\n");
}
else if (inbyte == ‘B’)
{
p = 1;
if (q == 1)
{
q = 0;
stepCount = ttt;
}
aaa = 0;
ccc = 0;
ddd = 0;
if (bbb == 0)
{
bbb = 1;
Serial.println(" -------------------------------");
Serial.println(" DIRECTION : CCW MODE : DISCRETE");
Serial.println(" -------------------------------");
Serial.print(" POS STEP “); Serial.println(“POS DEGREE”);
Serial.print(”\r\n");

}
inbyte = ‘\0’;
if (prev != ‘B’)
{ n = 1;
j = 0;
}
else
n++;

while ((1.8 * j) < (9.37894738 * 1.8 * n))
{
j++;
myStepper.step(-1);
}
stepCount–;
angle = ((float)stepCount * 1.8);

Serial.print(" “); Serial.print(stepCount); Serial.print(” "); Serial.print(angle);

Serial.print("\r\n");
}
else if (inbyte == ‘C’)
{
if (p == 1)
{
stepC = stepCount * 5.21;
ang = angle * 5.21;
p = 0;
}
q = 1;
aaa = 0;
bbb = 0;
ddd = 0;
inbyte = ‘\0’;

Serial.print("\r\n");
Serial.println(“Step Mode: CONTINUOUS”);

Serial.print(“Enter target angle(CW):”);

i = 0;

while (1)
{
if (Serial.available() > 0)
{
inbyte = Serial.read();

if (inbyte == 13)
{
if (ccc == 0)
{
ppp = 0;
ttt = 0;
Serial.print("\r\n");
Serial.println(" -------------------------------");
Serial.println(" DIRECTION : CW MODE : CONTINUOUS");
Serial.println(" -------------------------------");
Serial.print(" POS STEP “); Serial.println(“POS DEGREE”);
Serial.print(”\r\n");
ccc = 1;
}
step_angle = 0;

if (i == 4)step_angle = (((signed int)a[1] - 48) * 10) + (((signed int)a[3] - 48) * 1);
else if (i == 5)step_angle = (((signed int)a[1] - 48) * 100) + (((signed int)a[2] - 48) * 10) + (((signed int)a[4] - 48) * 1);
else if (i == 6)step_angle = (((signed int)a[1] - 48) * 1000) + (((signed int)a[2] - 48) * 100) + (((signed int)a[3] - 48) * 10) + (((signed int)a[5] - 48) * 1);
else if (i == 7)step_angle = (((signed int)a[1] - 48) * 10000) + (((signed int)a[2] - 48) * 1000) + (((signed int)a[3] - 48) * 100) + (((signed int)a[4] - 48) * 10) + (((signed int)a[6] - 48) * 1);

if (a[0] == ‘-’)step_angle = (-1) * step_angle;

angle2 = ((float)step_angle / (float)10);

while (1)
{
if (ang > angle2 * 5.21052632) {
angle2_flag = 1;
break;
}
else
{
// step one step:
myStepper.step(1);
delay(20);
stepC++;

ang = (float)stepC * 1.8;

}
ppp = ttt;
ttt = int(stepC / 5.21);
if (ppp != ttt)
{
Serial.print(" “); Serial.print(ttt); Serial.print(” "); Serial.print(ang / 5.21);

Serial.print("\r\n");
}
}

if (angle2_flag == 1) {
angle2_flag = 0;
break;
}
}
else
{
a[i++] = inbyte;
}
}
}

}
else if (inbyte == ‘D’)
{
if (p == 1)
{
stepC = stepCount * 5.21;
ang = angle / 5.21;
p = 0;
}
q = 1;
aaa = 0;
bbb = 0;
ccc = 0;
inbyte = ‘\0’;

Serial.print("\r\n");
Serial.println(“Step Mode: CONTINUOUS”);

Serial.print(“Enter target angle(CCW):”);

i = 0;

while (1)
{
if (Serial.available() > 0)
{
inbyte = Serial.read();

if (inbyte == 13)
{
if (ddd == 0)
{
ppp = 0;
ttt = 0;
Serial.print("\r\n");
Serial.println(" -------------------------------");
Serial.println(" DIRECTION : CCW MODE : CONTINUOUS");
Serial.println(" -------------------------------");
Serial.print(" POS STEP “); Serial.println(“POS DEGREE”);
Serial.print(”\r\n");
ddd = 1;
}
step_angle = 0;

if (i == 4)step_angle = (((signed int)a[1] - 48) * 10) + (((signed int)a[3] - 48) * 1);
else if (i == 5)step_angle = (((signed int)a[1] - 48) * 100) + (((signed int)a[2] - 48) * 10) + (((signed int)a[4] - 48) * 1);
else if (i == 6)step_angle = (((signed int)a[1] - 48) * 1000) + (((signed int)a[2] - 48) * 100) + (((signed int)a[3] - 48) * 10) + (((signed int)a[5] - 48) * 1);
else if (i == 7)step_angle = (((signed int)a[1] - 48) * 10000) + (((signed int)a[2] - 48) * 1000) + (((signed int)a[3] - 48) * 100) + (((signed int)a[4] - 48) * 10) + (((signed int)a[6] - 48) * 1);

if (a[0] == ‘-’)step_angle = (-1) * step_angle;

angle2 = ((float)step_angle / (float)10);

while (1)
{
if (ang <= angle2 * 5.21052632) {
angle2_flag = 1;
break;
}
else
{
// step one step:
myStepper.step(-1);
delay(20);
stepC–;

ang = (float)stepC * 1.8;

}
ppp = ttt;
ttt = int(stepC / 5.21);
if (ppp != ttt)
{
Serial.print(" “); Serial.print(ttt); Serial.print(” "); Serial.print(ang / 5.21);

Serial.print("\r\n");
}
}

if (angle2_flag == 1) {
angle2_flag = 0;
break;
}
}
else
{
a[i++] = inbyte;
}
}
}

}
}
}

A CSV file has values separated by commas. all you need do is Serial.print (",") between the values and send the data to a proper terminal programme, like RealTerm. This will record the the data as a CSV file. The same proigramme can timestamp the readings if you need that.

Also posted at:
https://arduino.stackexchange.com/q/47791
If you’re going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.