#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
int a;
int b;
unsigned int impact_Command;
int j = 1;
int j2 = 0;
//Serial Communication
const unsigned int max_message_length = 12; // MAX serial message length
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
Serial.begin(9600);
Serial.println("Serial Ready");
lcd.print(" SIMULATOR");
lcd.setCursor(0,1);
}
void loop() {
//Check to see if anything is available in the serial receive buffer
if (Serial.available()) { //Check to see if Serial command is available
String data_from_display = ""; //Empty String to collect serial data
delay(30); //30ms delay to catch entire string without double touch
while (Serial.available()) {
data_from_display += char(Serial.read()); //Collect data and append to itself
//Serial.println(data_from_display); //Debugging Test - should send data from the display to the serial monitor
primaryControl(data_from_display); //Debugging &
}
if (data_from_display.startsWith("g_")){
//command = Serial.parseInt();
static_Impact(data_from_display);
}
}
if(impact_Command == 1){
//Serial.println(j);
}
if (impact_Command == 2)
calibration_Impact();
}
void primaryControl(String data_from_display) {
//POWER CONTROLS
if (data_from_display == "list"){
Serial.print("Impact Command: ");
Serial.print(impact_Command);
Serial.print(" J: ");
Serial.print(impact_Command);
Serial.print(" J2: ");
Serial.println(j2);
}
if (data_from_display == "reset"){
j2 = 0;
}
if (data_from_display == "calibrate"){
Serial.println("Calibration Started");
impact_Command = 2;
}
if (data_from_display == "i12") {
lcd.setCursor(10,3);
lcd.print(" ON ");
Serial.println("IMPACT RECEIVED");
impact_Command = 1;
}
if (data_from_display == "OFF") {
//digitalWrite(VAC_POS, LOW); //Turns OFF relay to supply 12V+ to VAC
lcd.setCursor(10,3);
lcd.print(" OFF ");
Serial.println("THE COMMAND TO TURN OFF HAS BEEN RECEIVED");
impact_Command = 0;
delay(30);
j=0;
delay(30);
j2=j2;
//delay(30);
Serial.print("Impact Command is: ");
Serial.println(impact_Command);
Serial.print("J is now: ");
Serial.println(impact_Command);
Serial.print("J2 is now: ");
Serial.println(j2);
}
//INTERUPT TEST
if (data_from_display == "print"){
//Serial.println(command);
//command = "";
}
}
void static_Impact(String data_from_display){
int gen_command = data_from_display.substring(2).toInt();
switch (gen_command){
case 0: // 0.00G
Serial.println("gen_0 Received");
break;
case 1: // 0.25G
Serial.println("gen_1 Received");
break;
case 2: // 0.50G
Serial.println("gen_2 Received");
break;
case 3: // 0.75G
Serial.println("gen_3 Received");
break;
case 4: // 1.00G
Serial.println("gen_4 Received");
break;
case 5: // 1.25G
Serial.println("gen_5 Received");
break;
case 6: // 1.50G
Serial.println("gen_6 Received");
break;
case 7: // 1.75G
Serial.println("gen_7 Received");
break;
case 8: // 2.00G
Serial.println("gen_8 Received");
break;
case 9: // 2.25G
Serial.println("gen_9 Received");
break;
case 10: // 2.50G
Serial.println("gen_10 Received");
break;
}
}
void calibration_Impact(){
//TURN SERIAL GET INTO A FUNCTION
while (impact_Command == 2){
if (Serial.available()) { //Check to see if Serial command is available
String data_from_display = ""; //Empty String to collect serial data
delay(30); //30ms delay to catch entire string without double touch
while (Serial.available()) {
data_from_display += char(Serial.read()); //Collect data and append to itself
//Serial.println(data_from_display); //Debugging Test - should send data from the display to the serial monitor
primaryControl(data_from_display);
}
}
delay(1000); //NEED to use millis instead of delay
Serial.print("J2 is: ");
Serial.print(j2);
Serial.print(" J is: ");
Serial.println(j);
j2=++j2;
j=++j;
/*if(j==5){
//j2 = j2 + 1;
j = 0;
}
if (j2 >= 3){
Serial.println("BREAK OUT");
impact_Command = 0;
break;
}*/
}
}