Advertisement
Synthetech

RC using pulseIn

Nov 30th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.72 KB | None | 0 0
  1. #include <SD.h>
  2.  
  3. #include "FastLED.h"
  4.  
  5. // How many leds in your strip?
  6. #define NUM_LEDS 60
  7.  
  8. // For led chips like Neopixels, which have a data line, ground, and power, you just
  9. // need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
  10. // ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
  11. #define DATA_PIN 6
  12. #define CLOCK_PIN 13
  13. int rcPin = 2; // PWM signal arduino pin
  14. int Speed = 0;    // Receiver channel 1 pwm value
  15. // Define the array of leds
  16. CRGB leds[NUM_LEDS];
  17.  
  18. void setup() {
  19.     FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS);
  20.  pinMode(rcPin, INPUT);
  21.   Serial.begin(9600);
  22. }
  23.  
  24.  
  25.  
  26.  
  27. void loop() {
  28. // First slide the led from outer ends towards the middle
  29. // Move the dots to the 7th LED inwards from each end
  30. for(int i = 0; i < (NUM_LEDS / 8) - 1; i++) {
  31. // Set the i'th led to red
  32.  
  33.  
  34. //Start with 1st LED and move inwards
  35. leds[i] = CRGB::DarkViolet ;
  36. //while also having opposite end LED move inwards
  37. leds[NUM_LEDS - i -1] = CRGB::DarkViolet ;
  38. // Show the leds
  39. FastLED.show();
  40.  
  41.  
  42. //erase drawn dots
  43. // now that we've shown the leds, reset the i'th led to black
  44. leds[i] = CRGB::Black;
  45. leds[NUM_LEDS - i -1] = CRGB::Black;
  46. // Wait a little bit before we loop around and move the dots inwards by one
  47. delay(Speed);
  48.  
  49. //when we get to the 7th LED inwards from each end
  50. //check the pin hooked up to the rc receiver to check
  51. //PW size on the low side
  52. //the PW LOW is longest at lowest throttle position
  53. //to give a longer delay and slow the pattern down
  54. //PW is shortest at the highest position-full throttle
  55. //to give a shorter delay and speed up the pattern
  56. //shortest LOW is approx 19950
  57. //longest LOW is approx 20800
  58. //I alter the data to give a delay time range of 2-36
  59. //2 at full throttle and 36 at lowest throttle position
  60. }
  61.   Speed = pulseIn(rcPin, LOW);
  62.  Speed= (Speed -19900)/25;
  63.   if (Speed <=0) {
  64.    Speed=0;
  65.   }
  66.   //show me the value of Speed
  67.   Serial.println(Speed);
  68.  
  69.   //beginning at the 8th LED inwards on each end
  70.   //we begin where we left off from the prev for loop
  71.   //and stop at the 14th LED on each end.
  72.   for(int i= 8;i < (NUM_LEDS / 4) - 1; i++) {
  73. // Set the i'th led to red
  74.  
  75. leds[i] = CRGB::DarkViolet ;
  76. leds[NUM_LEDS - i -1] = CRGB::DarkViolet ;
  77. // Show the leds
  78. FastLED.show();
  79.  
  80. // now that we've shown the leds, reset the i'th led to black
  81. leds[i] = CRGB::Black;
  82. leds[NUM_LEDS - i -1] = CRGB::Black;
  83. // Wait a little bit before we loop around and do it again
  84. delay(Speed);
  85.  
  86. }
  87.  
  88.     Speed = pulseIn(rcPin, LOW);
  89.  Speed= (Speed -19900)/25;
  90.   if (Speed <=0) {
  91.    Speed=0;
  92.   }
  93.    Serial.println(Speed);
  94.    
  95.   //now we've reached midpoint for each end
  96.     for(int i= 15;i < (NUM_LEDS / 3) +2; i++) {
  97. // Set the i'th led to red
  98.  
  99. leds[i] = CRGB::DarkViolet ;
  100. leds[NUM_LEDS - i -1] = CRGB::DarkViolet ;
  101. // Show the leds
  102. FastLED.show();
  103.  
  104. // now that we've shown the leds, reset the i'th led to black
  105. leds[i] = CRGB::Black;
  106. leds[NUM_LEDS - i -1] = CRGB::Black;
  107. // Wait a little bit before we loop around and do it again
  108. delay(Speed);
  109.  
  110. }
  111.  
  112.      Speed = pulseIn(rcPin, LOW);
  113.  Speed= (Speed -19900)/25;
  114.   if (Speed <=0) {
  115.    Speed=0;
  116.   }  
  117.    Serial.println(Speed);
  118.  
  119.       for(int i= 22;i < (NUM_LEDS / 2) -1; i++) {
  120. // Set the i'th led to red
  121.  
  122. leds[i] = CRGB::DarkViolet ;
  123. leds[NUM_LEDS - i -1] = CRGB::DarkViolet ;
  124. // Show the leds
  125. FastLED.show();
  126.  
  127. // now that we've shown the leds, reset the i'th led to black
  128. leds[i] = CRGB::Black;
  129. leds[NUM_LEDS - i -1] = CRGB::Black;
  130. // Wait a little bit before we loop around and do it again
  131. delay(Speed);
  132.  
  133. }
  134.  
  135.  
  136.     Speed = pulseIn(rcPin, LOW);
  137.  Speed= (Speed -19900)/25;
  138.   if (Speed <=0) {
  139.    Speed=0;
  140.   }  
  141.    Serial.println(Speed);
  142.  
  143.  
  144.  
  145. // Now go in the other direction.  
  146. //we reached the middle, now let's go back towards the ends
  147. for(int i = (NUM_LEDS / 2); i > 22; i--) {
  148.  
  149. // Set the i'th led to red
  150. leds[i] = CRGB::Red;
  151. leds[NUM_LEDS - i -1] = CRGB::Red;
  152. // Show the leds
  153. FastLED.show();
  154.  
  155. // now that we've shown the leds, reset the i'th led to black
  156. leds[i] = CRGB::Black;
  157. leds[NUM_LEDS - i -1] = CRGB::Black;
  158. // Wait a little bit before we loop around and do it again
  159. delay(Speed);
  160. }
  161.  
  162.  
  163.  
  164.  
  165.  
  166. for(int i = 22; i > 15; i--) {
  167.  
  168. // Set the i'th led to red
  169. leds[i] = CRGB::Red;
  170. leds[NUM_LEDS - i -1] = CRGB::Red;
  171. // Show the leds
  172. FastLED.show();
  173.  
  174. // now that we've shown the leds, reset the i'th led to black
  175. leds[i] = CRGB::Black;
  176. leds[NUM_LEDS - i -1] = CRGB::Black;
  177. // Wait a little bit before we loop around and do it again
  178. delay(Speed);
  179. }
  180.   Speed = pulseIn(rcPin, LOW);
  181.  Speed= (Speed -19900)/25;
  182.   if (Speed <=0) {
  183.    Speed=0;
  184.   }
  185.  
  186.    Serial.println(Speed);
  187.    
  188.    
  189.  
  190.   for(int i = 15; i > 8; i--) {
  191.  
  192. // Set the i'th led to red
  193. leds[i] = CRGB::Red;
  194. leds[NUM_LEDS - i -1] = CRGB::Red;
  195. // Show the leds
  196. FastLED.show();
  197.  
  198. // now that we've shown the leds, reset the i'th led to black
  199. leds[i] = CRGB::Black;
  200. leds[NUM_LEDS - i -1] = CRGB::Black;
  201. // Wait a little bit before we loop around and do it again
  202. delay(Speed);
  203. }
  204.   Speed = pulseIn(rcPin, LOW);
  205.  Speed= (Speed -19900)/25;
  206.   if (Speed <=0) {
  207.    Speed=0;
  208.   }  
  209.  
  210.    Serial.println(Speed);
  211.    
  212.    
  213.   for(int i = 8; i > 0; i--) {
  214.  
  215. // Set the i'th led to red
  216. leds[i] = CRGB::Red;
  217. leds[NUM_LEDS - i -1] = CRGB::Red;
  218. // Show the leds
  219. FastLED.show();
  220.  
  221. // now that we've shown the leds, reset the i'th led to black
  222. leds[i] = CRGB::Black;
  223. leds[NUM_LEDS - i -1] = CRGB::Black;
  224. // Wait a little bit before we loop around and do it again
  225. delay(Speed);
  226. }
  227.    Speed = pulseIn(rcPin, LOW);
  228.  Speed= (Speed -19900)/25;
  229.   if (Speed <=0) {
  230.    Speed=0;
  231.   }  
  232.    Serial.println(Speed);
  233.  
  234. }
  235. //go back to the first LED's on each end and start over
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement