Advertisement
Synthetech

RC using attachInterrupt

Nov 30th, 2014
213
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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(prev code only)
  14. int Speed = 0;    // Receiver channel 1 pwm value
  15. // Define the array of leds
  16. CRGB leds[NUM_LEDS];
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. //pin 2, INT0, is receiving PWM input
  24. #define CHANNEL_1_PIN 2
  25.  
  26. //micros when the pin goes LOW
  27. volatile unsigned long timer_start;
  28.  
  29. //difference between timer_start and micros() is the length of time that the pin
  30. //was HIGH - the PWM pulse length.
  31. volatile int pulse_time;
  32.  
  33. //this is the time that the last interrupt occurred.
  34. //you can use this to determine if your receiver has a signal or not.
  35. volatile int last_interrupt_time;
  36.  
  37. //calcSignal is the interrupt handler
  38. //that will read the PW LOW length/time on pin 2
  39. void calcSignal()
  40. {
  41. //record the interrupt time so that we can tell if the receiver has a signal from the transmitter
  42.  last_interrupt_time = micros();
  43.  
  44. //if the pin has gone LOW, record the microseconds since the Arduino started up
  45. //reading INT0 on Pin 2
  46.  if(digitalRead(2) == LOW)
  47.     {
  48.         timer_start = micros();
  49.     }
  50. //otherwise, the pin has gone HIGH
  51.     else
  52.     {
  53.         //only worry about this if the timer has actually started
  54.         if(timer_start > 0)
  55.         {
  56.             //record the pulse time
  57.             pulse_time = ((volatile int)micros() - timer_start);
  58.             //restart the timer
  59.             timer_start = 0;
  60.         }
  61.     }
  62. }
  63.  
  64. //this is all normal arduino stuff
  65. void setup()
  66. {
  67.     FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS);
  68. //REMOVED pinMode(rcPin, INPUT);
  69.  
  70.     timer_start = 0;
  71. //here's the interrupt, obviously
  72. //INT0 is on Pin 2.
  73.     attachInterrupt(0, calcSignal, CHANGE);
  74.     Serial.begin(115200);
  75. }
  76.  
  77. void loop()
  78. {
  79.   // First slide the led in one direction
  80. for(int i = 0; i < (NUM_LEDS / 2) - 1; i++) {
  81. // Set the i'th led to red
  82.  
  83. leds[i] = CRGB::DarkViolet ;
  84. leds[NUM_LEDS - i -1] = CRGB::DarkViolet ;
  85. // Show the leds
  86. FastLED.show();
  87.  
  88. /*when PW raw time is viewed in serial monitor I get values average 20100
  89. and 20900. below is formula to convert the numbers to a range of 0-100.
  90. 100 being low throttle and 0 for high/full throttle.
  91. */
  92. pulse_time= pulse_time -20100;
  93.  
  94. if (pulse_time >800) {
  95.    pulse_time=800;}
  96.    pulse_time= pulse_time/8;
  97.    if (pulse_time <0) {
  98.    pulse_time=0;}
  99. //print the value so I can monitor it
  100. Serial.println(pulse_time);
  101. //I get wildly jumping values in the monitor if I do not put this delay in
  102. delay(25);
  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 time of 25 gives a medium slow pattern movement
  109. //if I try to put the 'pulse_time' var in 25's place, it will not work
  110. //and the serial monitor will lock up with only a few zero's showing
  111. //the LED's will not move either. I think the UNO locks up :(
  112.  delay(25);
  113. }
  114.  
  115.  
  116. // Now go in the other direction.  
  117. for(int i = (NUM_LEDS / 2); i > 0; i--) {
  118.  
  119. // Set the i'th led to red
  120. leds[i] = CRGB::Red;
  121. leds[NUM_LEDS - i -1] = CRGB::Red;
  122. // Show the leds
  123. FastLED.show();
  124. pulse_time= pulse_time -20100;
  125.  
  126. if (pulse_time >800) {
  127.    pulse_time=800;}
  128.     pulse_time= pulse_time/8;
  129.    if (pulse_time <0) {
  130.    pulse_time=0;}
  131. Serial.println(pulse_time);
  132. delay(25);
  133.  
  134. // now that we've shown the leds, reset the i'th led to black
  135. leds[i] = CRGB::Black;
  136. leds[NUM_LEDS - i -1] = CRGB::Black;
  137. // Wait a little bit before we loop around and do it again
  138.  delay(25);
  139. }
  140.  
  141. }
Advertisement
RAW Paste Data Copied
Advertisement