Advertisement
RuiViana

Untitled

Jun 10th, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. // Código original do Rodrigo
  2. // http://labdegaragem.com/forum/topics/frequencimetro-e-voltimetro
  3.  
  4. #include <LiquidCrystal_I2C.h>
  5. #include <Wire.h>
  6. LiquidCrystal_I2C lcd(0x20,20,4);
  7. //Frequencia
  8. long tempo, freq;
  9. int pulsos;
  10. boolean pulso;
  11. //Tensão
  12. #define resistance_R500 1000000
  13. #define resistance_V2 10000
  14. #define caliberation_V2 1.05
  15. #define range500_mul (resistance_R500 / resistance_V2) * caliberation_V2
  16. #define resistance_R2 1000
  17. int adc_value = 0;
  18. int voltage_peak_value = 0;
  19. float voltage_average_value = 0;
  20. float dc_voltage_V0 = 0;
  21. int ac_voltage_V0 = 0;
  22. unsigned long sample_count = 0;
  23. void setup()
  24. {
  25. pulso=HIGH;
  26. pinMode(2,INPUT);
  27. Serial.begin(9600);
  28. }
  29. void loop()
  30. {
  31. tempo = millis();
  32. Serial.println(tempo);
  33. if(digitalRead(2)==HIGH)
  34. {
  35. if(pulso==HIGH)
  36. {
  37. pulsos = pulsos + 1;
  38. }
  39. pulso=LOW;
  40. }
  41. else
  42. {
  43. pulso=HIGH;
  44. }
  45. if(tempo%1000==0)
  46. freq = (pulsos/2.0);
  47.  
  48. else;
  49. delayMicroseconds(10);
  50.  
  51. voltage_peak_value = 0;
  52. for(sample_count = 0; sample_count < 5000; sample_count ++)
  53. {
  54. adc_value = analogRead(A1);
  55. if(voltage_peak_value < adc_value)
  56. voltage_peak_value = adc_value;
  57. else;
  58. delayMicroseconds(10);
  59. }
  60. dc_voltage_V0 = voltage_peak_value * 0.00488;
  61. ac_voltage_V0 = (dc_voltage_V0 / 1.414) * range500_mul;
  62.  
  63. Serial.print("FREQUENCIMETRO: ");
  64. Serial.print(freq);
  65. Serial.print("Hz ");
  66. Serial.print("VOLTIMETRO: ");
  67. Serial.println(ac_voltage_V0);
  68. pulsos=0;
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement