const int led_pin = 13;
const int buzzer_pin = 4;
-const int mosfet_pins[] = { 9, 10, 6 };
-int mosfet_pwm[] = { 0, 0, 0 };
+const int mosfet_pins[] = { 9, 10, 6 }; // PWM pins: 3,5,6,9,10,11
+const int ldr_pin = A3; // LDR +5 -- A3 -[10K]- GND
+const int pir_pin = A2;
+#define TOUCHPAD 1 // set this to 0 if debugging without touchpad
+
+#if TOUCHPAD
#include <ps2.h>
PS2 mouse(8, 7); // PS2 synaptics clock, data
+#endif
+
+int mosfet_pwm[] = { 255, 255, 255 }; // initial and current state of mosfet pwm
void setup() {
Serial.begin(115200);
+ Serial.println("setup");
pinMode(led_pin, OUTPUT);
+ digitalWrite(led_pin, HIGH); // signal reset
+
pinMode(buzzer_pin, OUTPUT);
- for(int i=0; i<sizeof(mosfet_pins); i++) pinMode(mosfet_pins[i], OUTPUT);
+ for(int i=0; i<=2; i++) {
+ pinMode(mosfet_pins[i], OUTPUT);
+ analogWrite(mosfet_pins[i], mosfet_pwm[i]);
+ }
+
+ pinMode(ldr_pin, INPUT);
+ pinMode(pir_pin, INPUT);
+#if TOUCHPAD
+ Serial.println("Synaptics touchpad init");
mouse.write(0xff); // reset
mouse.read(); // ack byte
mouse.read(); // blank */
+
mouse.read(); // blank */
- mouse.write(0xf0); // remote mode
+ mouse.write(0xf0); // remote mode -- send motion data only on $EB (read data) command
mouse.read(); // ack
delayMicroseconds(100);
- mouse.write(0xe8);
+ mouse.write(0xe8); // set resolution
mouse.read(); // ack byte
- mouse.write(0x03); // x1 ( x1 * 64 + x2 * 16 + x3 * 4 + x4 == modebyte )
+ mouse.write(0x03); // rr ( rr * 64 + ss * 16 + tt * 4 + uu == modebyte )
mouse.read(); // ack byte
mouse.write(0xe8);
mouse.read(); // ack byte
- mouse.write(0x00); // x2
+ mouse.write(0x00); // ss
mouse.read(); // ack byte
mouse.write(0xe8);
mouse.read(); // ack byte
- mouse.write(0x01); // x3
+ mouse.write(0x01); // tt
mouse.read(); // ack byte
mouse.write(0xe8);
mouse.read(); // ack byte
- mouse.write(0x00); // x4
+ mouse.write(0x00); // uu
mouse.read(); // ack byte
mouse.write(0xf3); // set samplerate 20 (stores mode)
mouse.read(); // ack byte
mouse.read(); // ack byte
delayMicroseconds(100);
+#endif
Serial.println("Commands: b - beep, qwe/asd/zxc - MOSFETs");
+ digitalWrite(led_pin, LOW);
+
}
void mosfet(int nr, int value) {
unsigned int last_cx = 0;
unsigned int last_cy = 0;
+int last_ldr = 0;
+// number of LDR reading to average
+#define LDR_SIZE 100
+static int ldr_sum = 0;
+static int ldr_count = 0;
+int last_pir = 0;
void loop() {
+#if TOUCHPAD
byte mstat1;
byte mstat2;
byte mxy;
unsigned int cx,cy;
- mouse.write(0xeb);
+ mouse.write(0xeb); // read data
mouse.read();
mstat1 = mouse.read();
}
Serial.println();
}
-
+#endif
+
if (Serial.available()) {
digitalWrite(led_pin, HIGH);
}
digitalWrite(led_pin, LOW);
}
+
+ int ldr = analogRead(ldr_pin);
+ ldr_sum += ldr >> 2;
+ ldr_count++;
+ if ( ldr_count > LDR_SIZE ) {
+ ldr = ldr_sum / ldr_count;
+ ldr_count = 0;
+ ldr_sum = 0;
+
+ if ( abs(ldr - last_ldr) > 5 ) {
+ Serial.print("LDR = ");
+ Serial.println(ldr);
+ }
+ last_ldr = ldr;
+ }
+
+
+ int pir = digitalRead(pir_pin);
+ if ( pir != last_pir) {
+ last_pir = pir;
+ Serial.print("PIR = ");
+ Serial.println(pir);
+ }
+
}