SHARE
TWEET

hfjhdfhfg

a guest May 25th, 2019 63 in 6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. //I am Using a 128 x 64 Graphics LCD for ths project.
  4. //Very Cool and perfect result
  5. //Pictures are attached.
  6.  
  7.  
  8. #include "U8glib.h"
  9.  
  10. U8GLIB_ST7920_128X64_4X u8g(10);        // SPI Com: SCK = en = 13, MOSI = rw = 11, CS = di = 10, HW SPI
  11.  
  12. //calculations
  13. //tire radius ~ 13.5 inches
  14. //circumference = pi*2*r =~85 inches
  15. //max speed of 35mph =~ 616inches/second
  16. //max rps =~7.25
  17.  
  18. #define reed A0//pin connected to read switch
  19.  
  20. //storage variables
  21. int reedVal;
  22. long timer;// time between one full rotation (in ms)
  23. int mph;
  24. float radius = 13.5;// tire radius (in inches)
  25. float circumference;
  26. int kph;
  27.  
  28. int rpm;
  29.  
  30. int maxReedCounter = 10;//min time (in ms) of one rotation (for debouncing)
  31. int reedCounter;
  32.  
  33. int gear_n = 7;
  34.  
  35. const char* gear;
  36.  // A Splash Screen, To be displayed for the first 6 seconds, the time can be adjusted in the loop().
  37.  void draw_splash(void){
  38.     u8g.setFont(u8g_font_helvB12);
  39.     u8g.drawStr(22, 13, "WELCOME");
  40.     u8g.setFont(u8g_font_8x13);
  41.     u8g.drawStr(22, 25, "PLEASE WEAR");
  42.     u8g.drawStr(22, 38, "YOUR HELMET");
  43.     u8g.drawStr(19, 50,"Designed By:");
  44.     u8g.drawStr(9, 63,"Shahid Khattak");
  45.    
  46.  }
  47.  
  48. void draw_main(void) {
  49.  
  50.   u8g.setFont(u8g_font_timR10);
  51.   u8g.drawStr(4, 12,"Speed Km / h");
  52.  
  53.   u8g.drawLine(0,16,84,16);
  54.  
  55.   u8g.setFont(u8g_font_fur42n);
  56.   u8g.setPrintPos(0,62);
  57.   u8g.print(kph);
  58.  
  59.   u8g.setFont(u8g_font_fub11);
  60.   u8g.drawStr(88, 12,"RPM");
  61.  
  62.   u8g.setFont(u8g_font_8x13);
  63.   //u8g.drawStr(86, 29,"01234");
  64.   u8g.setPrintPos(86,29);
  65.   u8g.print(rpm);
  66.  
  67.   u8g.setFont(u8g_font_8x13);
  68.   u8g.drawStr(91, 43,"Gear");
  69.  
  70.   u8g.drawLine(84,0,84,63);
  71.  
  72.   u8g.drawLine(84,32,128,32);
  73.  
  74.    u8g.setFont(u8g_font_helvB14);
  75.   //u8g.drawStr(100, 62,"N");
  76.   u8g.setPrintPos(100,62);
  77.   u8g.print(gear);
  78. }
  79.  
  80. void setup(void) {
  81.   // flip screen, if required
  82.   u8g.setRot180(); // comment out this line if you do not want to flip the screen
  83.   reedCounter = maxReedCounter;
  84.   circumference = 2*3.14*radius;
  85.   pinMode(reed, INPUT);
  86.   pinMode(gear_n, INPUT);
  87.  
  88.   // TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch
  89.   //for more info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
  90.   cli();//stop interrupts
  91.  
  92.   //set timer1 interrupt at 1kHz
  93.   TCCR1A = 0;// set entire TCCR1A register to 0
  94.   TCCR1B = 0;// same for TCCR1B
  95.   TCNT1  = 0;
  96.   // set timer count for 1khz increments
  97.   OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1
  98.   // turn on CTC mode
  99.   TCCR1B |= (1 << WGM12);
  100.   // Set CS11 bit for 8 prescaler
  101.   TCCR1B |= (1 << CS11);  
  102.   // enable timer compare interrupt
  103.   TIMSK1 |= (1 << OCIE1A);
  104.  
  105.   sei();//allow interrupts
  106.   //END TIMER SETUP
  107.  
  108. }
  109.  
  110.  
  111. ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
  112.   reedVal = digitalRead(reed);//get val of A0
  113.   if (reedVal){//if reed switch is closed
  114.     if (reedCounter == 0){//min time between pulses has passed
  115.       mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
  116.       rpm = (float(60000))/(float(timer));
  117.       timer = 0;//reset timer
  118.       reedCounter = maxReedCounter;//reset reedCounter
  119.       kph = mph * 1.609344;
  120.     }
  121.     else{
  122.       if (reedCounter > 0){//don't let reedCounter go negative
  123.         //reedCounter -= 1;//decrement reedCounter
  124.       }
  125.     }
  126.   }
  127.   else{//if reed switch is open
  128.     if (reedCounter > 0){//don't let reedCounter go negative
  129.       reedCounter -= 1;//decrement reedCounter
  130.     }
  131.   }
  132.   if (timer > 1000){
  133.     mph = 0;
  134.   kph = 0;//if no new pulses from reed switch- tire is still, set mph, rpm and kph to 0
  135.   rpm=0;
  136.   }
  137.   else{
  138.     timer += 1;//increment timer
  139.   }
  140. }
  141.  
  142. void loop(void) {
  143.   // picture loop
  144.  
  145.   if(millis()<6000){ // The time in milli seconds, for you want to show the splash screen 6000 = 6 Seconds
  146.   u8g.firstPage();  
  147.   do {
  148.     draw_splash();
  149.   } while( u8g.nextPage() );
  150.   }else{
  151.  
  152.   u8g.firstPage();  
  153.     do {
  154.       draw_main();
  155.     } while( u8g.nextPage() );
  156.  
  157.      // rebuild the picture after some delay
  158.        delay(50);
  159.         }
  160.          if(digitalRead(gear_n) == LOW ){
  161.    
  162.           gear ="N";
  163.    
  164.            }else{
  165.  
  166.              gear =" ";
  167.      
  168.               }
  169.  
  170.  
  171.  
  172. }
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