// Add library
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <AccelStepper.h>
//#include <EEPROM.h>
//#include "HX711_ADC.h"
#include <SD.h>
// Define digital pins for motors
#define dirPin 19
#define stepPin 18
//#define dirPin2 21
//#define stepPin2 20
#define stepsPerRevolution 200
#define motorInterfaceType 1
// Define digital pins SD Card Module and Ultra Sonic Sensor
#define PIN_SPI_CS 53
/*
#define ECHO_PIN_1 37
#define TRIG_PIN_1 35
#define ECHO_PIN_2 41
#define TRIG_PIN_2 39
*/
// Define SD card
File myFile;
// Define motors pins
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
//AccelStepper stepper2 = AccelStepper(motorInterfaceType, stepPin2, dirPin2);
// Define Loadcell pins
/*
HX711_ADC LoadCell_1(A0, A1);
HX711_ADC LoadCell_2(A2, A3);
unsigned long t = 0; // Track time for valid loadcell data
*/
// define LCD digital pins
LiquidCrystal lcd2(13, 12, 14, 15, 16, 17);
LiquidCrystal lcd(38, 39, 25, 27, 29, 31);
// define keypad digital pins
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = { //Defining keypad characters
{ '1', '4', '7', '*' },
{ '2', '5', '8', '0' },
{ '3', '6', '9', '#' },
{ 'A', 'B', 'C', 'D' }
};
byte rowPins[ROWS] = { 11, 10, 9, 8 }; //digital connect to the row pins of the keypad
byte colPins[COLS] = { 7, 6, 5, 4 }; //digital connect to the column pints of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); //mapping digital with keypad
// define common variables
String displacementValue = ""; // User defined max displacement
String speedValue = "";
double displacement;
double speedData;
int pressedA = 0; //Initialize key A state
int pressedB = 0; //Initialize key B state
int pressedA2 = 0; //Initialize key A state
int pressedB2 = 0; //Initialize key B state
int speedCheckA = 0;
int speedCheckB = 0;
int next = 0; //define motors operation
double steps = 0; //displacement translated as motor steps
int numCheck = 0;
double validMM = 100; //Valid mm range
double validIN = 100; //Valid in range
double validMMs = 100; //Valid mm range
float validINs = 100; //Valid in range
unsigned long int t1 = 0; //time region
unsigned long int t2 = 0;
int i = 0;
int dir = 0; //motors direction
int mode2 = 0; //motors mode
int z = 1;
int num =4;
String unit;
const int pinA = 20, pinB = 21, phaseLookup[] = {0,-1,1,-1,1,0,-1,-1,-1,1,0,1,1,1,-1,0};
const int pinC = 3, pinD = 2;
volatile bool A = false, B = false, updated = false;
volatile int counter_1 = 0, counter_2 = 0, phase = 0;
//holding variable
char key3;
char key5;
char holdKey;
unsigned long t_hold;
// Initial function setup
void setup() {
// Serial initial
Serial.begin(115200);
lcd.begin(20, 4);
lcd2.begin(20, 4);
// Case SD not detected
if (!SD.begin(PIN_SPI_CS)) {
Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
lcd.setCursor(0,0);
lcd.print("SD CARD FAILED,");
lcd.setCursor(0,1);
lcd.print("OR NOT PRESENT!");
while (1); // don't do anything more:
}
// SD detected
Serial.println(F("SD CARD INITIALIZED."));
SD.remove("arduino.txt"); // delete the file if existed
// Create new file by opening file for writing
myFile = SD.open("arduino.txt", FILE_WRITE);
// Recording in new file
if (myFile) {
Serial.println("SD is recording");
//lcd.print("SD is recording");
} else {
Serial.print(F("SD Card: error on opening file arduino.txt"));
//lcd.print(F("SD Card: error on opening file arduino.txt"));
}
Serial.println("HX710B Demo with HX711 Library");
Serial.println("Initializing the scale");
/*
// Loadcell initialize
LoadCell_1.begin();
LoadCell_2.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
float calibrationValue_1; // calibration value
float calibrationValue_2;
calibrationValue_1 = 0.42;
calibrationValue_2 = 0.42;
unsigned long stabilizingtime = 2000;
boolean _tare = true; //perform tare
byte loadcell_1_rdy = 0;
byte loadcell_2_rdy = 0;
while ((loadcell_1_rdy + loadcell_2_rdy) < 2) { //run startup, stabilization and tare, both modules simultaniously
if (!loadcell_1_rdy) loadcell_1_rdy = LoadCell_1.startMultiple(stabilizingtime, _tare);
if (!loadcell_2_rdy) loadcell_2_rdy = LoadCell_2.startMultiple(stabilizingtime, _tare);
}
if (LoadCell_1.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 no.1 wiring and pin designations");
}
if (LoadCell_2.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 no.2 wiring and pin designations");
}
LoadCell_1.setCalFactor(calibrationValue_1); // user set calibration value (float)
LoadCell_2.setCalFactor(calibrationValue_2); // user set calibration value (float)
*/
Serial.println("Startup is complete");
// Display start up commands via lcd
lcd.setCursor(0, 0);
lcd.print(" Press A to Start");
lcd2.setCursor(0, 0);
lcd2.print(" Press A to Start");
Serial.println(" Press A to Start");
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
pinMode(pinC, INPUT);
pinMode(pinD, INPUT);
attachInterrupt(digitalPinToInterrupt(pinA), trig_1, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinB), trig_1, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinC), trig_2, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinD), trig_2, CHANGE);
//digital pin for output
// pinMode(TRIG_PIN_1, OUTPUT);
// pinMode(ECHO_PIN_1, INPUT);
// pinMode(TRIG_PIN_2, OUTPUT);
// pinMode(ECHO_PIN_2, INPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// pinMode(stepPin2, OUTPUT);
// pinMode(dirPin2, OUTPUT);
stepper.setMaxSpeed(10000);
// stepper2.setMaxSpeed(1000);
digitalWrite(dirPin, HIGH);
// digitalWrite(dirPin2, HIGH);
stepper.setSpeed(0);
stepper.runSpeed();
}
// loop function for the system
void loop() {
// 8mm per rev; 0.31496063 in per rev;
// 200 steps per rev; 0.125 rev per mm; 1/0.31496063 rev per in;
// 8 mm per 200 steps -> 0.04 mm per steps -> 25 steps per mm
// 0.31496063 in per 200 steps -> 0.0015748 in per steps -> 635 steps per in
// 1 mm per sec -> 25 steps per sec
// 0.03937 in per sec -> 25 steps per sec
// steps = 0.125*displacement*200
// steps = 1/0.31496063*displacement*200
// translate User defined max displacement to step
int num =4;
displacement = displacementValue.toDouble();
if (pressedA == 1) {
steps = 1 / 0.31496063 * displacement * 200*3.3;
}
if (pressedB == 1) {
steps = 0.125 * displacement * 200*3.3;
}
// Translate User defined speed to motor speed
double speed = speedValue.toDouble();
if (speedCheckA == 1) {
speedData = 25.4*speed * 32.67;
speed = speedData;
}
else if (speedCheckB == 1){
speedData = speed * 32.67;
speed = speedData;
}
speedData = 2000;
myFile.print("Trial ");
myFile.print(z);
myFile.println(" -------------------------------------");
// Enter mode 1 of the system
if (next == 1) {
// Clear LCD
lcd.clear();
lcd2.clear();
// Speed of mode 2
if (mode2 == 1) {
speed = 0; // motor inital speed
}
lcd.setCursor(0, 0);
if (mode2 == 0) {
lcd.print("Mode1:Extend/Retract");
Serial.println("Mode1:Extend/Retract");
}
if (mode2 == 1) {
lcd.print("Mode2:Manual Control");
Serial.println("Mode2:Manual Control");
}
// Data collection and LCD display loop
while (next == 1)
{
if (updated)
{
if (pressedA == 1) {
unit = "in";
}
if (pressedB == 1) {
unit = "mm";
}
Serial.print((counter_1*0.005),3);
Serial.print(" ");
Serial.print(unit);
Serial.print(" ");
Serial.print((counter_2*0.005),3);
Serial.print(" ");
Serial.print(unit);
Serial.print(" ");
lcd2.setCursor(0,0);
lcd2.print("X: ");
if (pressedA == 1){
lcd2.print((counter_1*0.005/25.4),3);
}
if (pressedB == 1){
lcd2.print((counter_1*0.005),3);
}
lcd2.print(" ");
lcd2.print(unit);
lcd2.print(" ");
lcd2.setCursor(0,1);
lcd2.print("Y: ");
if (pressedA == 1){
lcd2.print((counter_2*0.005/25.4),3);
}
if (pressedB == 1){
lcd2.print((counter_2*0.005),3);
}
lcd2.print(" ");
lcd2.print(unit);
lcd2.print(" ");
updated = false;
}
// display data using imperial units
if (pressedA == 1) {
//float distance_1 = readDistanceCM(TRIG_PIN_1, ECHO_PIN_1);
//float distance_2 = readDistanceCM(TRIG_PIN_2, ECHO_PIN_2);
lcd.setCursor(0, 1);
lcd.print("X: ");
lcd.print(-1*stepper.currentPosition() / (steps/displacement), 4);
lcd.print(" in");
// lcd.setCursor(0, 2);
// lcd.print("Y = ");
// lcd.print(stepper2.currentPosition() / (1 / 0.31496063) / 200, 4);
// lcd.print(" in");
// lcd2.setCursor(0, 2);
// lcd2.print("X = ");
// lcd2.print((-readDistanceCM(TRIG_PIN_1, ECHO_PIN_1)) * 0.39370079+4.3307,4);
// lcd2.setCursor(17, 2);
// lcd2.print(" in");
// lcd2.setCursor(0, 3);
// lcd2.print("Y = ");
// lcd2.print((-readDistanceCM(TRIG_PIN_2, ECHO_PIN_2)) * 0.39370079+4.3307,4);
// lcd2.setCursor(17, 3);
// lcd2.print(" in");
// Uncomment below to display via serial monitor
Serial.print("X: ");
Serial.print(-1*stepper.currentPosition() / (steps/displacement), 4);
Serial.print(" in ");
// Serial.print("Y = ");
// Serial.print(stepper2.currentPosition() / (1 / 0.31496063) / 200, 4);
// Serial.print(" in ");
Serial.print("X: ");
Serial.print((counter_1*0.005/25.4),3);
Serial.print(" in ");
Serial.print("Y: ");
Serial.print((counter_2*0.005/25.4),3);
Serial.print(" in ");
// Serial.print("X = ");
// Serial.print((readDistanceCM(TRIG_PIN_1, ECHO_PIN_1))*0.39370079);
// Serial.print(" in ");
// Serial.print("Y = ");
// Serial.print((readDistanceCM(TRIG_PIN_2, ECHO_PIN_2))*0.39370079);
// Serial.print(" in ");
// Save data to sd card
//Loadcell();
myFile.print("X: ");
myFile.print(-1*stepper.currentPosition() / (steps/displacement), 4);
myFile.print(" in ");
// myFile.print("Y = ");
// myFile.print(stepper2.currentPosition() / (1 / 0.31496063) / 200, 4);
// myFile.print(" in");
myFile.print("X: ");
myFile.print((counter_1*0.005/25.4),3);
myFile.print(" in ");
myFile.print("Y: ");
myFile.print((counter_2*0.005/25.4),3);
myFile.print(" in ");
// myFile.print(" X = ");
// myFile.print((-readDistanceCM(TRIG_PIN_1, ECHO_PIN_1)) * 0.39370079+4.3307,4);
// myFile.print(" in ");
// myFile.print("Y = ");
// myFile.print((-readDistanceCM(TRIG_PIN_2, ECHO_PIN_2)) * 0.39370079+4.3307,4);
// myFile.print(" in ");
/*
// Save load cell data to SD card at every time period
float a = LoadCell_1.getData();
float b = LoadCell_2.getData();
String c = "g";
if (pressedA == 1) {
a = a * 0.00220462;
b = b * 0.00220462;
c = "lb";
}
// Reduce digits after decimal
int num =4;
if (a >= 10 | b >=10){
num =3;
}
myFile.print(" X Load = ");
myFile.print(a, num);
myFile.print(" ");
myFile.print(c);
myFile.print(" Y Load = ");
myFile.print(b, num);
myFile.print(" ");
myFile.print(c);
*/
}
// display data using metric units
if (pressedB == 1) {
// float distance_1 = readDistanceCM(TRIG_PIN_1, ECHO_PIN_1);
// float distance_2 = readDistanceCM(TRIG_PIN_2, ECHO_PIN_2);
lcd.setCursor(0, 1);
lcd.print("X: ");
lcd.print(-1*stepper.currentPosition() /(steps/displacement), 4);
lcd.print(" mm");
// lcd.setCursor(0, 2);
// lcd.print("Y = ");
// lcd.print(stepper2.currentPosition() / 0.125 / 200, 4);
// lcd.print(" mm");
// lcd2.setCursor(0, 2);
// lcd2.print("X = ");
// lcd2.print((-readDistanceCM(TRIG_PIN_1, ECHO_PIN_1)) * 10+110,4);
// lcd2.setCursor(17, 2);
// lcd2.print(" mm");
// lcd2.setCursor(0, 3);
// lcd2.print("Y = ");
// lcd2.print((-readDistanceCM(TRIG_PIN_2, ECHO_PIN_2)) * 10+110,4);
// lcd2.setCursor(17, 3);
// lcd2.print(" mm");
// Uncomment below to display via serial monitor
Serial.print("X: ");
Serial.print(-1*stepper.currentPosition() / (steps/displacement), 4);
Serial.print(" mm ");
// Serial.print("Y = ");
// Serial.print(stepper2.currentPosition() / 0.125 / 200, 4);
// Serial.print(" mm ");
Serial.print("X: ");
Serial.print((counter_1*0.005),3);
Serial.print(" mm ");
Serial.print("Y: ");
Serial.print((counter_2*0.005),3);
Serial.print(" mm ");
// Serial.print("X = ");
// Serial.print((readDistanceCM(TRIG_PIN_1, ECHO_PIN_1))*10);
// Serial.print(" mm ");
// Serial.print("Y = ");
// Serial.print((readDistanceCM(TRIG_PIN_2, ECHO_PIN_2))*10);
// Serial.print(" mm ");
// Save data to SD card
//Loadcell();
myFile.print("X: ");
myFile.print(-1*stepper.currentPosition() / (steps/displacement), 4);
myFile.print(" mm ");
// myFile.print("Y = ");
// myFile.print(stepper2.currentPosition() / 0.125 / 200, 4);
// myFile.print(" mm");
myFile.print("X: ");
myFile.print((counter_1*0.005),3);
myFile.print(" mm ");
myFile.print("Y: ");
myFile.print((counter_2*0.005),3);
myFile.print(" mm ");
// myFile.print(" X = ");
// myFile.print((-readDistanceCM(TRIG_PIN_1, ECHO_PIN_1)) * 10+110,4);
// myFile.print(" mm ");
// myFile.print("Y = ");
// myFile.print((-readDistanceCM(TRIG_PIN_2, ECHO_PIN_2)) * 10+110,4);
// myFile.print(" mm ");
/*
// Save load cell data to SD card at every time period
float a = LoadCell_1.getData();
float b = LoadCell_2.getData();
String c = "g";
if (pressedA == 1) {
a = a * 0.00220462;
b = b * 0.00220462;
c = "lb";
}
int num =4;
if (a >= 10 | b >=10){
num =3;
}
myFile.print(" X Load = ");
myFile.print(a, num);
myFile.print(" ");
myFile.print(c);
myFile.print(" Y Load = ");
myFile.print(b, num);
myFile.print(" ");
myFile.print(c);
*/
}
// display time data
t2 = millis();
Serial.print(t2 - t1);
Serial.println(" ms");
lcd.setCursor(0, 3);
lcd.print("t = ");
lcd.print(t2 - t1);
lcd.print(" ms");
myFile.print(" t = ");
myFile.print(t2 - t1);
myFile.println(" ms ");
// enter mode 2 operation
if (mode2 == 1) {
if (key3) {
holdKey = key3;
}
// Enter if key is pressed
if (keypad.getState() == PRESSED) {
if (holdKey == 'B' && -1*stepper.currentPosition() < steps) {
speed = -speedData;
}
else if (holdKey == 'D') {
//else if (holdKey == 'C' && -1*stepper.currentPosition() > 0) {
speed = speedData;
}
else if (holdKey == '#') {
//else if (holdKey == 'C' && -1*stepper.currentPosition() > 0) {
speed = -speedData;
}
else if (holdKey == 'C' && -1*stepper.currentPosition() > 0) {
speed = speedData;
}
else {
speed = 0;
}
}
// Enter if key is held
else if (keypad.getState() == HOLD) {
if ((millis() - t_hold) > 0 ) {
if (holdKey == 'B' && -1*stepper.currentPosition() < steps) {
speed = -speedData;
}
else if (holdKey == 'D') {
//else if (holdKey == 'C' && -1*stepper.currentPosition() > 0) {
speed = speedData;
}
else if (holdKey == '#') {
//else if (holdKey == 'C' && -1*stepper.currentPosition() > 0) {
speed = -speedData;
}
else if (holdKey == 'C' && -1*stepper.currentPosition() > 0) {
speed = speedData;
}
else {
speed = 0;
}
t_hold = millis();
}
}
// Enter if idle
else {
speed = 0;
}
//motor running
stepper.setSpeed(speed);
stepper.runSpeed();
//stepper2.setSpeed(speed);
//stepper2.runSpeed();
}
// Enter mode 1 operation if operation 2 didnt run
// Motor extension
else if (-1*stepper.currentPosition() <= steps && dir == 0) {
speed = -speedData;
stepper.setSpeed(speed);
stepper.runSpeed();
//stepper2.setSpeed(speed);
//stepper2.runSpeed();
// change direction
if (-1*stepper.currentPosition() > steps) {
dir = 1;
}
}
// Motor retraction
else if (-1*stepper.currentPosition() >= 0 && dir == 1) {
speed = speedData;
stepper.setSpeed(speed);
stepper.runSpeed();
//stepper2.setSpeed(-speed);
//stepper2.runSpeed();
// change direction
if (-1*stepper.currentPosition() < 0) {
dir = 0;
}
}
// stop motors
key3 = keypad.getKey();
if (key3 == 'A') {
speed = 0;
lcd.clear();
lcd2.clear();
lcd.setCursor(0, 0);
lcd.print("Motors Stopped");
Serial.println("Motors Stopped");
lcd2.setCursor(0, 0);
lcd2.print("Press A to Continue");
Serial.println("Press A to Continue");
lcd2.setCursor(0, 2);
lcd2.print("Press B to reset");
lcd2.setCursor(0, 3);
lcd2.print("Displacement");
next = 0;
validMM = 100;
validIN = 100;
validMMs = 100;
validINs = 100;
myFile.close();
Serial.println("------Reading Data from SD------");
myFile = SD.open("arduino.txt", FILE_READ);
//while (myFile.available()) {
// char ch = myFile.read(); // read characters one by one from Micro SD Card
// Serial.print(ch); // print the character to Serial Monitor
//}
myFile.close();
z++;
key5 = 'B';
while (key5 != 'A'){
key5 = keypad.getKey();
if (key5 == 'B') {
//stepper.setAcceleration(1000);
stepper.runToNewPosition(0);
//stepper2.setAcceleration(1000);
//stepper2.runToNewPosition(0);
}
}
}
i++;
}
}
int dot = 1;
// User interface
while (validIN > 1 && validMM > 25.4) {
char key = keypad.getKey(); //key is input
if (key != NO_KEY) { // When pressed
// Request max displacement
if (key == 'A' & next == 0) { //Continue after A is pressed
myFile = SD.open("arduino.txt", FILE_WRITE);
lcd.clear();
lcd2.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Max");
lcd.setCursor(0, 1);
lcd.print("Displacement: in");
lcd.setCursor(0, 2);
lcd.print("0 - 1 in");
Serial.println("Enter Max Displacement: in 0 - 1 in");
dot = 1;
displacementValue = "";
numCheck = 0;
pressedA = 1;
pressedB = 0;
}
// Change to metrics
if (key == 'B' & next == 0 & pressedA == 1) { //Continue after B is pressed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Max");
lcd.setCursor(0, 1);
lcd.print("Displacement: mm");
lcd.setCursor(0, 2);
lcd.print("0 - 25.4 mm");
Serial.println("Enter Max Displacement: mm 0 - 25.4 mm");
dot = 1;
displacementValue = "";
numCheck = 0;
pressedA = 0;
pressedB = 1;
}
// Enter numerical values
if (key != 'A' && key != 'B' && key != 'C' && key != 'D' && key != '*' && key != '#' && (pressedA == 1 || pressedB == 1)) {
displacementValue = displacementValue + key; //Reset value
lcd.clear();
lcd2.clear();
if (pressedA == 1) {
lcd2.setCursor(0, 2);
lcd2.print("0 - 1 in");
}
if (pressedB == 1) {
lcd2.setCursor(0, 2);
lcd2.print("0 - 25.4 mm");
}
lcd2.setCursor(0,0);
lcd2.print("D to enter");
lcd.setCursor(0, 0);
lcd.print(displacementValue); //display key input
Serial.println(displacementValue);
numCheck = 1;
}
// Allow decimal values
if (key == '*' && dot == 1 && (pressedA == 1 || pressedB == 1)) {
displacementValue = displacementValue + '.'; //Reset value
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(displacementValue); //display key input
Serial.println(displacementValue);
dot = 0;
}
// Key evaluation
if (key == 'D' && numCheck == 1) { //if key D is pressed
dot = 1;
if (pressedA == 1) {
validIN = displacementValue.toDouble();
}
if (pressedB == 1) {
validMM = displacementValue.toDouble();
}
// Display input in imperial (Incorrect ver.)
if (validIN > 1 && pressedA == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Max Displacement");
lcd.setCursor(0, 1);
lcd.print("is too large");
Serial.println("Max Displacement is too large");
delay (2000);
lcd.setCursor(0, 0);
lcd.print("Enter Max");
lcd.setCursor(0, 1);
lcd.print("Displacement: in");
Serial.println("Enter Max Displacement: in");
displacementValue = "";
}
// Display input in metrics (Incorrect ver.)
else if (validMM > 25.4 && pressedB == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Max Displacement");
lcd.setCursor(0, 1);
lcd.print("is too large");
Serial.println("Max Displacement is too large");
delay (2000);
lcd.setCursor(0, 0);
lcd.print("Enter Max");
lcd.setCursor(0, 1);
lcd.print("Displacement: mm");
Serial.println("Enter Max Displacement: mm");
displacementValue = "";
}
// Display inputs
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Displacement: ");
lcd.setCursor(0, 1);
Serial.println("Displacement: ");
lcd.print(displacementValue); //display key input
Serial.print(displacementValue);
if (pressedA == 1) {
lcd.print(" in");
Serial.println(" in");
}
else {
lcd.print(" mm");
Serial.println(" mm");
}
delay(2000);
}
}
}
}
// Request Speed
lcd.clear();
lcd2.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Speed: in");
lcd.setCursor(0, 1);
lcd.print("(0.003937 -");
lcd.setCursor(0, 2);
lcd.print("0.03937 in/s)");
Serial.println("Enter Speed: in 0.003937 - 0.03937 in/s)");
speedValue = "";
speedCheckA = 1;
speedCheckB = 0;
numCheck = 0;
int pressedA2 = 1;
int pressedB2 = 0;
dot = 1;
while ((validINs > 0.0393701 | validMMs > 1) | (validINs < 0.003937 | validMMs < 0.1)) {
char key4 = keypad.getKey(); //key is input
if (key4 != NO_KEY) { // When pressed
if (key4 == 'A' & next == 0) { //Continue after A is pressed
myFile = SD.open("arduino.txt", FILE_WRITE);
lcd.clear();
lcd2.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Speed: in");
lcd.setCursor(0, 1);
lcd.print("(0.003937 -");
lcd.setCursor(0, 2);
lcd.print("0.03937 in/s)");
Serial.println("Enter Speed: in 0.003937 - 0.03937 in/s)");
dot = 1;
speedValue = "";
speedCheckA = 1;
speedCheckB = 0;
numCheck = 0;
pressedA2 = 1;
pressedB2 = 0;
}
// Change to metrics
if (key4 == 'B' & next == 0) { //Continue after B is pressed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Speed: mm");
lcd.setCursor(0, 1);
lcd.print("(0.1 - 1 mm/s)");
Serial.println("Enter Speed: mm 0.1 - 1 mm/s)");
dot = 1;
speedValue = "";
speedCheckA = 0;
speedCheckB = 1;
numCheck = 0;
pressedA2 = 0;
pressedB2 = 1;
}
// Enter numerical values
if (key4 != 'A' && key4 != 'B' && key4 != 'C' && key4 != 'D' && key4 != '*' && key4 != '#' && (pressedA2 == 1 || pressedB2 == 1)) {
speedValue = speedValue + key4; //Reset value
lcd.clear();
lcd2.clear();
if (pressedA2 == 1) {
lcd2.setCursor(0, 2);
lcd2.print("(0.003937 -");
lcd2.setCursor(0, 3);
lcd2.print("0.03937 in/s)");
}
if (pressedB2 == 1) {
lcd2.setCursor(0, 2);
lcd2.print("0.1 - 1 mm");
}
lcd2.setCursor(0,0);
lcd2.print("D to enter");
lcd.setCursor(0, 0);
lcd.print(speedValue); //display key input
Serial.println(speedValue);
numCheck = 1;
}
// Allow decimal values
if (key4 == '*' && dot == 1 && (pressedA2 == 1 || pressedB2 == 1)) {
speedValue = speedValue + '.'; //Reset value
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(speedValue); //display key input
Serial.println(speedValue);
dot = 0;
}
// Key evaluation
if (key4 == 'D' && numCheck == 1) { //if key D is pressed
dot = 1;
if (pressedA2 == 1) {
validINs = speedValue.toDouble();
validMMs = validINs*25.4;
}
if (pressedB2 == 1) {
validMMs = speedValue.toDouble();
validINs = validMMs/25.4;
}
// Display input in imperial (Incorrect ver.)
if ((validINs > 0.03937 || validINs < 0.003937) && pressedA2 == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Speed");
delay (2000);
lcd.setCursor(0, 0);
lcd.print("Enter Speed: in");
lcd.setCursor(0, 1);
lcd.print("(0.003937 -");
lcd.setCursor(0, 2);
lcd.print("0.03937 in/s)");
Serial.println("Invalid Speed Enter Speed: in (0.003937 -0.03937 in/s)");
speedValue = "";
}
// Display input in metrics (Incorrect ver.)
else if ((validMMs > 1 || validMMs < 0.1) && pressedB2 == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Speed");
delay (2000);
lcd.setCursor(0, 0);
lcd.print("Enter Speed: mm");
lcd.setCursor(0, 1);
lcd.print("(0.1 - 1 mm/s)");
Serial.println("Invalid Speed Enter Speed: mm (0.1 - 1 mm/s)");
speedValue = "";
}
// Display inputs
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Displacement: ");
Serial.println("Displacement: ");
lcd.setCursor(0, 1);
lcd.print(displacementValue); //display key input
Serial.print(displacementValue);
if (pressedA == 1) {
lcd.print(" in");
Serial.println(" in");
}
else {
lcd.print(" mm");
Serial.println(" mm");
}
lcd.setCursor(0, 2);
lcd.print("Speed: ");
Serial.println("Speed: ");
lcd.setCursor(0, 3);
lcd.print(speedValue); //display key input
Serial.print(speedValue);
if (pressedA2 == 1) {
lcd.print(" in/s");
Serial.println(" in/s");
}
else {
lcd.print(" mm/s");
Serial.println(" mm/s");
}
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode1:Extend/Retract");
Serial.println("Mode1:Extend/Retract");
lcd.setCursor(0, 1);
lcd.print("Mode2:Manual Control");
Serial.println("Mode2:Manual Control");
// Leading to mode 1 or mode 2
char key2 = keypad.getKey();
while (next == 0) {
key2 = keypad.getKey();
if (key2 == '1') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode1:Extend/Retract");
Serial.println("Mode1:Extend/Retract");
next = 1;
mode2 = 0; // enter mode 1
}
if (key2 == '2') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode2:Manual Control");
Serial.println("Mode2:Manual Control");
next = 1;
mode2 = 1; // enter mode 2
}
}
}
}
}
}
delay(1000);
t1 = millis(); // record time at then end of loop
}
/*
// Load cell function
void Loadcell() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell_1.update()) newDataReady = true;
LoadCell_2.update();
//get smoothed value from data set
if ((newDataReady)) {
if (millis() > t + serialPrintInterval) {
float a = LoadCell_1.getData();
float b = LoadCell_2.getData();
String c = "g";
lcd2.setCursor(0, 0);
lcd2.print("X Load = ");
if (pressedA == 1) {
a = a * 0.00220462;
b = b * 0.00220462;
c = "lb";
}
int num =4;
if (a >= 10 | b >=10){
num =3;
}
lcd2.print(a, num);
lcd2.setCursor(18, 0);
lcd2.print(c);
lcd2.setCursor(0, 1);
lcd2.print("Y Load = ");
lcd2.print(b, num);
lcd2.setCursor(18, 1);
lcd2.print(c);
newDataReady = 0;
t = millis();
}
}
// receive command from serial terminal, send 't' to initiate tare operation:
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') {
LoadCell_1.tareNoDelay();
LoadCell_2.tareNoDelay();
}
}
/*
//check if last tare operation is complete
if (LoadCell_1.getTareStatus() == true) {
lcd2.setCursor(0,2);
lcd2.print("Tare load cell 1 complete");
}
if (LoadCell_2.getTareStatus() == true) {
lcd2.setCursor(0,3);
lcd2.print("Tare load cell 2 complete");
}
}
// Ultra sonic sensor function
float readDistanceCM(int trig_pin, int echo_pin) {
digitalWrite(trig_pin, LOW);
//delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
//delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
int duration = pulseIn(echo_pin, HIGH);
//if((duration * 0.034 / 2) <= 2){
// duration =2;
// return duration * 0.034 / 2;
// }
//else{
return duration * 0.034 / 2;
//}
}
*/
void trig_1()
{
A = digitalRead(pinA);
B = digitalRead(pinB);
phase <<= 1;
phase |= A;
phase <<= 1;
phase |= B;
phase &= 0xF;
counter_1 += phaseLookup[phase];
updated = true;
}
void trig_2()
{
A = digitalRead(pinC);
B = digitalRead(pinD);
phase <<= 1;
phase |= A;
phase <<= 1;
phase |= B;
phase &= 0xF;
counter_2 += phaseLookup[phase];
updated = true;
}