Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. //Frequenzimetro by Xfox
  2.  
  3. #include <LiquidCrystal.h>
  4. const int in = A0;
  5. const int out = 3;
  6. LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
  7.  
  8.  
  9. long tempo1 = 0;
  10. long tempo2 = 0;
  11.  
  12. double T = 0;
  13. double f = 0;
  14.  
  15. long wait = 0;
  16.  
  17. void setup() {
  18.   Serial.begin(19200);
  19.   lcd.begin(16, 2);
  20.   pinMode(in, INPUT);
  21.   pinMode(out, OUTPUT);
  22.   Serial.println("Start");
  23.   splashscreen();
  24.   mostra();
  25. }
  26.  
  27. void loop(){
  28.   if (digitalRead(in) == LOW){
  29.     tempo1 = millis();
  30.     for (digitalRead(in); digitalRead(in) != HIGH; ){
  31.     }
  32.     tempo2 = millis();
  33.   }
  34.  
  35.  
  36.   T = tempo2 - tempo1;
  37.   T = T * 0.001;
  38.   f = 1 / T;
  39.   mostra();
  40.   seriale();
  41. }
  42.  
  43.  
  44. void mostra(){
  45.   if (wait < millis()){
  46.     wait = millis() + 50;
  47.     lcd.clear();
  48.     lcd.setCursor(0, 0);
  49.     lcd.print("f: ");
  50.  
  51.     if (f > 1000){
  52.       f = f / 1000;    
  53.       lcd.print(f);
  54.       lcd.print(" KHz");
  55.     }
  56.     else{
  57.       lcd.print(f);
  58.       lcd.print(" Hz");
  59.     }
  60.     lcd.setCursor(0,1);
  61.  
  62.     if (T < 0.001){
  63.       T = T * 1000;
  64.       lcd.print("T: ");
  65.       lcd.print(T);  
  66.       lcd.print(" ms");
  67.     }
  68.     else{
  69.       lcd.print("T: ");
  70.       lcd.print(T);  
  71.       lcd.print(" s");
  72.     }
  73.   }
  74. }
  75.  
  76. long wait_seriale = 0;
  77. void seriale(){
  78.  
  79.   if (wait_seriale < millis()){
  80.     wait_seriale = millis() + 1000;
  81.     Serial.print("T = ");
  82.     Serial.print(T);
  83.     Serial.print("\t");
  84.     Serial.print("T2 = ");
  85.     Serial.print(tempo2);
  86.     Serial.print("\t");
  87.     Serial.print("T1 = ");
  88.     Serial.print(tempo1);
  89.     Serial.print("\t");
  90.     Serial.print("f = ");
  91.     Serial.print(f);
  92.     Serial.print("\t");
  93.     Serial.println();
  94.   }
  95. }
  96.  
  97. void splashscreen(){
  98.   lcd.clear();
  99.   lcd.print("Xfox & Arduino");
  100.   lcd.setCursor(0, 1);
  101.   lcd.print("Frequency");
  102.   delay(2000);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement