uas_arduino

Serial Reader/Writer Part 1

Apr 26th, 2013
40
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define MAX_STRING_SIZE 256
  2. #define DIVIDER 0
  3.  
  4. char * myString;
  5. int stringPosition = 0;
  6.  
  7. byte counter = 0;
  8. byte subCounter = 0;
  9.  
  10. void setup(){
  11.   Serial.begin(9600);
  12.  
  13.   myString = (char *)malloc(sizeof(char) * MAX_STRING_SIZE);
  14.   memset(myString, 0xff, MAX_STRING_SIZE);
  15. }
  16.  
  17. void serialEvent(){
  18.   while(Serial.available()){
  19.     myString[stringPosition++] = Serial.read();
  20.   }
  21.  
  22.   if(myString[stringPosition - 1] == '\r' || myString[stringPosition - 1] == '\n'){
  23.     // Uncomment to write the counter that is incremented by loop()
  24.     //Serial.write(counter);
  25.     Serial.write(stringPosition);
  26.     memset(myString, 0xff, MAX_STRING_SIZE);
  27.     stringPosition = 0;
  28.   }
  29. }
  30.  
  31. void loop(){
  32.   if(subCounter++ % DIVIDER == 0){
  33.     counter++;
  34.   }
  35. }
RAW Paste Data