Advertisement
snakerdlk

TEENSY-XRF via UART

Apr 11th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #define LED_PIN 11
  2. #define BAUD 9600
  3.  
  4. HardwareSerial Uart = HardwareSerial();
  5.  
  6. void setup()
  7. {
  8.     pinMode(LED_PIN, OUTPUT);
  9.  
  10.     digitalWrite(LED_PIN, HIGH);
  11.     delay(1000);
  12.     pinMode(LED_PIN, LOW);
  13.  
  14.     Serial.begin(BAUD); // USB, communication to PC or Mac
  15.     Serial.println("SETUP teensy");
  16.  
  17.     Uart.begin(BAUD);   // UART, communication to Dorkboard
  18. }
  19.  
  20. char beacon[]="abcdef";
  21. char answer[]="fedcba";
  22.  
  23. unsigned i = 0;
  24. long led_on_time=0;
  25. char input[]="000000";
  26.  
  27. void loop()
  28. {
  29.     unsigned char c;
  30.  
  31.     if (Uart.available()) {
  32.         digitalWrite(LED_PIN, HIGH);
  33.         led_on_time = millis()+200;
  34.  
  35.         c = Uart.read();
  36.         if (c == beacon[0]){
  37.             input[0] = c;
  38.             i=1;
  39.         }else if(i > 0){
  40.             input[i] = c;
  41.             i++;
  42.         }
  43.         if ( i >= strlen(beacon)){
  44.             if (strcmp(input, beacon) == 0){
  45.                 digitalWrite(LED_PIN, HIGH);
  46.                 led_on_time = millis()+500;
  47.  
  48.                 Uart.write(answer);
  49.                 Serial.println("Got beacon!");
  50.             }else{
  51.                 strcpy("000000",input);
  52.             }
  53.             i=0;
  54.         }
  55.     }
  56.     if (millis() > led_on_time) {
  57.         digitalWrite(LED_PIN, LOW);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement