Advertisement
Guest User

Untitled

a guest
Nov 24th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.78 KB | None | 0 0
  1. // configure the imp (best practice)
  2. imp.configure("Lego IR Remote", [], []);
  3.  
  4. // CREATE a global variabled called led,
  5. // AND assign pin9 TO it
  6. led <- hardware.pin9;
  7.  
  8. toggle <- [0,0,0,0];
  9. // CREATE a global variable TO store CURRENT
  10. // state OF the LED
  11. state <- 0;
  12. s2ms <- 0.000001;
  13.  
  14. //The used Global variabels
  15. PWM_FWD7 <- 0x7;
  16. PWM_FLT <- 0x0;
  17.  
  18. //channel
  19. CH1 <- 0x0;
  20.  
  21. //output
  22. RED <- 0x0;
  23.  
  24.  
  25. // configure led TO be a digital output
  26. led.configure(DIGITAL_OUT);
  27.  
  28. // CREATE a global variable TO store CURRENT
  29. // state OF the LED
  30. state <- 0;
  31.  
  32. FUNCTION blink() {
  33. // invert the VALUE OF state:
  34. // WHEN state = 1, 1-1 = 0
  35. // WHEN state = 0, 1-0 = 1
  36. state = 1-state;
  37.  
  38. // sWITCHING IR COMMANDS
  39.  
  40.     server.log("Starting motor");
  41.     SingleOutput(PWM_FWD7,RED,CH1);
  42.    
  43.  
  44.     // schedule imp TO wakeup IN .5 seconds AND do it again.
  45.     imp.sleep(5);
  46.    
  47.     server.log("Stopping motor");
  48.     SingleOutput(PWM_FLT, RED,CH1);
  49.    
  50.    
  51.    
  52.     // schedule imp TO wakeup IN .5 seconds AND do it again.
  53.     imp.wakeup(5, blink);
  54.  
  55. }
  56.  
  57. FUNCTION SingleOutput(pwm,output,channel) {
  58.  
  59.   server.log("Sending IR Commands..")
  60.  
  61.   SINGLE_OUTPUT <- 0x4;
  62.  
  63.   nib1 <- toggle[channel] | channel;
  64.   nib2 <- SINGLE_OUTPUT | output;
  65.   nib3 <- pwm;
  66.   nib4 <- 0xf ^ nib1 ^ nib2 ^ nib3;
  67.  
  68.   FOR (LOCAL i=0; i<6; i++)
  69.   {
  70.     message_pause(channel, i);
  71.     pf_send(nib1 << 4 | nib2, nib3 << 4 | nib4);  
  72.    
  73.   }
  74.  
  75.   IF(toggle[channel] == 0)
  76.     toggle[channel] = 8;
  77.   ELSE
  78.     toggle[channel] = 0;
  79.    
  80.   server.log("ready")
  81.  
  82. }
  83.  
  84.  
  85. FUNCTION start_pause()
  86. {
  87.   imp.sleep(1014*s2ms);
  88. }
  89.  
  90. FUNCTION high_pause()
  91. {
  92.   imp.sleep(546*s2ms);
  93. }
  94.  
  95. FUNCTION low_pause()
  96. {
  97.   imp.sleep(260*s2ms);
  98. }
  99.  
  100. FUNCTION tx_pause()
  101. {
  102.   imp.sleep(156*s2ms);
  103. }
  104.  
  105. FUNCTION message_pause(channel, COUNT)
  106. {
  107.    a <- 0;
  108.  
  109.   IF(COUNT == 0)
  110.     a = 4 - channel + 1;
  111.   ELSE IF(COUNT == 1 || COUNT == 2)
  112.     a = 5;
  113.   ELSE IF(COUNT == 3 || COUNT == 4)
  114.     a = 5 + (channel + 1) * 2;
  115.  
  116.   imp.sleep(a * 77 * s2ms);
  117.  
  118. }
  119.  
  120. FUNCTION pf_send(code1, code2)
  121. {
  122.   x <- 128;
  123.  
  124.   start_stop_bit();
  125.  
  126.   while (x)
  127.   {
  128.     oscillationWrite(156 * s2ms);
  129.  
  130.     IF (code1 & x) //high bit
  131.       high_pause();
  132.     ELSE //low bit
  133.     low_pause();
  134.  
  135.     x = x >> 1;  //NEXT bit
  136.   }
  137.  
  138.   x = 128;
  139.   while (x)
  140.   {
  141.     oscillationWrite(156*s2ms);
  142.  
  143.     IF (code2 & x) // high bit
  144.       high_pause();
  145.     ELSE //low bit
  146.     low_pause();
  147.  
  148.     x = x >> 1;  //NEXT bit
  149.   }
  150.  
  151.   start_stop_bit();
  152.  
  153. }
  154.  
  155. FUNCTION start_stop_bit()
  156. {
  157.   oscillationWrite(156*s2ms);  
  158.   start_pause();
  159. }
  160.  
  161. FUNCTION oscillationWrite(TIME) {
  162.   FOR( LOCAL i=0; i <= TIME/26; i++) {
  163.    
  164.     led.WRITE(1)
  165.     imp.sleep(13*s2ms);
  166.     led.WRITE(0)
  167.     imp.sleep(13*s2ms);
  168.   }
  169. }
  170.  
  171.  
  172. // START the loop
  173. blink();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement