Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. //Programa: Medidor de corrente com sensor ACS712
  2. //Autor: Arduino e Cia
  3.  
  4. #include <Wire.h>
  5. #include <U8glib.h>
  6.  
  7. //Definicoes do display Oled
  8. U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
  9.  
  10. //int valor = 0;
  11. String str;
  12. int tamanho;
  13.  
  14. const int analogIn = A0;
  15. int mVperAmp = 66;
  16. int RawValue = 0;
  17. int ACSoffset = 2500;
  18. double Voltage = 0;
  19. double Amps = 0;
  20.  
  21. void draw()
  22. {
  23. //Comandos graficos para o display devem ser colocados aqui
  24. u8g.drawRFrame(0, 16, 128, 48, 4);
  25. u8g.drawRFrame(0, 0, 128, 16, 4);
  26. u8g.setFont(u8g_font_8x13B);
  27. u8g.setColorIndex(0);
  28. u8g.setColorIndex(1);
  29. u8g.drawStr( 20, 13, "Corrente (A)");
  30. u8g.setFont(u8g_font_fur25);
  31. str = String(Amps);
  32. tamanho = str.length();
  33. u8g.setPrintPos(64 - (tamanho * 10), 53);
  34. u8g.print(Amps,3);
  35. }
  36.  
  37. void setup(void)
  38. {
  39. Serial.begin(9600);
  40. Serial.println("Sensor de Corrente ACS712"); Serial.println("");
  41. Serial.println("");
  42. if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
  43. u8g.setColorIndex(255); // white
  44. }
  45. else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
  46. u8g.setColorIndex(3); // max intensity
  47. }
  48. else if ( u8g.getMode() == U8G_MODE_BW ) {
  49. u8g.setColorIndex(1); // pixel on
  50. }
  51. else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
  52. u8g.setHiColorByRGB(255, 255, 255);
  53. }
  54. }
  55.  
  56. void loop(void)
  57. {
  58. Calcula_corrente();
  59. //Chama a rotina de desenho na tela
  60. u8g.firstPage();
  61. do
  62. {
  63. draw();
  64. }
  65. while ( u8g.nextPage() );
  66. delay(150);
  67. }
  68.  
  69. void Calcula_corrente()
  70. {
  71. RawValue = analogRead(analogIn);
  72. Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
  73. Amps = ((Voltage - ACSoffset) / mVperAmp);
  74. delay(2000);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement