Hi guys. I am attempting to create an SPI as a way to transmit position data from a stepper motor from one arduino board to another. I was wondering what an SPI would look like in this case. I have no code to post for the SPI, because I do not know how to implement it. My stepper code is shown below:
Thank you for the reference link. However, I am not sure how to put it into a program. I have done a lot of reading but still do not understand. My master board is the arduino due.
EM501:
Thank you for the reference link. However, I am not sure how to put it into a program. I have done a lot of reading but still do not understand. My master board is the arduino due.
A Due? Do you have any other Arduinos you'd be interested in using?
The reason I ask is because the Due can be a real pain to use in terms of communication protocols (such as I2C, SPI, etc). This might be a better question for the "Due" section of the forum.
The reason I ask here is because I just need a way to approach this project. The due is our final board and I have already experienced some of the problems with the due.
I am only responsible for the master the slave is more complex and is currently being worked on.
Is anyone familiar with using an SPI to transfer data? I am also having the problem of converting a "long" into a "const char." In this case, I am trying to transfer the value of the variable "pos_deg" once per loop. How would I do this?
#include <StepperDriver.h>
#include <AccelStepper.h>
#include <SPI.h>
const int maxrpm = 2500;
const int accdec_steps = 1000;
const int motor_steps = 400;
const int step_division = 1;
const int en_pin = 3;
const int cw_pin = 5; //direction pin
const int clk_pin = 3; //step pin
int stepnumber = 0;
int rpm = 0;
int totalsteps = 6278;
int stepinterval = 1;
int stallcount = 0;
int passcount = 0;
long pos_deg;
int resetcount=0;
long dircount;
StepperDriver ss(motor_steps, step_division, en_pin, cw_pin, clk_pin);
void setup(){
Serial.begin(9600);
ss.powerEnable(true);
delay(1600);
digitalWrite(SS,HIGH);
SPI.begin();
}
void loop(){
byte c;
digitalWrite(SS,LOW);
for(const char * p = "pos_deg"; c = *p; p++); //SPI transfering data - is this correct for what I want to do?
SPI.transfer(c);
digitalWrite(SS,HIGH);
float sensorValue=analogRead(A1);
float voltage=sensorValue*(10.0/1023.0);
if(dircount%2==0){
pos_deg=0.032*stepnumber;
}
if(dircount%2==1){
pos_deg=200-(0.032*stepnumber);
}
if(stepnumber<accdec_steps){
rpm=rpm+(maxrpm/accdec_steps);
ss.setSpeed(rpm);
ss.step(stepinterval);
if(voltage<=2){
passcount++;
}
stepnumber++;
}
if(stepnumber>=accdec_steps && stepnumber<=totalsteps-accdec_steps){
ss.setSpeed(rpm);
ss.step(stepinterval);
if(voltage<=2){
passcount++;
}
stepnumber++;
}
if(stepnumber>totalsteps-accdec_steps){
rpm=rpm-(maxrpm/accdec_steps);
ss.setSpeed(rpm);
ss.step(stepinterval);
if(voltage<=2){
passcount++;
}
stepnumber++;
}
if(stepnumber==totalsteps && passcount>=1){
rpm=100;
pos_deg=0;
stepnumber=0;
stepinterval=stepinterval*-1;
dircount++;
passcount=0;
// Serial.println("stallcount");
// Serial.println(stallcount);
// Serial.println("resetcount");
// Serial.println(resetcount);
}
if(stepnumber==totalsteps && passcount==0){
if(dircount%2==0){
ss.setSpeed(800);
ss.step(totalsteps,accdec_steps,accdec_steps);
resetcount++;
}
if(dircount%2==1){
ss.setSpeed(800);
ss.step(-totalsteps,accdec_steps,accdec_steps);
resetcount++;
}
stallcount++;
rpm=100;
pos_deg=0;
stepnumber=0;
stepinterval=stepinterval*-1;
dircount++;
passcount=0;
// Serial.println("stallcount");
// Serial.println(stallcount);
// Serial.println("resetcount");
// Serial.println(resetcount);
}
}