The sketch for my mirror with display:
/* mirror
*
* control PWM for fan, read temperatures from DS1820 and interface to PIR
*/
#include
#include
#include // interrupt routine
#define ONE_WIRE_BUS 3 // define port for DS1820 interface
#define TEMP_DEVICE_IN 28E08F3606000003
#define TEMP_DEVICE_EX 289A78370600002C
#define TEMP_DEVICE_BOARD 2873163706000020
#define LED 4
#define PIR_INTERFACE 5 // define port for PIR
#define FANSENSE 8
#define FANPWM 9
#define THRESHOLD_OFF 25
#define THRESHOLD_LOW 30
#define THRESHOLD_HIGH 40
#define THRESHOLD_ON 45
#define TASTER_DOWN 6
#define TASTER_UP 7
DeviceAddress devices[] = {TEMP_DEVICE_IN, TEMP_DEVICE_EX, TEMP_DEVICE_BOARD };
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int buttonPressed = 0;
void setup() {
Serial.begin(9600);// start serial port
sensors.begin();// Start up the library
for( int i=0; i < 3; i++){
sensors.setResolution(devices[i], 10);// set the resolution to 10 bit
}
pinMode(PIR_INTERFACE, INPUT);// read PIR status from external Board
pinMode(FANSENSE, INPUT); // read RPM data from sense of FAN
digitalWrite(FANSENSE, HIGH); // activate internal Pull Up
pinMode(LED, OUTPUT); // LED for status info
pinMode(TASTER_DOWN, INPUT); // external key for navigation input
pinMode(TASTER_DOWN, INPUT);
}
void setLED(int pulse){
if(pulse > 0){
digitalWrite(LED, HIGH);
delay(10*pulse);
digitalWrite(LED, LOW);
delay(10*pulse);
}
}
void loop() {
/* main loop sends list of values via serial line in format:
* up/down (PIR status flag), TempExternal, TempInternal, TempBoard, RPM from Fan
*/
float Temperature = 0;
double frequency = 0;
unsigned long pulseDuration = 0;
int pirStatus = 0;
pirStatus = digitalRead(PIR_INTERFACE);
if( digitalRead(TASTER_UP) == HIGH ){
buttonPressed = 2; // up taster pressed
} else if ( digitalRead(TASTER_UP) == HIGH ) {
buttonPressed = 1; // down taster pressed
} else {
buttonPressed = 0; // no button pressed
}
if (pirStatus == HIGH) {
Serial.print("up,");
} else {
Serial.print("down,");
}
for(int i = 0; i < 3; i++){
Temperature = Temperature + sensors.getTempC(devices[i]);
Serial.print(Temperature);
Serial.print(",");
}
Temperature = Temperature / 3;
// Temperature Hytherese
pulseDuration = pulseIn(FANSENSE, LOW);
frequency = 1000000/pulseDuration;
Serial.print(frequency);
Serial.println();// new line for next data
// analogWrite(FANPWM, 70); //set PWM signal
}