SHARE
TWEET

Untitled

a guest Aug 26th, 2015 557 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <TimerOne.h>
  2.  
  3. int one = A1;
  4. int two = A2;
  5. int three = A3;
  6. int en = A5;
  7.  
  8. //int in = 2;
  9.  
  10. void setup() {
  11.   // put your setup code here, to run once:
  12.  
  13.   //1000000 is 1.0 Second
  14.     //2000 = 2ms
  15.     //2000
  16.    Timer1.initialize(100); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  17.    Timer1.attachInterrupt( timerIsr ); // attach the service routine here
  18.    
  19.    //pinMode(in, INPUT_PULLUP);
  20.    attachInterrupt(0, isr, FALLING);
  21.  
  22.    pinMode(one, OUTPUT);
  23.    pinMode(two, OUTPUT);
  24.    pinMode(three, OUTPUT);
  25.    pinMode(en, OUTPUT);
  26.    pinMode(13, OUTPUT);
  27.    
  28.    
  29.    Serial.begin(9600);
  30. }
  31.  
  32. bool seq[][3] = {
  33.       {true, true, false },
  34.       {true, false, false },
  35.       {true, false, true },
  36.       {false, false, true },
  37.       {false, true, true },
  38.       {false, true, false },      
  39.   };
  40.  
  41.  
  42. volatile long msold = 0;
  43. volatile long msnew = 0;
  44.  
  45. volatile int mpr1 = 0;
  46. volatile int mpr2 = 0;
  47.  
  48. volatile long tick = 0;
  49. volatile int freq = 250; //frequency at which every freq'nth time the ISR calls, we do next step
  50. volatile int freqmin = 25; //the fastest theoretical speed is determined by this
  51.  
  52.  
  53. void isr()   //magnet speed sensor
  54. {
  55.   msold = msnew; //set "old new" ms to oldms
  56.   msnew = micros();
  57.  
  58.   mpr2 = mpr1;
  59.   mpr1 = mpr();
  60.  
  61.   if(mpr1 < mpr2)
  62.   {
  63.       if(freq > 100)  
  64.           freq -= 10;
  65.       else if(freq > 15)
  66.        if(tick % 3 == 0)   freq -= 1;  //In order to get more speed I had to slow down the acceleration, hence the % 3
  67.   }
  68.  
  69.   digitalWrite(13, !digitalRead(13));
  70. }
  71.  
  72. long mpr() //return Millisecond Per Revolution
  73. {
  74.  return msnew - msold;
  75. }
  76.  
  77. void timerIsr()
  78. {
  79.   tick++;
  80.   if(tick % freq == 0)
  81.       nextStep();  
  82. }
  83.  
  84. volatile long steps = 0;
  85.  
  86. volatile byte pointer = 0;
  87.  
  88. void nextStep()
  89. {
  90.   steps++;
  91.    if(pointer > 5)
  92.     {
  93.         pointer = 0; //reset if we reached last step. happens 4 times a revolution;
  94.     }
  95.  
  96.      digitalWrite(en, LOW); //disable ENABLE pin
  97.      digitalWrite(one, seq[pointer][0]);
  98.      digitalWrite(two, seq[pointer][1]);
  99.      digitalWrite(three, seq[pointer][2]);
  100.      digitalWrite(en, HIGH); //enable ENABLE pin
  101.      pointer++;
  102. }
  103.  
  104.  
  105. void loop() {
  106.   // put your main code here, to run repeatedly:
  107.  
  108.   delay(500); Serial.println(mpr());
  109.  
  110.  
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top