My code, usually will listen to the serial line for an action and execute few patterns.
first, it will listen any start string which starts with '<' and end with '>'.
There are few patterns that involved the usage of 'delay', so i really need the interrupt to make sure,
like an example, when I'm running pattern 1, then suddenly when I click pattern2 on my vb application, it will listen and executes directly.
The patterns are situated in 345(); and 123(); subroutines.
here are some details of the code.
//*********** START LOOP FUNCTION********
//----WILL BE REPEATED THROUGHOUT THE PROCESS
void loop() {
inByte=0;
inByte = Serial.read();
string_len=0; // clear the array of inByte
if (inByte == '<'){
read_serial(); // If Start of line ("<") is found, call read_serial() function
}
checkbuttonstate();
}
//*********** END LOOP FUNCTION********
//*********** START CHECK BUTTON STATE SUBROUTINE********
void checkbuttonstate(){
if(buttautostate==0 && buttmanstate==1) // manual mode
{
if(buttmode1==1 && buttmode2==0 && buttmode3==0){ // check for song mode position
123(); //play 123 dialog
345();
buttmode1=0; // clear back status
}
if(buttmode1==0 && buttmode2==1 && buttmode3==0){ //play mission impossible song
missionimpossiblesequence();
buttmode2=0; //clear back status
}
if(buttmode1==0 && buttmode2==0 && buttmode3==1){ //play blue danube song
repeatbluedanube(); `
endbluedanube();
buttmode3=0; //clear back status
}
}
return;
}
.........................................
//*********** END CHECK BUTTON STATE SUBROUTINE********
void read_serial() // FUNCTION FOR READING THE SERIAL MESSAGE
{
Serial.println("processing");
while (inByte!= '>') // As long as end string not found, keep reading until 'break' found. break does not work with 'if'
{
if (Serial.available() > 0) // if new data is available, repeat this loop until Serial.available= 0
{
if(Serial.available()>1){
inByte = Serial.read(); // Read new byte
//Serial.print("READ : "); // Display the new byte
stringx[string_len] = inByte; // Save the data in a character array
// Serial.println(string[string_len]); // Print the characters that was recieved. -1 so that we would not read the '>' end string
string_len++; //add array
}
if(Serial.available()==1){ // if serial is on the last bit. receiving '>'
inByte=Serial.read(); // Read new byte. make it overflow as we dont want to save '>' in string.
}
}
else if (Serial.available() == 0) // if serial buffer has all been read, and still '>' does not found, execute this line
{
//Serial.println("end string not available, data string invalid"); // If end string not in the string
// Serial.println("$N"); //print FAIL SIGNATURE
Serial.flush(); // clear the serial buffer before reading new data
memset(stringx, 0, sizeof(stringx)); // clear the memory array
break; // get out of the while loop
}
}
if (inByte == '>') //only execute the command if the end string '>' found
{
//******* START PUMP STATUS CHECKING
if (strcmp(stringx, "mp-1") == 0){ // test to see if the two strings are equal
mainpmpsw_on();
Serial.println ("main pump is on"); //put a new line
// blinkLED(ledPin, 2, leddelay);
}
..............// end checkbuttonstate
void read_serial() // FUNCTION FOR READING THE SERIAL MESSAGE
{
Serial.println("processing");
while (inByte!= '>') // As long as end string not found, keep reading until 'break' found. break does not work with 'if'
{
if (Serial.available() > 0) // if new data is available, repeat this loop until Serial.available= 0
{
if(Serial.available()>1){
inByte = Serial.read(); // Read new byte
//Serial.print("READ : "); // Display the new byte
stringx[string_len] = inByte; // Save the data in a character array
// Serial.println(string[string_len]); // Print the characters that was recieved. -1 so that we would not read the '>' end string
string_len++; //add array
}
if(Serial.available()==1){ // if serial is on the last bit. receiving '>'
inByte=Serial.read(); // Read new byte. make it overflow as we dont want to save '>' in string.
}
}
else if (Serial.available() == 0) // if serial buffer has all been read, and still '>' does not found, execute this line
{
Serial.flush(); // clear the serial buffer before reading new data
memset(stringx, 0, sizeof(stringx)); // clear the memory array
break; // get out of the while loop
}
}
if (inByte == '>') //only execute the command if the end string '>' found
{
//******* START PUMP STATUS CHECKING
/*
if (strcmp(stringx, "ms-1") == 0){ // test to see if the two strings are equal
mainsw_on();
Serial.println ("main switch is on"); //put a new line
// blinkLED(ledPin, 2, leddelay); // blinkLED(ledPin, times, delay);
}
if (strcmp(stringx, "ms-0") == 0){ // test to see if the two strings are equal
mainsw_off();
Serial.println ("main switch is off"); //put a new line
//blinkLED(ledPin, 2, leddelay);
}
*/
if (strcmp(stringx, "mp-1") == 0){ // test to see if the two strings are equal
mainpmpsw_on();
Serial.println ("main pump is on"); //put a new line
// blinkLED(ledPin, 2, leddelay);
}
................ end void serial read
void 123(){
//print welcome screen first
printwelcomelcd();
//set delay for starting
delay(3000);
//play introduction to 123 - signal low
digitalWrite(7, LOW);
// delay for 123 dialog to play
delay(17000); //123 dialog finished
//set 123 dialog trigger pin off - signal high
digitalWrite(7, HIGH);
return;
}
//*********** END 123 SUBROUTINES ********