Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

vw rx working

By: a guest on Jun 3rd, 2013  |  syntax: C  |  size: 1.37 KB  |  views: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Reciever Code (Teensy 2)
  2. #include <VirtualWire.h>
  3. int red, green, blue;
  4.  
  5. int RedPin = 4;
  6. int GreenPin = 9;
  7. int BluePin = 10;
  8. char cRGB[8];
  9. char Cred[2],Cgreen[2],Cblue[2];
  10. void setup()
  11. {
  12.   Serial.begin(9600);
  13.   pinMode(RedPin, OUTPUT);
  14.   pinMode(GreenPin, OUTPUT);
  15.   pinMode(BluePin, OUTPUT);
  16.  
  17.   vw_setup(4000);
  18.   vw_set_rx_pin(7);
  19.   vw_rx_start();
  20. }
  21.  
  22. void loop(){
  23.     uint8_t buf[VW_MAX_MESSAGE_LEN];
  24.     uint8_t buflen = VW_MAX_MESSAGE_LEN;
  25.    
  26.     // Non-blocking
  27.     if (vw_get_message(buf, &buflen))
  28.     {
  29.     int i;
  30.         for (i = 0; i < buflen; i++)
  31.     {            
  32.           cRGB[i] = char(buf[i]); //fill cRGB with msg that was received
  33.     }
  34.         cRGB[buflen] = '\0'; //something for safety
  35.        
  36. // Convert cRGB Char array to smaller char arrays to eventually turn into numbers. (its awful, i know)
  37.     Cred[0] = cRGB[1];
  38.     Cred[1] = cRGB[2];
  39.     red = int(strtol(Cred, NULL, 16));
  40.     analogWrite (RedPin, red);
  41.    
  42.     Cgreen[0] = cRGB[3];
  43.     Cgreen[1] = cRGB[4];
  44.     green = int(strtol(Cgreen, NULL, 16));
  45.     analogWrite (GreenPin, green);
  46.    
  47.     Cblue[0] = cRGB[5];
  48.     Cblue[1] = cRGB[6];
  49.     blue = int(strtol(Cblue, NULL, 16));
  50.     analogWrite (BluePin, blue);
  51.    
  52.     Serial.println(red); //for monitoring
  53.     Serial.println(green);
  54.     Serial.println(blue);
  55.     Serial.println("------------");
  56.  
  57.  
  58.     }
  59. }
clone this paste RAW Paste Data