Victor795

Stepper + Sonar

Jul 30th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.88 KB | None | 0 0
  1. #include <TimerOne.h>
  2.  
  3. #define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
  4. #define ECHO_PIN 2 // 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
  5. #define MAX_DISTANCE 40 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  6.  
  7. #define TIMER_US 10000                         // 10mS set timer duration in microseconds
  8. #define TICK_COUNTS 20                          // 2S worth of timer ticks
  9. #define STEPS 400
  10.  
  11. #define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
  12. #define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead
  13.  
  14. #define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
  15.  
  16.  
  17.  
  18. const int ledPin =  13;      // the number of the LED pin
  19.  
  20. unsigned long previousMic = 0;        // will store last time LED was updated
  21. unsigned long currentMic;
  22. unsigned int tmp,tmp2;
  23.  
  24. volatile long tick_count = TICK_COUNTS;         // Counter for 2S
  25. volatile bool in_long_isr = false;              // True if in long interrupt
  26. volatile bool timeout1 = false;
  27.  
  28. int SetDistance = 0;
  29. int ValueDist = 0;
  30. int motorPin1 = A1;
  31. int motorPin2 = A2;
  32. int motorPin3 = A3;
  33. int motorPin4 = A4;
  34. unsigned int uS;
  35. unsigned int n_step;
  36. int trig = TRIGGER_PIN;
  37. int echo = ECHO_PIN;
  38.  
  39.  
  40. unsigned int tempo;
  41.  
  42. volatile int nThrottleIn = NEUTRAL_THROTTLE; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
  43. volatile unsigned long ulStartPeriod = 0; // set in the interrupt
  44. volatile boolean bNewThrottleSignal = false; // set in the interrupt and read in the loop
  45. // we could use nThrottleIn = 0 in loop instead of a separate variable, but using bNewThrottleSignal to indicate we have a new signal
  46. // is clearer for this first example
  47.  
  48.  
  49. void setup() {
  50. Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  51. Serial.println(" Hello " );
  52.  
  53. pinMode(A0,OUTPUT);
  54. pinMode(A5,OUTPUT);
  55. pinMode(A1,OUTPUT);
  56. pinMode(A2,OUTPUT);
  57. pinMode(A3,OUTPUT);
  58. pinMode(A4,OUTPUT);
  59.  
  60.  
  61. pinMode(trig,OUTPUT);
  62. pinMode(echo,INPUT);
  63.  
  64.  
  65. Timer1.initialize(TIMER_US);                  // Initialise timer 1
  66. Timer1.attachInterrupt( timerIsr );           // attach the ISR routine here
  67.  
  68. digitalWrite(A0,HIGH);
  69. digitalWrite(A5,HIGH);
  70.  
  71. attachInterrupt(THROTTLE_SIGNAL_IN,calcInput,CHANGE);
  72. }
  73.  
  74. void loop() {
  75.        
  76.   while ( n_step++<100){
  77.          
  78.           digitalWrite(motorPin1, HIGH);
  79.           digitalWrite(motorPin2, LOW);
  80.           digitalWrite(motorPin3, LOW);
  81.           digitalWrite(motorPin4, LOW);
  82.            
  83.           while (tmp2++<12000);
  84.           tmp2=0;
  85.           digitalWrite(motorPin1, LOW);
  86.           digitalWrite(motorPin2, LOW);
  87.           digitalWrite(motorPin3, HIGH);
  88.           digitalWrite(motorPin4, LOW);
  89.          
  90.          while (tmp2++<12000);
  91.           tmp2=0;
  92.           digitalWrite(motorPin1, LOW);
  93.           digitalWrite(motorPin2, HIGH);
  94.           digitalWrite(motorPin3, LOW);
  95.           digitalWrite(motorPin4, LOW);
  96.          
  97.           while (tmp2++<12000);
  98.           tmp2=0;
  99.           digitalWrite(motorPin1, LOW);
  100.           digitalWrite(motorPin2, LOW);
  101.           digitalWrite(motorPin3, LOW);
  102.           digitalWrite(motorPin4, HIGH);
  103.           while (tmp2++<12000);
  104.           tmp2=0;
  105.        }
  106.  
  107.         n_step = 0;
  108.        
  109.         while ( n_step++<100){
  110.           digitalWrite(motorPin1, LOW);
  111.           digitalWrite(motorPin2, LOW);
  112.           digitalWrite(motorPin3, LOW);
  113.           digitalWrite(motorPin4, HIGH);
  114.           while (tmp2++<12000);
  115.           tmp2=0;
  116.  
  117.           digitalWrite(motorPin1, LOW);
  118.           digitalWrite(motorPin2, HIGH);
  119.           digitalWrite(motorPin3, LOW);
  120.           digitalWrite(motorPin4, LOW);
  121.           while (tmp2++<12000);
  122.           tmp2=0;
  123.           digitalWrite(motorPin1, LOW);
  124.           digitalWrite(motorPin2, LOW);
  125.           digitalWrite(motorPin3, HIGH);
  126.           digitalWrite(motorPin4, LOW);
  127.           while (tmp2++<12000);
  128.           tmp2=0;
  129.           digitalWrite(motorPin1, HIGH);
  130.           digitalWrite(motorPin2, LOW);
  131.           digitalWrite(motorPin3, LOW);
  132.           digitalWrite(motorPin4, LOW);
  133.           while (tmp2++<12000);
  134.  
  135.         }
  136.  
  137.          n_step = 0;
  138.  
  139.  
  140. }
  141.  
  142. // --------------------------
  143. // timerIsr() 10 milli second interrupt ISR()
  144. // Called every time the hardware timer 1 times out.
  145. // --------------------------
  146. void timerIsr()
  147. {
  148. if(bNewThrottleSignal)
  149.  {
  150.    Serial.println(nThrottleIn/58,DEC);   // misura in cm
  151.  
  152.    // set this back to false when we have finished
  153.    // with nThrottleIn, while true, calcInput will not update
  154.    // nThrottleIn
  155.    bNewThrottleSignal = false;
  156.  }
  157.  
  158.                 digitalWrite(trig,HIGH);
  159.                 delayMicroseconds(10);
  160.                 digitalWrite(trig,LOW);
  161. }    
  162.        
  163.  
  164. //Credits -> http://rcarduino.blogspot.it/2012/01/how-to-read-rc-receiver-with.html
  165. void calcInput()
  166. {
  167.   // if the pin is high, its the start of an interrupt
  168.   if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
  169.   {
  170.     // get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
  171.     // easy to understand and works very well
  172.     ulStartPeriod = micros();
  173.   }
  174.   else
  175.   {
  176.     // if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
  177.     // start time ulStartPeriod from the current time returned by micros()
  178.     if(ulStartPeriod && (bNewThrottleSignal == false))
  179.     {
  180.       nThrottleIn = (int)(micros() - ulStartPeriod);
  181.       ulStartPeriod = 0;
  182.  
  183.       // tell loop we have a new signal on the throttle channel
  184.       // we will not update nThrottleIn until loop sets
  185.       // bNewThrottleSignal back to false
  186.       bNewThrottleSignal = true;
  187.     }
  188.   }
  189. }
Add Comment
Please, Sign In to add comment