Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
- other ATmega32U4 based boards.
- 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 data
- boolean stringComplete = false; // whether the string is complete
- int strLen = 0;
- String poruka = "Duzina stringa: ";
- String poruka1 = "Uneli ste: ";
- void setup() {
- // initialize serial:
- Serial.begin(9600);
- // reserve 200 bytes for the inputString:
- inputString.reserve(200);
- }
- void loop() {
- // print the string when a newline arrives:
- if (stringComplete) {
- strLen = inputString.length() - 1; // ne racunam \n karakter
- Serial.println(poruka1 + inputString);
- Serial.println(poruka + strLen);
- // proveravam palindrom
- proveriPalindrom();
- // clear the string:
- inputString = "";
- stringComplete = false;
- }
- }
- void proveriPalindrom(){
- String curr = "";
- String suprotni = "";
- boolean jestePalindrom = true;
- for( int i = 0; i < strLen / 2; i++) {
- curr = inputString.charAt(i);
- suprotni = inputString.charAt(strLen - 1 - i); // karakter na simetricnoj poziciji
- //Serial.println(curr);
- //Serial.println(" Trenutni: " + curr);
- //Serial.println("i = " + 1 + " Trenutni: " + curr);
- Serial.println("Suprotni: " + suprotni);
- if(curr != suprotni) {
- jestePalindrom = false;
- }
- }
- if(jestePalindrom) {
- Serial.println("Uneseni string jeste palindrom!");
- } else {
- Serial.println("Uneseni string NIJE palindrom!");
- }
- }
- /*
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement