Advertisement
dioxik

Untitled

Mar 4th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. //arduino bike speedometer w serial.print()
  2. //by Amanda Ghassaei 2012
  3. //http://www.instructables.com/id/Arduino-Bike-Speedometer/
  4.  
  5. /*
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11. */
  12.  
  13. //calculations
  14. //tire radius ~ 13.5 inches
  15. //circumference = pi*2*r =~85 inches
  16. //max speed of 35mph =~ 616inches/second
  17. //max rps =~7.25
  18.  
  19. #define reed A0//pin connected to read switch
  20.  
  21. //storage variables
  22. int reedVal;
  23. long timer;// time between one full rotation (in ms)
  24. float mph;
  25. float radius = 13.5;// tire radius (in inches)
  26. float circumference;
  27.  
  28. int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
  29. int reedCounter;
  30.  
  31.  
  32. void setup(){
  33.  
  34.   reedCounter = maxReedCounter;
  35.   circumference = 2*3.14*radius;
  36.   pinMode(reed, INPUT);
  37.  
  38.   // TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch
  39.   //for more info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
  40.   cli();//stop interrupts
  41.  
  42.   //set timer1 interrupt at 1kHz
  43.   TCCR1A = 0;// set entire TCCR1A register to 0
  44.   TCCR1B = 0;// same for TCCR1B
  45.   TCNT1  = 0;
  46.   // set timer count for 1khz increments
  47.   OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1
  48.   // turn on CTC mode
  49.   TCCR1B |= (1 << WGM12);
  50.   // Set CS11 bit for 8 prescaler
  51.   TCCR1B |= (1 << CS11);  
  52.   // enable timer compare interrupt
  53.   TIMSK1 |= (1 << OCIE1A);
  54.  
  55.   sei();//allow interrupts
  56.   //END TIMER SETUP
  57.  
  58.   Serial.begin(9600);
  59. }
  60.  
  61.  
  62. ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
  63.   reedVal = digitalRead(reed);//get val of A0
  64.   if (reedVal){//if reed switch is closed
  65.     if (reedCounter == 0){//min time between pulses has passed
  66.       mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
  67.       timer = 0;//reset timer
  68.       reedCounter = maxReedCounter;//reset reedCounter
  69.     }
  70.     else{
  71.       if (reedCounter > 0){//don't let reedCounter go negative
  72.         reedCounter -= 1;//decrement reedCounter
  73.       }
  74.     }
  75.   }
  76.   else{//if reed switch is open
  77.     if (reedCounter > 0){//don't let reedCounter go negative
  78.       reedCounter -= 1;//decrement reedCounter
  79.     }
  80.   }
  81.   if (timer > 2000){
  82.     mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
  83.   }
  84.   else{
  85.     timer += 1;//increment timer
  86.   }
  87. }
  88.  
  89. void displayMPH(){
  90.   Serial.println(mph);
  91. }
  92.  
  93. void loop(){
  94.   //print mph once a second
  95.   displayMPH();
  96.   delay(1000);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement