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

vw tx working

By: a guest on Jun 3rd, 2013  |  syntax: C  |  size: 1.10 KB  |  views: 36  |  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. //Transmitter Code (Uno)
  2. #include <VirtualWire.h>
  3. int red, green, blue;
  4. String RGB;
  5. char cRGB[8];
  6.  
  7. void setup()
  8. {
  9.   Serial.begin(9600);
  10.   int red = 255;
  11.   int blue = 255;
  12.   int green = 255;
  13.   vw_setup(4000);
  14.   vw_set_tx_pin(7);
  15. }
  16.  
  17. void loop()
  18. {
  19.   //protocol expects data in format of 4 bytes
  20.   //(xff) as a marker to ensure proper synchronization always
  21.   //followed by red, green, blue bytes
  22.     if (Serial.available()>=4)
  23.   {
  24.   if(Serial.read() == 0xff)
  25.     {
  26.   red = Serial.read();
  27.   green= Serial.read();
  28.   blue = Serial.read();
  29.     }
  30.   }
  31. //here is my shitty way to convert the rgb into a hex color
  32.   RGB = "F"; //starting with "F" (easier to un-hex for the receiver)
  33. if (red<=16){
  34.   RGB += "0"+String(red,HEX);}
  35.   else if (red>16){RGB+=String(red,HEX);}
  36. if (green<=16){
  37.   RGB += "0"+String(green,HEX);}
  38.   else if (green>16){RGB+=String(green,HEX);}
  39. if (blue<=16){
  40.   RGB += "0"+String(blue,HEX);}
  41.   else if (blue>16){RGB+=String(blue,HEX);}  
  42. // example of end result from the rgb color (10,10,10) = F0A0A0A
  43.  
  44.  
  45.   RGB.toCharArray(cRGB,8);
  46.  
  47.       vw_send((uint8_t *)cRGB, 8);
  48. }