Final Arduino Code

download Final Arduino Code

of 2

Transcript of Final Arduino Code

  • 8/18/2019 Final Arduino Code

    1/2

    /*  Serial Event example

     When new serial data arrives, this sketch adds it to a String. When a newline is received, the loop prints the string and clears it.

     A good test for this is to try it with a GPS receiver that sends out NMEA 0183 sentences.

     Created 9 May 2011 by Tom Igoe

     This example code is in the public domain.

     http://www.arduino.cc/en/Tutorial/SerialEvent

     */

    String inputString = ""; // a string to hold incoming dataint stringComplete = 0; // whether the string is complete int p=0;void setup() {  // initialize serial:

      Serial.begin(9600);  Serial1.begin(9600);  // reserve 200 bytes for the inputString:  inputString.reserve(200);}

    void loop() { // serialEvent();// print the string when a newline arrives:  char inChar = Serial1.read();  if(inChar=='D')  {p=1;  Serial.println("np=1");

      }  if(p==1)  {  if(inChar!='Z'){

      Serial.println(inChar);  inputString+=inChar;  }  else if(inChar=='Z')  {  inputString+='\0';  Serial.println(inputString);  stringComplete= 1;

      }  if(stringComplete==1)  {  if(inputString=="DAAAA")  {  Serial.println("led 1 on");  inputString = "";  stringComplete = 0;  p=0;  }

  • 8/18/2019 Final Arduino Code

    2/2

      else if(inputString=="DBBBB")  {  Serial.println("led 1 OFF");  inputString = "";  stringComplete = 0;  p=0;  } else if(inputString=="DCCCC")

      {  Serial.println("led 2 on");  inputString = "";  stringComplete = 0;  p=0;  } 

    else if(inputString=="DDDDD")  {  Serial.println("led 2 OFF");  inputString = "";  stringComplete = 0;  p=0;  } 

    else  {  Serial.println(inputString);  Serial.println("notgot");  inputString = "";  stringComplete = 0;  p=0;  }  }  } delay(1000);}

    /*  SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available. */