Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdelfino/e17cd6c6d637cdc8191d to your computer and use it in GitHub Desktop.
Save carlosdelfino/e17cd6c6d637cdc8191d to your computer and use it in GitHub Desktop.
Um monitor de temperatura triplo baseado em NTC, LM35 e o termômetro interno do Arduino.
double readInternalTemp() {
ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
delay(2);
double result;
byte mean = 0;
for(;mean < MEAN_TEMP_INTERNAL; mean++){
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
result += ((ADCH << 8) | ADCL);
}
result /= mean;
result = (result - TEMP_INTERNAL_TOS) / TEMP_INTERNAL_K;
return result;
}
#ifndef PARAMETERS_H
#define PARAMETERS_H 1
#include "Arduino.h"
#include "limits.h"
#include "debug.h"
#if DEBUG > 0
#define SYNAP_SYS_BOARD "Arduino DEBUG"
const byte LASERS[] = { 2, 3, 4, 5, 6};
const byte SERVOS[] = {12, 11, 10, 9, 8};
#elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) // Arduino Mega e Variantes
//#elif defined(ARDUINO_ARCH_AVR) && defined(ARDUINO_AVR_MEGA2560)
#define SYNAP_SYS_BOARD "Arduino MEGA"
const byte LASERS[] = { 2, 3, 4, 5, 6};
const byte SERVOS[] = {12, 11, 10, 9, 8};
#elif defined(__AVR_ATmega328P__) // Arduino UNO
//#elif defined(ARDUINO_ARCH_AVR) && defined(ARDUINO_AVR_UNO)
#define SYNAP_SYS_BOARD "Arduino UNO"
const byte LASERS[] = { 2, 3, 4, 5, 6};
const byte SERVOS[] = {12, 11, 10, 9, 8};
#else
#error "Este codigo ainda nao esta pronto para outras plataformas"
#endif
#define TEMP_INTERNAL_TOS 19
#define TEMP_INTERNAL_K 10
#define MEAN_TEMP_ROOM 10
#define MEAN_TEMP_INTERNAL 10
#define MEAN_TEMP_HEAT_SINK 10
#define PIN_TEMP_HEAT_SINK A0
#define PIN_TEMP_ROOM A1
const byte NUM_LASERS = sizeof(LASERS) / sizeof(*LASERS);
const byte NUM_SERVOS = sizeof(SERVOS) / sizeof(*SERVOS);
#define MAX_SERVO_ANGLE 179
#define MIDLE_SERVO_ANGLE (MAX_SERVO_ANGLE/2)
#define MIN_SERVO_ANGLE 0
#define SERVO_DELAY_TEST 250
#define DELAY_TEMP_ROOM (1000)
#define DELAY_TEMP_HEAT_SINK (1000)
#define DELAY_TEMP_INTERNAL (1000)
// user apenas DEFAULT ou INTERNAL
#define TEMP_AREF (INTERNAL) // cuidado ao fazer esta mudanca vc pode danificar o arduino
#endif
double readInternalTemp() {
ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
delay(2);
double result;
byte mean = 0;
for (; mean < MEAN_TEMP_INTERNAL; mean++) {
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
result += ((ADCH << 8) | ADCL);
}
result /= mean;
result = (result - TEMP_INTERNAL_TOS) / TEMP_INTERNAL_K;
return result;
}
double readRoomTemp() {
double temp;
byte mean = 0;
for (; mean < MEAN_TEMP_ROOM; mean++)
temp += analogRead(PIN_TEMP_ROOM);
temp /= mean;
#if (TEMP_AREF == (DEFAULT)) // para leitura com 5V de referencia
const double fator = .48828125;
#elif (TEMP_AREF == (INTERNAL)) // para leitura com 1.1V de referencia
const double fator = .107421875;
#else
const double fator = 0;
#endif
temp *= fator;
return temp;
}
/temp/heatsink/23.74/
/temp/room/6.03/
/temp/internal/20.74
/temp/heatsink/21.30/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.31/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.29/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.32/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.34/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.34/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.32/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.34/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.32/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.33/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.34/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.33/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.34/
/temp/room/6.05/
/temp/internal/20.71
/temp/heatsink/21.34/
void loop() {
long time = millis();
if ((time - lastTimeTempHeatSink) > DELAY_TEMP_HEAT_SINK) {
analogReference(DEFAULT);
delay(10);
double temp = readHeatSingkTemp();
Serial.print("/temp/heatsink/");
Serial.print(temp);
Serial.println("/");
lastTimeTempHeatSink = time;
}
if ((time - lastTimeTempRoom) > DELAY_TEMP_ROOM) {
analogReference(TEMP_AREF);
analogRead(PIN_TEMP_ROOM);
Serial.print("/temp/room/");
double temp = readRoomTemp();
Serial.print(temp);
Serial.println("/");
analogReference(DEFAULT);
lastTimeTempRoom = time;
}
if ((time - lastTimeTempInternal) > DELAY_TEMP_INTERNAL) {
Serial.print("/temp/internal/");
Serial.println(readInternalTemp());
lastTimeTempInternal = time;
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment