I did a lot of research to find an explanation about custom classes in Arduino. I'm getting a headache over here so maybe you guys could help me. First some code. Here is my custom class:
class LetterData
{
int distance[];
int rotation[];
}
LetterData sequence[26];
These arrays are filled in startup:
//A
sequence[0] = new LetterData();
sequence[0].distance = new int[] {10, 10, 5, 5, 5, 5, 5, 10, 5};
sequence[0].rotation = new int[] {0, 90, 90, 90, 180, 90, 0, -90, -90};
//B
sequence[1] = new LetterData();
sequence[1].distance = new int[] {10, 10, 5, 5, 5, 8, 5, 8, 10, 13 };
sequence[1].rotation = new int[] {0, 90, 90, 90, 180, 90, 90, -90, -90, -90};
The problem is I get the following error: sketch_mar12a:32: error: expected initializer before 'sequence'.
Is it even possible to create such a class in Arduino? I've looked at many tutorials about classes but none of them explains in a good way how to create and import these classes.
Any insight is really appreciated. Thanks in advance!
EDIT:
Here is my whole code. Its not pretty as I'm still busy converting it to work with arduino (I made a simulation with Unity first to create some idea's):
int letters[] = {};
int distance[4];
int rotation[4];
float rideTimer;
float rotationTimer;
int seqNumber;
int lNumber;
float currentRotation;
float treshRotation;
bool right;
bool left;
int moveSpeed;
int rotSpeed;
int currDist;
int currRot;
class LetterData
{
public:
int distance[];
int rotation[];
};
LetterData sequence[26];
void setup()
{
SetLetters();
// GetSequence();
}
/*void GetSequence()
{
for(int i = 0; i<4;i++)
{
int index = letters[i];
for(int j=0;j<sizeof(sequence[index].distance);j++)
{
distance.Add(num);
}
for(int num : sequence[index].rotation)
{
rotation.Add(num);
}
}
}*/
// Update is called once per frame
void loop()
{
if (rideTimer < distance[seqNumber])
{
//rideTimer += moveSpeed * Time.deltaTime;
//transform.Translate(Vector3.forward * Time.deltaTime);
currDist = distance[seqNumber];
}
else
{
if (rotationTimer < rotation[seqNumber])
right = true;
else if (rotationTimer > rotation[seqNumber])
left = true;
else if (rotationTimer == rotation[seqNumber])
{
seqNumber += 1;
rotationTimer = 0;
rideTimer = 0;
}
currRot = rotation[seqNumber];
if (right)
{
// rotationTimer += rotSpeed * Time.deltaTime;
// transform.Rotate(0, rotSpeed * Time.deltaTime, 0);
currentRotation = rotationTimer;
if (rotationTimer > rotation[seqNumber] - treshRotation)
{
treshRotation = 0;
//!- treshRotation = Math.Abs(rotationTimer - rotation[seqNumber]);
seqNumber += 1;
rotationTimer = 0;
rideTimer = 0;
right = false;
}
}
else if (left)
{
// rotationTimer -= rotSpeed * Time.deltaTime;
//transform.Rotate(0, -rotSpeed * Time.deltaTime, 0);
if (rotationTimer < rotation[seqNumber] + treshRotation)
{
treshRotation = 0;
//!- treshRotation = Mathf.Abs(rotationTimer - rotation[seqNumber])*1;
seqNumber += 1;
rotationTimer = 0;
rideTimer = 0;
left = false;
}
}
//}
}
}
void SetLetters()
{
//A
sequence[0] = new LetterData();
sequence[0].distance = new int[]{10, 10, 5, 5, 5, 5, 5, 10, 5};
sequence[0].rotation = new int[] {0, 90, 90, 90, 180, 90, 0, -90, -90};
//B
sequence[1] = new LetterData();
sequence[1].distance = new int[] {10, 10, 5, 5, 5, 8, 5, 8, 10, 13 };
sequence[1].rotation = new int[] {0, 90, 90, 90, 180, 90, 90, -90, -90, -90};
//C
sequence[2] = new LetterData();
sequence[2].distance = new int[] {10, 10, 5, 5, 10, 5, 10, 5 };
sequence[2].rotation = new int[] {0, 90, 180, -90, -90, 90, -90, -90 };
//D
sequence[3] = new LetterData();
sequence[3].distance = new int[] {10, 10, 8, 8, 10, 10};
sequence[3].rotation = new int[] {0, 135, 90, -45,-90, -90};
}
On a side note here; how would one create new arrays with predefined indexes to a custom class (like the 'SetLetters' function works in c#).