Hey all. Hope you're all doing well.
Could do with a little advice/pointers as to amend a piece of code/sketch so that it will work with a DRV8834 Stepper Driver.
I know i need to use either the stepper or accelstepper library.
Any questions. Please ask.
I'm assuming all Arduino code is open source so i shall post the code that i need to adapt here.
If for any reason I'm not allowed or the original author wants it removed. I shall do so.
// ScanController Move sketch
// provided input must be 10 bytes
// byte 0 = 'F' or 'M' Fullsteps or Microsteps
// byte 1 = 'F' or 'B' Forewards or Backwards
// byte 2345 = number of steps 0001 to 9999
// byte 6789 = number of millisec delay between steps 0001 to 9999
#define Pin4 4 // Signal at digital pin 4 Stepper Steuerung
#define Pin5 5 // Signal at digital pin 5 Stepper Steuerung
#define Pin6 6 // Signal at digital pin 6 Stepper Steuerung
#define Pin7 7 // Signal at digital pin 7 Stepper Steuerung
#define Pin8 8 // Signal at digital pin 8 catch reel control
// catch reel motor must only be powered in "forward" mode
// in "back" mode the stepper motor pulls the film from the source reel
// no motor support needed - motor support would be counter productive
int readvalue = 0; // value read from serial input
int nrmoves = 0; // number of movements to perform
int stepdelay = 0; // number of milliseconds as delay between steps/microsteps
int compvalue = 0; // compare value to verify the transfered string
int trnsvalue = 0; // transmitted compare value to verify the transfered string
int error = true; // error = false if transmission is OK, that is: if trnsvalue and compvalue are identical
int skipMsg = false;
int startindex = 0;
int actindex;
int initSwitch = 0;
int bytecount = 0; // how many bytes have been read
int bytenumber; // number of actually read byte
char serinput[12] = { 'F','F','0','1','0','0','0','0','1','5','0','0' };
void setup()
{
pinMode (Pin4, OUTPUT); // digital pin is output
pinMode (Pin5, OUTPUT); // digital pin is output
pinMode (Pin6, OUTPUT); // digital pin is output
pinMode (Pin7, OUTPUT); // digital pin is output
pinMode (Pin8, OUTPUT); // digital pin is output
Serial.begin(9600); // open the serial port to send data
}
void loop()
{
bytecount = Serial.available();
bytenumber = -1;
error = true;
startindex = 0;
compvalue = 0;
while (Serial.available() > 0)
{
bytenumber = bytenumber +1;
char value = Serial.read();
actindex = startindex + bytenumber;
if (actindex == 0)
{
serinput[0] = value;
if (value == 'F')
{
compvalue = compvalue + 1;
}
else {
if (value == 'M')
{
compvalue = compvalue + 2;
}
}
}
if (actindex == 1)
{
serinput[1] = value;
if (value == 'B')
{
compvalue = compvalue + 3;
}
else
{
if (value == 'F')
{
compvalue = compvalue + 4;
}
}
}
if ((actindex > 1) && (actindex < 10))
{
if (value >= '0' && value <= '9')
{
readvalue = value -48;
}
else
{
readvalue = 0;
}
serinput[actindex] = readvalue;
compvalue = compvalue + readvalue;
}
if ((actindex > 9) && (actindex < 12))
{
if (value >= '0' && value <= '9')
{
readvalue = value -48;
}
else
{
readvalue = 0;
}
serinput[actindex] = readvalue;
}
// test if provided input has been correctly read
if (actindex > 11)
{
Serial.flush();
skipMsg = true;
}
else
{
trnsvalue = serinput[10]*10 + serinput[11];
if (compvalue == trnsvalue) {
error = false;
}
else {
error = true;
}
skipMsg = false;
}
}
if (error == false)
{
Serial.println("OK OK OK OK OK OK ");
delay(25);
nrmoves = (serinput[2]*1000 + serinput[3]*100 + serinput[4]*10 + serinput[5]);
stepdelay = (serinput[6]*1000 + serinput[7]*100 + serinput[8]*10 + serinput[9]);
delay(stepdelay);
if(serinput[1] == 'F') // rotate foreward
{
digitalWrite(Pin8, HIGH);
delay(5);
digitalWrite(Pin8, HIGH);
}
else
{
digitalWrite(Pin8, LOW);
delay(5);
digitalWrite(Pin8, LOW);
}
for (int i = 0; i < nrmoves; i++)
{
if(serinput[0] == 'F') // perform full steps
{
if(serinput[1] == 'F') // rotate foreward
{
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
}
if(serinput[1] == 'B') // rotate backward
{
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
}
}
if(serinput[0] == 'M') // perform micro steps
{
if(serinput[1] == 'F') // rotate foreward
{
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
}
if(serinput[1] == 'B') // rotate backward
{
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, HIGH);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, HIGH);
delay(stepdelay);
}
}
}
digitalWrite(Pin8, LOW);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(5);
digitalWrite(Pin8, LOW);
serinput[0] = ' ';
serinput[1] = ' ';
serinput[2] = '0';
serinput[3] = '0' ;
serinput[4] = '0';
serinput[5] = '0';
serinput[6] = '0';
serinput[7] = '0';
serinput[8] = '0';
serinput[9] = '0';
serinput[10] = '0';
Serial.flush();
if (nrmoves > -1) {
Serial.println("MoveEnd MoveEnd ");
}
nrmoves = -1;
delay(1500);
Serial.flush();
delay(0500);
}
else
{
digitalWrite(Pin8, LOW);
digitalWrite(Pin7, LOW);
digitalWrite(Pin6, LOW);
digitalWrite(Pin5, LOW);
digitalWrite(Pin4, LOW);
delay(5);
digitalWrite(Pin8, LOW);
serinput[0] = ' ';
serinput[1] = ' ';
serinput[2] = '0';
serinput[3] = '0' ;
serinput[4] = '0';
serinput[5] = '0';
serinput[6] = '0';
serinput[7] = '0';
serinput[8] = '0';
serinput[9] = '0';
serinput[10] = '0';
serinput[11] = '0';
Serial.flush();
if (skipMsg == false)
{
Serial.println("ER ER ER ER ER ER ");
}
delay(1500);
Serial.flush();
delay(0500);
}
}