//*************************************************************** //Program: Mail_Launchpad.ino //IDE: Energia //Target Dev: TI Launchpad Experimenters Dev Boards //Description: Program listens for keyword ("MAIL") from //serial port then performs a set of functions including //activating a servo and turning on an LED. //A button press is handled by an Interrupt Suervice Routine (ISR) //that resets the system for the next receipt of the keyword. //*************************************************************** //Include need libraries #include //Define keywords and variables #define LED RED_LED //Using onboard red LED String inputString = ""; // store incoming serial data, make empty boolean stringComplete = false; // flag to determine if whole string has been read Servo servo; //define an instance of the servo class //*************************************************************** //Function: setup //Description: Initializes variables and setups onboard hardware //*************************************************************** void setup() { pinMode(LED, OUTPUT); digitalWrite(LED, LOW); //Turn LED off Serial.begin(9600); // initialize serial inputString.reserve(200); // reserve 200 bytes for inputString servo.attach(P1_2); //attach servo attachInterrupt(P3_0, resetISR, FALLING); //ISR to handle reset button press, looks for falling edge } //*************************************************************** //Function: loop //Description: Main repeating function. Listens from input on //serial port. If the string received matches keyword "MAIL" //then call then turn on LED and turn servo 90-degrees, raising //the flag. //*************************************************************** void loop() { if (stringComplete) { //Wait until entire string read Serial.println(inputString); //print string if (inputString.compareTo("Mail\n") == 0) { //If string matches keyword "MAIL" digitalWrite(LED, HIGH); // Turn onboard LED on servo.write(90); //PWM Code for rasing flag, turn 90-degrees } inputString = ""; // clear the string stringComplete = false; // set flag to false, now ready for next string } } //*************************************************************** //Function: serialEvent //Description: Handles reading the serial port, one char at a //time. //*************************************************************** void serialEvent() { while (Serial.available()) { //Is there data on serial port to be read? char inChar = (char)Serial.read(); //Get next unread char inputString += inChar; //Add that char to the string if (inChar == '\n') { //Once we reach end of line char, string is complete stringComplete = true; } } } //*************************************************************** //ISR: resetISR //Description: When falling edge of button press is detected, //this interrupt service routine will run regardless of where we //are in the program execution. //*************************************************************** void resetISR() { digitalWrite(LED, LOW); // Turn onboard LED off servo.write(0); //PWM code for lowering flag }