//Including Libraries
#include <LiquidCrystal.h>
// Declaring Pin Variables for stepper
byte direction_pin = 8;
byte step_pin = 9;
byte led_pin = 13;
//Declaring Pin Variables for buttons
byte slice_pos_pin = 14; //for moving the motor forward 1 cushion thickness
byte slice_neg_pin = 15; //for moving the motor backward 1 cushion thickness
byte jog_pos_pin = 16; //for moving the motor 1 step forward
byte jog_neg_pin = 17; //for moving the motor 1 step backward
byte slice_inc_pin = 18; //for increasing the size of the movement per slice command
byte slice_dec_pin = 19; //for decreasing the size of the movement per slice command
//Declaring Pin Variables for LCD
const int rs=12;
const int en=11;
const int d4=7;
const int d5=6;
const int d6=5;
const int d7=4;
//Declaring Pin Checks
byte slice_pos_check;
byte slice_neg_check;
byte jog_pos_check;
byte jog_neg_check;
byte slice_inc_check;
byte slice_dec_check;
// Declaring Step Parameters
int num_steps_slice; //Steps
int num_steps_jog = 1; //Steps
int pulse_width_time = 500; //Microseconds
int pulse_width_delay = 250; //Microseconds
int inter_request_delay = 50; //Milliseconds
//Declaring and establishing LCD
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
// Declaring Step Count and distance variables
long step_count; //Steps
float screw_position; //Millimeters
float slice_movement; //Millimeters
void setup() {
//Setting Baud Rate
Serial.begin(9600);
//Setting the number of columns and rows in the LCD respectively
lcd.begin(16,2);
//Formatting the sceem
lcd.setCursor(9,0);
lcd.print("mm");
lcd.setCursor(9,1);
lcd.print("mm");
//Setting Pin Modes for inputs and outputs
pinMode(direction_pin,OUTPUT);
pinMode(step_pin,OUTPUT);
pinMode(led_pin,OUTPUT);
pinMode(slice_pos_pin,INPUT_PULLUP);
pinMode(slice_neg_pin,INPUT_PULLUP);
pinMode(jog_pos_pin,INPUT_PULLUP);
pinMode(jog_neg_pin,INPUT_PULLUP);
pinMode(slice_inc_pin,INPUT_PULLUP);
pinMode(slice_dec_pin,INPUT_PULLUP);
//Turning off LED at the start
digitalWrite(led_pin, LOW);
// Setting step count to zero
step_count = 0;
// Setting number of steps in a slice movement to default
num_steps_slice=378;
}
void loop() {
//Checking for positive slice movement request
slice_pos_check=digitalRead(slice_pos_pin);
//Checking for negative slice movement request
slice_neg_check=digitalRead(slice_neg_pin);
//Checking for positive jog movement request
jog_pos_check=digitalRead(jog_pos_pin);
//Checking for negative jog movement request
jog_neg_check=digitalRead(jog_neg_pin);
//Checking for an increase slice movement request
slice_inc_check=digitalRead(slice_inc_pin);
//Checking for a decrease slice movement request
slice_dec_check=digitalRead(slice_dec_pin);
// Serial.println(" ");
// Serial.println(" ");
// Serial.println(" ");
// Serial.println(" ");
// Serial.println("slice_pos_check");
// Serial.println(slice_pos_check);
// Serial.println("slice_neg_check");
// Serial.println(slice_neg_check);
// Serial.println("jog_pos_check");
// Serial.println(jog_pos_check);
// Serial.println("jog_neg_check");
// Serial.println(jog_neg_check);
// Serial.println("slice_inc_check");
// Serial.println(slice_inc_check);
// Serial.println("slice_dec_check");
// Serial.println(slice_dec_check);
// Serial.println("step_count");
// Serial.println(step_count);
// Serial.println("screw_position");
// Serial.println(screw_position);
//Running positive slice movement request
if (slice_pos_check == LOW) {
//Setting direction to positive motion
digitalWrite(direction_pin,HIGH);
//Sending signal to move with pulses and adding to the step count
for (int a = 0; a < num_steps_slice ; a++,step_count++) {
digitalWrite(step_pin,HIGH);
delayMicroseconds(pulse_width_time);
digitalWrite(step_pin,LOW);
delayMicroseconds(pulse_width_delay);
//Flashing LED pin to indicate movement
digitalWrite(led_pin, !digitalRead(led_pin));
}
//Making sure LED is off once movement is complete
digitalWrite(led_pin,LOW);
delay(inter_request_delay);
}
else {
}
//Running negative slice movement request
if (slice_neg_check == LOW) {
//Setting direction to negative motion
digitalWrite(direction_pin,LOW);
//Sending signal to move with pulses and subtracting from the step count
for (int a = 0; a < num_steps_slice ; a++,step_count--) {
digitalWrite(step_pin,HIGH);
delayMicroseconds(pulse_width_time);
digitalWrite(step_pin,LOW);
delayMicroseconds(pulse_width_delay);
//Flashing LED pin to indicate movement
digitalWrite(led_pin, !digitalRead(led_pin));
}
//Making sure LED is off once movement is complete
digitalWrite(led_pin,LOW);
delay(inter_request_delay);
}
else {
}
//Running positive jog movement request
if (jog_pos_check == LOW) {
//Setting direction to positive motion
digitalWrite(direction_pin,HIGH);
//Sending signal to move with pulses and adding to the step count
for (int a = 0; a < num_steps_jog ; a++,step_count++) {
digitalWrite(step_pin,HIGH);
delayMicroseconds(pulse_width_time);
digitalWrite(step_pin,LOW);
delayMicroseconds(pulse_width_delay);
//Flashing LED pin to indicate movement
digitalWrite(led_pin, !digitalRead(led_pin));
}
//Making sure LED is off once movement is complete
digitalWrite(led_pin,LOW);
delay(inter_request_delay);
}
else {
}
//Running negative jog movement request
if (jog_neg_check == LOW) {
//Setting direction to negative motion
digitalWrite(direction_pin,LOW);
//Sending signal to move with pulses and subtracting from the step count
for (int a = 0; a < num_steps_jog ; a++,step_count--) {
digitalWrite(step_pin,HIGH);
delayMicroseconds(pulse_width_time);
digitalWrite(step_pin,LOW);
delayMicroseconds(pulse_width_delay);
//Flashing LED pin to indicate movement
digitalWrite(led_pin, !digitalRead(led_pin));
}
//Making sure LED is off once movement is complete
digitalWrite(led_pin,LOW);
delay(inter_request_delay);
}
else {
}
//Running increase slice movement request
if (slice_inc_check == LOW) {
//incrementing the number of steps in a slice by 1
num_steps_slice++;
delay(inter_request_delay);
}
else {
}
//Running decrease slice movement request
if (slice_dec_check == LOW) {
//decrementing the number of steps in a slice by 1
num_steps_slice--;
delay(inter_request_delay);
}
else {
}
//Converting step count (steps) into screw_position (mm)
screw_position = step_count * 0.0079375;
slice_movement = num_steps_slice * 0.0079375;
//Sending screw position to LCD
lcd.setCursor(0,0);
lcd.print(screw_position);
lcd.setCursor(0,1);
lcd.print(slice_movement);
delay(100);
}