//#include #include "Arduino.h" #include #include #include #include // Actual (accurate) measurements // 132322 Hz = off // 141328 Hz = on #define DISCONNECTED_FREQUENCY 10000 #define THRESHOLD_FREQUENCY 52500 #define POWER_RELAY 15 #define SPEAKER_RELAY 16 #define LED 13 #define OPTICAL_POWER 4 // Prescaler factors for high and low speed. // mode 0 - Do nothing - normal 16Mhz clock // mode 1 - Clock divide by 2 - 8Mhz // Mode 2 - Clock divide by 4 - 4Mhz // Mode 3 - Clock divide by 8 - 2Mhz // Mode 4 - Clock divide by 16 - 1Mhz #define PRESCALER_LOW 4 #define PRESCALER_HIGH 1 #define DO_SERIAL //#define OVERFLOWS // I found this at ftp://82.140.81.3/pub/atmel/_AVR_MCUs_8bit/_WITH_USB/AT90USBKEY_CD/starterkits/STK525-USBKEY/firmware/at90usb128-usbkey-demo-3enum-host-mouse-1_0_4/Atmel/at90usb128-usbkey-demo-3enum-host-mouse/at90usb128/demo/ms_mouse_host_mouse/doc/html/a00101.html #define Set_prescaler(x) (CLKPR = (1< THRESHOLD_FREQUENCY) { if(state_direction != 1) { state = 0; state_direction = 1; } } else { // Frequency is in the "off" zone if(state_direction != -1) { state = 0; state_direction = -1; } } } if(long_tick_counter == 1) { // Take a frequency reading... digitalWrite(OPTICAL_POWER,HIGH); frequency_counter = 0; short_tick_counter = 1; #ifdef OVERFLOWS overflows = 0; TIFR1 =(1<=3) { long_tick_counter = 0; } state = state + state_direction; if(state == 1) { // Turn on the power relay digitalWrite(POWER_RELAY, HIGH); } else if(state == 5) { // turn on the speaker relay digitalWrite(SPEAKER_RELAY, HIGH); state_direction = 0; } if(state == -20) { // Turn off the speaker relay digitalWrite(SPEAKER_RELAY, LOW); } else if(state == -23) { // Turn off the power relay digitalWrite(POWER_RELAY, LOW); state_direction = 0; } } void setup(void) { int i; for(i=0; i <= 19; i++) { // Set all pins as inputs, with internal pull-ups // This is very nearly as power efficient as setting them all // as output/low, but safer in terms of accidental short circuit pinMode(i, INPUT); digitalWrite(i, HIGH); } // Now setup the pins we're using... // pin 5 = the digital input used by the frequency counter digitalWrite(5, LOW); // don't use pull-up on the optical in pinMode(POWER_RELAY, OUTPUT); pinMode(SPEAKER_RELAY, OUTPUT); pinMode(LED, OUTPUT); pinMode(OPTICAL_POWER,OUTPUT); digitalWrite(POWER_RELAY, LOW); digitalWrite(SPEAKER_RELAY, LOW); digitalWrite(OPTICAL_POWER,LOW); #ifdef DO_SERIAL // To use the serial port, the receiver's speed needs to be the speed // here divided by the PRESCALER_LOW divider. For example: // Prescaler mode 4 -> divider = 16, so actual port speed = 2400 Serial.begin(38400); // connect to the serial port #endif // hardware counter setup ( refer atmega168.pdf chapter 16-bit counter1) TCCR1A=0; // reset timer/counter1 control register A TCCR1B=0; // reset timer/counter1 control register A TCNT1=0; // counter value = 0 // set timer/counter1 hardware as counter , counts events on pin T1 ( arduino pin 5) // normal mode, wgm10 .. wgm13 = 0 TCCR1B |= (1<