Hi,
There is one difference with visual micro. It does not use a new system to establish where to inject c++ prototypes. Visual Micro will attempt to inject them before your user types have been created but you can override this behaviour and add them yourself.
Prototypes have the exact same signature as the methods they relate too along with a semi colon on the end.
Adding prototypes has the benefit that you can move your code to ccp/h file more easily and also decide for yourself the right time to declare the prototype for a method.
In this example the prototypes have been added together before the code. The comments have been added for information only.
// Connections to A4988
enum class Pins {
dirPin=5,
stepPin,
SleepPin,
ResetPin,
MS3,
MS2,
MS1,
EnablePin
};
// Motor steps per rotation
enum class Constants { STEPS_PER_REV = 200 };
enum class Resolutions { FullStep = 1,
HalfStep = 2,
QuarterStep = 4,
EigthStep = 8,
SixteenthStep = 16
};
unsigned int NumberOfSteps = 0;
/* PROTOTYPES */
void CalculateNumberOfSteps(Resolutions DesiredResolution);
void SetResolution(Resolutions DesiredResolution);
/* CODE */
void CalculateNumberOfSteps(Resolutions DesiredResolution)
{
NumberOfSteps = (unsigned int) Constants::STEPS_PER_REV * (unsigned int) DesiredResolution;
}
void SetResolution(Resolutions DesiredResolution)
{
// Set Steprate to Resolution
switch (DesiredResolution)
{
case Resolutions::FullStep:
{
digitalWrite((unsigned int)Pins::MS1, LOW);
digitalWrite((unsigned int)Pins::MS2, LOW);
digitalWrite((unsigned int)Pins::MS3, LOW);
break;
}
case Resolutions::HalfStep:
{
digitalWrite((unsigned int)Pins::MS1, HIGH);
digitalWrite((unsigned int)Pins::MS2, LOW);
digitalWrite((unsigned int)Pins::MS3, LOW);
break;
}
case Resolutions::QuarterStep:
{
digitalWrite((unsigned int)Pins::MS1, LOW);
digitalWrite((unsigned int)Pins::MS2, HIGH);
digitalWrite((unsigned int)Pins::MS3, LOW);
break;
}
case Resolutions::EigthStep:
{
digitalWrite((unsigned int)Pins::MS1, HIGH);
digitalWrite((unsigned int)Pins::MS2, HIGH);
digitalWrite((unsigned int)Pins::MS3, LOW);
break;
}
case Resolutions::SixteenthStep:
{
digitalWrite((unsigned int)Pins::MS1, HIGH);
digitalWrite((unsigned int)Pins::MS2, HIGH);
digitalWrite((unsigned int)Pins::MS3, HIGH);
break;
}
default:
{
digitalWrite((unsigned int)Pins::MS1, LOW);
digitalWrite((unsigned int)Pins::MS2, LOW);
digitalWrite((unsigned int)Pins::MS3, LOW);
break;
}
}
CalculateNumberOfSteps(DesiredResolution);
}
void setup()
{
// Setup the pins as Outputs
pinMode((unsigned int) Pins::stepPin,OUTPUT);
pinMode((unsigned int) Pins::dirPin,OUTPUT);
pinMode((unsigned int) Pins::SleepPin,OUTPUT);
pinMode((unsigned int) Pins::ResetPin,OUTPUT);
pinMode((unsigned int) Pins::MS3,OUTPUT);
pinMode((unsigned int) Pins::MS2,OUTPUT);
pinMode((unsigned int) Pins::MS1,OUTPUT);
pinMode((unsigned int) Pins::EnablePin,OUTPUT);
// Set Status Pins
digitalWrite((unsigned int) Pins::EnablePin, LOW);
digitalWrite((unsigned int) Pins::ResetPin, HIGH);
digitalWrite((unsigned int) Pins::SleepPin, HIGH);
Serial.begin(9600);
}
void loop()
{
// Experiment with dynamically changing Step Resolutions
// and Accelleration / Decelleration, simulationg 3D Print Movements
// First, do a quarter spin with lowest Resolution and high Speed movement to position
Serial.print("CW\n");
digitalWrite((unsigned int)Pins::dirPin, HIGH);
SetResolution(Resolutions::FullStep);
Serial.print("FullStep\n");
Serial.print("NumberOfSteps: ");
Serial.print(NumberOfSteps);
Serial.print("\n");
for(unsigned int x = 0; x < NumberOfSteps / 4; x++)
{
digitalWrite((unsigned int) Pins::stepPin, HIGH);
delayMicroseconds(500);
digitalWrite((unsigned int) Pins::stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
// Second, do a half spin with finest resolution and slow movement
SetResolution(Resolutions::SixteenthStep);
Serial.print("CW\n");
Serial.print("MicroStep \n");
Serial.print("NumberOfSteps: ");
Serial.print(NumberOfSteps);
Serial.print("\n");
for(unsigned int x = 0; x < NumberOfSteps / 2; x++)
{
digitalWrite((unsigned int) Pins::stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite((unsigned int) Pins::stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000);
// Third, go back to start position on high Speed
Serial.print("CCW\n");
digitalWrite((unsigned int) Pins::dirPin, LOW);
SetResolution(Resolutions::FullStep);
Serial.print("FullStep \n");
Serial.print("NumberOfSteps: ");
Serial.print(NumberOfSteps);
Serial.print("\n");
for(unsigned int x = 0; x < NumberOfSteps * 3 / 4; x++)
{
digitalWrite((unsigned int) Pins::stepPin, HIGH);
delayMicroseconds(500);
digitalWrite((unsigned int) Pins::stepPin, LOW);
delayMicroseconds(500);
}
// Fourth, repeat slow movement, this time with medium Resolution
// Reset Motor Driver - Test
//Serial.print("Reset \n");
//digitalWrite((unsigned int) Pins::ResetPin, LOW);
// shut off motor Serial.print("MicroStep \n");
//Serial.print("Disable \n");
//digitalWrite((unsigned int) Pins::EnablePin, LOW);
delay(2000);
// switch Motor on again
//Serial.print("Enable \n");
//digitalWrite((unsigned int) Pins::EnablePin, HIGH);
//delay(1000);
//Serial.print("Un-Reset \n");
//digitalWrite((unsigned int) Pins::ResetPin, HIGH);
}