#include <LiquidCrystal.h> //screen arduino library
#include <Stepper.h> //stepper library
/* Display and motor decleration */
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); //declares screen wiring
Stepper myStepper(200, 6, 7); //Setsup stepper motor, steps per rev, dir, step
/*Variable Decleration*/
////////////////////////////////////////////////////////////////////////////
//pot variables
int val; //Takes position of menu potentiometer (left)
double val2; // Takes position for variable potentiometer (right)
double val3; //New pot reading to find cycle number
//button decleration + varibles
#define BUTTON_PIN 5 //defines button wiring
int potpin = A0; //tells arduino position of first potentiometer
int potpin2 = A1; //tells arduino position of second potentiometer
int lastState = HIGH; //sets button as not pressed at start
//cyclebox variables
double cycles = 0, time_on = 0, time_off = 0; //variables
int collapse = 0;
float stepNUM;
double S_time = 0; //final calculation
//Condition variables
int jamie;
bool x, y, t1, t2, yes = false; //tests if button is pressed
////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600); //serial monitor freq
lcd.begin(16, 2); // sets display size 16x2 pixles
lcd.clear(); //clears screen for cursor
lcd.setCursor(0, 0); //sets cursor position
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
/*main body*/
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (0-1023)
val = map(val, 0, 1023, 0, 360); //scales potentiometer to 0-360
val2 = analogRead(potpin2);//potentiometer for setting variables
val2 = map(val2, 0, 1023, 4, 360); //scales to 0-360
double val3 = analogRead(potpin2);
int button = digitalRead((BUTTON_PIN)); //reads button value
if (jamie == 1)
{
if (button == LOW && jamie == 1)
{
for (float count = 0; count <= stepNUM; count++)
{
myStepper.step(-4);
delay(10);
lcd.clear();
}
jamie = 0;
yes = false;
stepNUM = 0;
S_time = 0;
}
else {
lcd.clear();
lcd.print("Test Complete");
lcd.setCursor(0, 1);
lcd.print("Press to reset");
delay(500);
jamie = 1;
S_time = 0;
}
}
else {
delay(250);
if (val >= 0 && val <= 60)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cummins Meritor");
lcd.setCursor(0, 1);
lcd.print("Clockwork Rig");
}
// Input number of cycles
if (val >= 61 && val <= 120)
{
lcd.clear();
lcd.print("Cycles: ");
double display = round(val3 * 100);
lcd.print(display, 0);
lcd.setCursor(0, 1);
lcd.print("Currently:");
lcd.print(cycles, 0);
if (button == LOW)
{
cycles = display;
}
}
//input %collapse
if (val >= 121 && val <= 180)
{
lcd.clear();
lcd.print("Collapse: ");
int display = val2 / 9;
lcd.print(display);
lcd.print("mm");
lcd.setCursor(0, 1);
lcd.print("Currently: ");
lcd.print( collapse);
lcd.print("mm");
if (button == LOW)
{
collapse = display;
}
else
{
collapse = collapse;
}
}
//input time off
if (val >= 181 && val <= 240)
{
lcd.clear();
lcd.print("On time: ");
double display = ((val2 / 36) / 10) * 4.5;
display = 2 * display;
display = round(display);
display = (display / 2) + 0.5;
lcd.print(display);
lcd.print("s");
lcd.setCursor(0, 1);
lcd.print("Currently: ");
lcd.print( time_on);
lcd.print("s");
if (button == LOW)
{
time_on = display;
}
else
{
time_on = time_on;
}
}
//input time on
if (val >= 241 && val <= 300)
{
lcd.clear();
lcd.print("Off time: ");
double display = ((val2 / 36) / 10) * 4.5;
display = 2 * display;
display = round(display);
display = (display / 2) + 0.5;
lcd.print(display);
lcd.print("s");
lcd.setCursor(0, 1);
lcd.print("Currently: ");
lcd.print(time_off);
lcd.print("s");
if (button == LOW && display > 0)
{
time_off = display;
}
else
{
time_off = time_off;
}
}
//To run the rig
if (val >= 301 && val <= 360)
{
lcd.clear();
lcd.print("Start test?");
lcd.setCursor(0, 1);
lcd.print("Press for OK");
//Checks if all values have been input
if (cycles > 0 && collapse > 0 && time_on > 0 && time_off > 0)
{
yes = true;
}
else
{
yes == false;
}
if (button == LOW && yes == true)
{
double total_time = time_on + time_off;
S_time = (total_time * cycles) / (0.0178 * collapse * 2.5 * 0.5);
//1:2 gear ratio achieved must account for it also account for mm travel by /2.5
stepNUM = collapse * 44.5;
Serial.println(S_time);
}
if (button == LOW && yes == false)
{
lcd.clear();
lcd.print("Missing some");
lcd.setCursor(0, 1);
lcd.print("variables");
}
}
if (!S_time == 0) //if S_time is not =0 then step the motor
{
for (float count = 0; count <= (stepNUM + 1); count++)
{
int complete = 100 * (count / stepNUM);
lcd.clear();
lcd.print("Progress:");
lcd.setCursor(0, 1);
lcd.print(complete);
lcd.print(" % ");
myStepper.step(4);
delay(S_time);
lcd.clear();
if (complete >= 100.00)
{
S_time = 0;
cycles = 0;
collapse = 0;
time_on = 0;
time_off = 0;
jamie = 1;
delay(100);
}
}
}
}
}