#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <SPI.h>
AccelStepper stepper1(AccelStepper::FULL2WIRE, 10, 11);//Cardiac
AccelStepper stepper2(AccelStepper::FULL2WIRE, 12, 13);//Pulmonary
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {6, 5, 4, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int spd = 1000; // The current speed in steps/second
int sign = 1; // Either 1, or 0. Can use to change to reverse if sign= -1
String RPM=("RPM");
String Steps=("Steps per Second");
String disp=("Running at:"); //A display string so user knows input will be performed
String disp2=("Steps per Second:"); //Secondary Display String
int input=0;
int z=0;//Changes to 1 and causes Cardiac Signal Generator to send signal
int posit=stepper1.currentPosition();//Determines position of stepper1. If position reaches zero again it triggers z=1;
void setup()
{
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV2);//divide the clock by 8
lcd.begin(20,4);
lcd.setCursor(0,0);
stepper1.setMaxSpeed(128000); //Setting max speed of 240 RPM if Steps are 400 On Off On
stepper1.setAcceleration(500); //Max acceleration
stepper1.setSpeed(1600); //Initial Starting Speed, not important, 2133=80- 3200=120 RPM only for stepper1();
//Because of Step Rate of stepper1(); driver
stepper2.setMaxSpeed(100000); //Setting max speed of 240 RPM if Steps are 400 On Off On
stepper2.setAcceleration(500); //Max acceleration
stepper2.setSpeed(3200); //Initial Starting Speed, not important, 2133=20 RPM only for stepper2();
//because of Step Rate of stepper1(); driver
lcd.print("Enter RPM for");
lcd.setCursor(0,1);
lcd.print("Stepper 1 (60 RPM)");
lcd.setCursor(0,2);
lcd.print("Add 1000 to RPM to");
lcd.setCursor(0,3);
lcd.print("change Stepper 2 20");
}
void loop()
{
posit=stepper1.currentPosition();
String input_String; //String for receving Serial.read(); and converting to int value_RPM
String num; //A display string so user knows input will be performed at num RPM
int key=kpd.getKey();
if(key != NO_KEY){ //Making sure key is available before taking any action
if(key == '#'){
lcd.setCursor(0,2);
lcd.print("Received:"); //Letting user know input was received
lcd.setCursor(0,3);
lcd.print(input); //Printing what int we got
while(input==8888){
stepper1.setAcceleration(11000);
stepper1.runToNewPosition(1600);
stepper1.setCurrentPosition(0);
stepper2.run();
}
if(input==9999){
stepper1.setSpeed(0);
stepper2.setSpeed(0);
lcd.clear();
lcd.print("All motion stopped");
lcd.setCursor(0,1);
lcd.print("Press reset button:");
lcd.setCursor(0,2);
lcd.print("Resume Motion or");
lcd.setCursor(0,3);
lcd.print("Turn off power");
}
else if (input >= 1000 && input <= 2000){ //This if only for stepper 2, makes sure that the input is over 1000 so we know
//User wants to modify stepper 2. Add 1000 to whatever RPM value you desire to control stepper 2
lcd.clear();
lcd.print("Modifying speed of Stepper 2");
input = input-1000; //Once confirmed that stepper 2 is being controlled, reduce to normal values and function normally
if (input == 1) { // Rotates forward, useless for now but if you want to add reverse functionality it is useful
disp=("Rotating Forward");
sign = 1;
}
else if (input == 0){ // stop. Once stopped, it will not restart
disp=("Rotation Stopping \n");
spd = 0;
num= "0";
lcd.clear();
lcd.print("You have stopped the motor. To resume motion, reupload the program");
}
else{
spd=input*213.3333; //Ratio for 1600 Steps
//stepper1(); as the steps/rev are different
num=(spd); //Convert spd back to string to display and manipulate easier
}
stepper2.setAcceleration(6400);
stepper2.setSpeed(sign * spd); //Keep this for future reverse functionality
lcd.clear();
lcd.setCursor(0,0);
lcd.print(disp + input + " " + RPM); //Final confirmation of action Arduino is taking
lcd.setCursor(0,1);
lcd.print(disp2); //Displayed in steps per second
lcd.setCursor(0,2);
lcd.print(num + " For Stepper 2");
lcd.setCursor(0,3);
lcd.print("Ready for RPM change");
}
else if(input<1000){ //This else for only stepper 1
lcd.clear();
lcd.print("Modifying speed of Stepper 1");
if (input == 0){ // stop. Once stopped, it will not restart
disp=("Rotation Stopping \n");
spd = 0;
num= "0";
lcd.clear();
lcd.print("You have stopped the motor. To resume motion, reupload the program");
}
else{
spd=input*26.6667; //Ratio for 400 steps
//The second motor as the steps/rev are different
num=(spd); //Convert spd back to string to display and manipulate easier
}
stepper1.setAcceleration(6400);
stepper1.setSpeed(sign * spd); //Keep this for future reverse functionality
lcd.clear();
lcd.setCursor(0,0);
lcd.print(disp + input + " " + RPM); //Final confirmation of action Arduino is taking
lcd.setCursor(0,1);
lcd.print(disp2); //Displayed in steps per second
lcd.setCursor(0,2);
lcd.print(num + " For Stepper 1");
lcd.setCursor(0,3);
lcd.print("Ready for RPM change");
}
input=0;
}
else{
input = input*10;
input = input + key - '0';
lcd.clear();
lcd.print(input);
}
}
posit=stepper1.currentPosition();
if(posit==0){
digitalWrite(SS, LOW); // enable Slave Select
z=1;
SPI.transfer (z);
}
digitalWrite(SS, HIGH); // disable Slave Select
z=0;
stepper1.runSpeed(); //Running arduino at desired speed
stepper2.runSpeed();
}
Essentially, I'm running two arduino Mega2560 on an SPI connection.
This is the master code. My slave works as intended. I am attempting to change the value of int z; from z=0; to z=1; whenever the stepper1(); completes 1 revolution. I am attempting to do this by using int posit=stepper1.currentPosition(); and setting that value as zero at the beginning of the program. And I want to constantly assess it until posit=0; again when I know that the stepper1(); has completed one revolution because its step value is 0. Are there better ways of assessing it? Or did I just place my currentPosition(); calls in the wrong places?
Apologies in advance for the messy code. This is the last thing I have to do for it and afterwards I'm gonna clean it up properly.