Advertisement
ajcorrea

Untitled

Oct 26th, 2011
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. /*
  2.  * Monitor de Voltagem
  3.  *
  4.  * Monitora a voltagem de um sistema, utilizando divisão de voltagem.
  5.  * Voltagem máxima: 20v
  6.  * Utilizar Resistor 3K entre V+ da bateria e a entrada analógica.
  7.  * Utilizar Resistor 1K no GND e V- da bateria, interligando na porta analogica
  8.  *
  9.  * Autor:
  10.  *           Alexandre Jeronimo Correa
  11.  *           ajcorrea@gmail.com
  12.  *           26/10/2011
  13.  *
  14.  * Changelog:
  15.  * 26/10/2011 - ajcorrea - Versao inicial
  16.  *                         Esta versao suporta apenas comunicacao serial.
  17.  * 26/10/2011 - ajcorrea - Versao 0.2
  18.  *                         Suporte a Ethernet Shield. O suporte inicial funciona
  19.  *                         da seguinte maneira, o client conecta no ip e porta
  20.  *                         do shield, o servidor envia a voltagem e termina a conexao.
  21.  */
  22.  
  23. #define ETHERNET 1
  24.  
  25. #ifdef ETHERNET
  26.  
  27. //Includes - Ethernet.h e SPI.h necessarios para Ethernet
  28. #include <Ethernet.h>
  29. #include <SPI.h>
  30.  
  31. #endif
  32.  
  33. //variaveis
  34. const int referenceVolts = 5; //valor de referencia, maximo aceito pela ADC
  35. const int maxVoltage = 20; //valor maximo a ser medido
  36. const int R1 = 3000; // valor do resistor1, 3000 ohms (3k)
  37. const int R2 = 1000; // valor do resistor2, 1000 ohms (1k)
  38. const int resistorFactor =  255 / (R2/(R1 + R2)); //Fator de resistencia
  39. const int batteryPin = 0; // Porta analogica que recebe o positivo da bateria (V+)
  40.  
  41. #ifdef ETHERNET
  42. //variaveis sistema ethernet
  43. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Mac address do shield
  44. byte ip[] = { 192, 168, 2, 177 }; //Ip address do shield
  45. byte gateway[] = { 192, 168, 2, 2 }; //gateway da rede
  46. byte subnet[] = { 255, 255, 255, 0 }; //mascara da rede
  47. const int Porta = 23; //Porta TCP de Acesso
  48.  
  49. //Inicializacao do servidor
  50. Server server(Porta);
  51.  
  52. #endif
  53.  
  54. void setup()
  55. {
  56.   Serial.begin(9600); //Inicia comunicacao serial.
  57.   #ifdef ETHERNET
  58.   Ethernet.begin(mac,ip,gateway,subnet);
  59.   server.begin();
  60.   delay(1000);
  61.   #endif
  62. }
  63.  
  64. float printVolts(int Pin) {
  65.   int value = analogRead(Pin);
  66.   float volts = (value / resistorFactor) * referenceVolts;
  67.   volts = (((volts / 1024) * -1) * (maxVoltage / referenceVolts));
  68.   return volts;
  69. }
  70.  
  71. void loop()
  72. {
  73.   float amostragem = 0.00;
  74.   amostragem = printVolts(batteryPin);
  75.   Serial.println(amostragem);
  76.  
  77.   #ifdef ETHERNET
  78.   Client client = server.available();
  79.   if (client) {
  80.     client.println(amostragem);
  81.   }
  82.   #endif
  83.   delay(500); //Pausa de 500ms
  84.  
  85.   #ifdef ETHERNET
  86.   if (client) {
  87.     client.stop();
  88.   }
  89.   #endif
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement