I am trying to make a device that runs a stepper motor at the same time as collecting force data. I cannot find a way to do this with only one Arduino. Collecting data with the same Arduino causes the stepper to stutter. I now have 2 Arduinos, one for data collection, the other for motor control.
I have abstracted the problem to make the motor side drive 2 LED's. One to signify the motor going forward and the other for backward. There are 2 command pins that are tied to interrupts on the motor side, the motor side then sends back a signal to say if the motor is busy or not, which is tied to an interrupt on the data side.
Here is the code for PusherControlSide.ino
//PINS
const int Motor_Busy_Pin = 4;
const int Comms_Pin_A = 5;
const int Comms_Pin_B = 6;
//must add in buttons eventually
//Variables
float val = 0;
//float calibration_factor = -234550; //-7050 worked for my 440lb max scale setup
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Change to better buad rate later
pinMode(Motor_Busy_Pin, INPUT);
pinMode(Comms_Pin_A, OUTPUT);
pinMode(Comms_Pin_B, OUTPUT);
digitalWrite(Comms_Pin_A,LOW);
digitalWrite(Comms_Pin_B,LOW);
attachInterrupt(digitalPinToInterrupt(Motor_Busy_Pin), wait4Motor_ISR, CHANGE);
//when the Motor_Busy_Pin goes from low to high this interrupt will trigger
}//void setup()
/*
* The Motor side will follow this truth table
* | A | B | Y
* | 0 | 0 | None
* | 0 | 1 | Forward
* | 1 | 0 | Backward
* | 1 | 1 | Trial
*/
void loop() {
//test loop for demo
Serial.println("start Loop");
delay(5000);
Serial.println("Forward");
// delay(5000);
digitalWrite(Comms_Pin_A,LOW);
digitalWrite(Comms_Pin_B,HIGH);
Serial.println("Backward");
// delay(5000);
digitalWrite(Comms_Pin_A,HIGH);
digitalWrite(Comms_Pin_B,LOW);
Serial.println("Trial");
// delay(5000);
digitalWrite(Comms_Pin_A,HIGH);
digitalWrite(Comms_Pin_B,HIGH);
}//void loop()
/*
* Checks to see which signal is being sent to the
* motor and hangs or collects data accordingly
* it also clears the signal back to 00
*
*/
void wait4Motor_ISR(){
if(digitalRead(Motor_Busy_Pin) == LOW){
digitalWrite(Comms_Pin_A,LOW);
digitalWrite(Comms_Pin_B,LOW);
delay(5000);
}else{
if(digitalRead(Comms_Pin_A) == HIGH && digitalRead(Comms_Pin_B) == HIGH){
// while(digitalRead(Motor_Busy_Pin) == HIGH){Serial.print(scale.get_units(), 3);}
int cycleCount = 0;
while(digitalRead(Motor_Busy_Pin) == HIGH){cycleCount++;}
// Serial.println(cycleCount);
}else{
int cycleCount = 0;
while(digitalRead(Motor_Busy_Pin) == HIGH){cycleCount++;}
// Serial.println(cycleCount);
}//else if
}//else if Motor_Busy_Pin
}//void wait4Motor_ISR()
Here is the code for PusherMotorSide.ino
//Library includes
//#include <Stepper.h> // for stepper motor
// more stepper stuff
//const int stepsPerRevolution = 200;
//Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// PINS
const int Motor_Busy_Pin = 4;
const int Comms_Pin_A = 3;
const int Comms_Pin_B = 2;
//For LED DEMO
const int ForwardLED_PIN = 11;
const int BackwardLED_PIN = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Change to better buad rate later
pinMode(Motor_Busy_Pin, OUTPUT);
digitalWrite(Motor_Busy_Pin,LOW);
pinMode(Comms_Pin_A, INPUT);
pinMode(Comms_Pin_B, INPUT);
attachInterrupt(digitalPinToInterrupt(Comms_Pin_A), inputRecieved_ISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(Comms_Pin_B), inputRecieved_ISR, CHANGE);
//For LED Demo
pinMode(ForwardLED_PIN, OUTPUT);
pinMode(BackwardLED_PIN, OUTPUT);
digitalWrite(ForwardLED_PIN, LOW);
digitalWrite(BackwardLED_PIN, LOW);
}//void setup()
void loop() {
// put your main code here, to run repeatedly:
}//void loop()
void inputRecieved_ISR(){
if(digitalRead(Comms_Pin_A) == LOW && digitalRead(Comms_Pin_B) == HIGH){
digitalWrite(Motor_Busy_Pin,HIGH);
// myStepper.setSpeed(800);
// myStepper.step(-50);
//For LED Demo
digitalWrite(ForwardLED_PIN, HIGH);
// for(int i = 0; i < 100000; i++){}
delay(2000);
digitalWrite(ForwardLED_PIN, LOW);
digitalWrite(Motor_Busy_Pin,LOW);
}//if(digitalRead(Comms_Pin_A) == LOW && digitalRead(Comms_Pin_B) == HIGH)
if(digitalRead(Comms_Pin_A) == HIGH && digitalRead(Comms_Pin_B) == LOW){
digitalWrite(Motor_Busy_Pin,HIGH);
// myStepper.setSpeed(800);
// myStepper.step(50);
//for LED Demo
digitalWrite(BackwardLED_PIN, HIGH);
// for(int i = 0; i < 100000; i++){}
delay(2000);
digitalWrite(BackwardLED_PIN, LOW);
digitalWrite(Motor_Busy_Pin,LOW);
}//if(digitalRead(Comms_Pin_A) == HIGH && digitalRead(Comms_Pin_B) == LOW)
if(digitalRead(Comms_Pin_A) == HIGH && digitalRead(Comms_Pin_B) == HIGH){
digitalWrite(Motor_Busy_Pin,HIGH);
// myStepper.setSpeed(800);
// int steplen = 18;
// for(int i = 0; i < 6; i++){
// for(int j = 0; j < 188; j++){
// myStepper.step(steplen);
// //get data original code makes stuttering
// }//for(int j = 0; j < 188; j++)
// steplen *= -1;
// }//for(int i = 0; i < 6; i++)
//for LED Demo
for(int i = 0; i < 6; i++){
if(i%2){
digitalWrite(ForwardLED_PIN, HIGH);
delay(2000);
// for(int i = 0; i < 100000; i++){}
digitalWrite(ForwardLED_PIN, LOW);
}else{
digitalWrite(BackwardLED_PIN, HIGH);
// for(int i = 0; i < 100000; i++){}
delay(2000);
digitalWrite(BackwardLED_PIN, LOW);
}//else if i%2
}//for(int i = 0; i < 6; i++)
digitalWrite(Motor_Busy_Pin,LOW);
}//if(digitalRead(Comms_Pin_A) == HIGH && digitalRead(Comms_Pin_B)
}//void inputRecieved_ISR()
here is a picture of my config
Currently, the device only flashes the forward LED once every 4 seconds. A far cry from the Demo routine detailed in PusherControllSide.ino. What am I missing here? Are the routines going too fast? I appreciate your feedback...
