You are on page 1of 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 */ #include <Servo.h> // Connections to make: // Arduino Pin 5 -> // Arduino Pin 6 -> // Arduino GND -> // Arduino Pin A0 -> // Arduino Pin D2 -> Sabertooth S1 Sabertooth S2 Sabertooth 0V sonar zigbee

// 1000us - full reverse, 2000us - full forward, 1500us - stop // output led Servo ST1,ST2; String inputString = ""; boolean stringComplete = false; void setup() { pinMode(8, OUTPUT); pinMode(13, OUTPUT); Serial.begin(19200); inputString.reserve(200); Serial.begin(19200); ST1.attach( 5, 1000, 2000); ST2.attach( 6, 1000, 2000); } void left() { int i; delay(500); Serial.println("G19257699"); // print the string when a newline arrives: if (stringComplete) { for(i=0;i<=13;i++) { Serial.println(inputString[i]);

// a string to hold incoming data // whether the string is complete

// initialize serial: // reserve 200 bytes for the inputString:

if(inputString[12] >= 0x3a) { ST1.writeMicroseconds(1450); ST2.writeMicroseconds(1450); digitalWrite(13, HIGH); digitalWrite(8, LOW);} else { digitalWrite(13, LOW); ST1.writeMicroseconds(1550); ST2.writeMicroseconds(1550); digitalWrite(8, LOW); } } // clear the string: inputString = ""; stringComplete = false; } } void loop() { left(); } /* 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. */ void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar == '\n') { stringComplete = true; } } }

You might also like