// Red - 5V // Black - GND const int encoder_a = 2; // Green - pin 2 - Digital const int encoder_b = 3; // White - pin 3 - Digital volatile float encoder = 0; float encoderCal = 0; int LED = 13; char incomingByte = 0; String command; String encoderString = "0"; unsigned long EresetVector = 0; void setup() { Serial.begin(9600); Serial1.begin(9600); pinMode(encoder_a, INPUT_PULLUP); pinMode(encoder_b, INPUT_PULLUP); attachInterrupt(0, encoderPinChangeA, CHANGE); attachInterrupt(1, encoderPinChangeB, CHANGE); pinMode(LED, OUTPUT); } void loop() { if (Serial.available() > 0) { EresetVector = millis(); SerialFunction(); if (millis() > EresetVector + 1000) { Serial1.println("encoder error"); return; } } } void encoderPinChangeA() { encoder += digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1; return; } void encoderPinChangeB() { encoder += digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1; return; } void SerialFunction() { if (Serial.available() > 0) //If data is coming in, stop and do this { incomingByte = Serial.read(); //anything coming in on the serial port is set as the chacter incomingByte Serial1.print(incomingByte); if (incomingByte == '\n') //If the user hits enter it should be a null character, meaning they're finished inputing a command { parseCommand(command); //Go to the function parseCommand and take the string called command with you Serial1.print("Command = "); Serial1.println(command); command = ""; //empty the string called command } else //if this is not a null character { command += incomingByte; //add the incoming Bytes to the string "command" } } return; } void parseCommand(String com) //This function takes the string "command" from the main loop, parses it, and then returns two values to be used by another function called MainlineFlowRate and RequiredFlowRate { String part1; //String part1 is used to find which command was sent. Either MainlineFlowRate or RequiredFlowRate String part2; //String part2 is used to find what the mainline flow rate is and what the desired flow rate is part1 = com.substring(0, com.indexOf(",")); //This reads the string command and sets part1 to the first word sent be the user Serial1.print("part1 "); Serial1.println(part1); if (part1.equalsIgnoreCase("ER")) //if the command from the serial port is MainlineFlowRate set the following two digits as the MainlineFlowRate { encoderCal = (encoder*.00056112024005143959419875687647353); //this line converts the raw encoder value to feet. Just run the lateral for a certain distacne and then dived the distance it went by the encoder value to get a calabration value. encoderString = ("ER,"); encoderString = (encoderString + encoderCal); Serial.println(encoderString); //set the 2 digits following the word MainlineFlowRate (part2) as the global integer MainlineFlowRate Serial1.println("encoder String"); //set the 2 digits following the word MainlineFlowRate (part2) as the global integer MainlineFlowRate Serial1.println(encoderString); //set the 2 digits following the word MainlineFlowRate (part2) as the global integer MainlineFlowRate encoderString = (""); } }