Vídeo:
código-fonte:
/************************************************************************************************************ **********************************display de 7 segmentos***************************************************** ************************************************************************************************************/ const int d7seg_pin_segments[] = {6,7,8,9,10,11,12,13}; //pinos para os segmentos: --> a b c d e f g . const int d7seg_pin_enable[] = {2,3,4,5}; //pinos para habilitar os displays const byte d7seg_digits[] = {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110}; //Babcdefg. 0--9 void d7seg_write(int digit, int pos, boolean point=false) { digitalWrite(d7seg_pin_enable[pos], HIGH); for(int i=0;i<7;i++) { digitalWrite(d7seg_pin_segments[i], d7seg_digits[digit] & (1 << (7-i)) ); } digitalWrite(d7seg_pin_segments[7], point); delay(1); //alterar aqui pro valor mais adequado digitalWrite(d7seg_pin_enable[pos], LOW); } void d7seg_write_number(float f, int decimals=0) { f = (f+0.000001) * pow(10, decimals); for(int i=0; i<sizeof(d7seg_pin_enable)/sizeof(int); i++) { d7seg_write( (unsigned int)(f/pow(10, i)) % 10, i, (i!=0)&&(decimals==i) ); } } /************************************************************************************************************ **********************************fim display de 7 segmentos************************************************* ************************************************************************************************************/ /************************************************************************************************************ **********************************voltimetro***************************************************************** ************************************************************************************************************/ const unsigned long r1 = 1000000; //resistor de 1M const unsigned long r2 = 100000; //resistor de 100K const unsigned int aRef = 5; //referencia de 5v float tensao = 0; //tensao lida unsigned long millis_ref = 0; const unsigned long time_refresh = 500; //faz nova leitura a cada 500 ms float get_voltage(int pin){ return (analogRead(pin) * aRef) / 1023.0 * ( (r1+r2)/r2 ); } void show_voltage(){ if ( (millis()-millis_ref) > time_refresh ) { //intervalo de tempo pra atualizar a leitura. tensao = get_voltage(A0); //calculo da tensão lida millis_ref = millis(); } float f = tensao; d7seg_write_number(f, f>=1000 ? 0 : (f>=100 ? 1 : (f>=10 ? 2 : 3) ) ); //de acordo com o numero, mostra 0, 1, 2 ou 3 casas decimais } /************************************************************************************************************ **********************************fim voltimetro************************************************************* ************************************************************************************************************/ /************************************************************************************************************ **********************************setup/loop***************************************************************** ************************************************************************************************************/ void setup(){ for(int i=2; i<=13; i++) { pinMode(i, OUTPUT); } } void loop() { show_voltage(); } /************************************************************************************************************ **********************************fim setup/loop************************************************************* ************************************************************************************************************/
Referência: http://br-arduino.org/2015/06/voltimetro-com-arduino-como-montar-programar-e-calibrar.html
Nenhum comentário:
Postar um comentário