2 Over complicated desk LED dimmer
3 2016-01-10 Dobrica Pavlinusic <dpavlin@rot13.org>
6 const int led_pin = 13;
7 const int buzzer_pin = 4;
8 const int mosfet_pins[] = { 9, 10, 6 }; // PWM pins: 3,5,6,9,10,11
9 const int ldr_pin = A3; // LDR +5 -- A3 -[10K]- GND
10 const int pir_pin = A2;
12 #define TOUCHPAD 1 // set this to 0 if debugging without touchpad
13 #define PIR_TIMEOUT 10 // s
17 PS2 mouse(8, 7); // PS2 synaptics clock, data
20 int mosfet_pwm[] = { 255, 255, 255 }; // initial and current state of mosfet pwm
24 Serial.println("setup");
26 pinMode(led_pin, OUTPUT);
27 digitalWrite(led_pin, HIGH); // signal reset
29 pinMode(buzzer_pin, OUTPUT);
31 for(int i=0; i<=2; i++) {
32 pinMode(mosfet_pins[i], OUTPUT);
33 analogWrite(mosfet_pins[i], mosfet_pwm[i]);
36 pinMode(ldr_pin, INPUT);
37 pinMode(pir_pin, INPUT);
40 Serial.println("Synaptics touchpad init");
42 mouse.write(0xff); // reset
43 mouse.read(); // ack byte
44 mouse.read(); // blank */
46 mouse.read(); // blank */
47 mouse.write(0xf0); // remote mode -- send motion data only on $EB (read data) command
49 delayMicroseconds(100);
50 mouse.write(0xe8); // set resolution
51 mouse.read(); // ack byte
52 mouse.write(0x03); // rr ( rr * 64 + ss * 16 + tt * 4 + uu == modebyte )
53 mouse.read(); // ack byte
55 mouse.read(); // ack byte
56 mouse.write(0x00); // ss
57 mouse.read(); // ack byte
59 mouse.read(); // ack byte
60 mouse.write(0x01); // tt
61 mouse.read(); // ack byte
63 mouse.read(); // ack byte
64 mouse.write(0x00); // uu
65 mouse.read(); // ack byte
66 mouse.write(0xf3); // set samplerate 20 (stores mode)
67 mouse.read(); // ack byte
69 mouse.read(); // ack byte
70 delayMicroseconds(100);
74 Serial.println("Commands: b - beep, qwe/asd/zxc - MOSFETs");
76 digitalWrite(led_pin, LOW);
80 void mosfet(int nr, int value) {
81 int pwm = mosfet_pwm[nr];
83 Serial.println("ignored");
86 Serial.print("MOSFET ");
89 Serial.println(value);
90 int sleep = 1000 / abs(pwm - value);
91 int step = pwm < value ? 1 : -1;
92 for(int i=pwm; i != value; i += step) {
93 analogWrite(mosfet_pins[nr], i);
97 analogWrite(mosfet_pins[nr], value);
98 mosfet_pwm[nr] = value;
102 unsigned int last_cx = 0;
103 unsigned int last_cy = 0;
106 // number of LDR reading to average
108 static int ldr_sum = 0;
109 static int ldr_count = 0;
112 long int pir_millis = millis();
120 byte mx; //x coordinate
121 byte my; //y coordinate
122 byte mz; //z pressure value
126 mouse.write(0xeb); // read data
129 mstat1 = mouse.read();
132 mstat2 = mouse.read();
137 // collect the bits for x and y
138 cx = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx ); // 1100-5800
139 cy = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my ); // 800-5000
141 if ( last_cx != cx || last_cy != cy ) {
145 Serial.print(cx, DEC);
146 Serial.print("\tY=");
147 Serial.print(cy, DEC);
148 Serial.print("\tZ=");
149 Serial.print(mz, DEC);
150 if ( cx > 1100 && cy > 800 ) {
151 int nr = ( cx - 1100 ) / (( 5800 - 1100 ) / 3);
152 int pwm = ( cy - 800 ) / (( 5000 - 800 ) / 255);
153 pwm -= 1; // allow off
154 Serial.print("\tmosfet = ");
156 Serial.print("\tpwm = ");
158 if ( nr >= 0 && nr <= 2 && pwm >= 0 && pwm <= 255 ) {
159 analogWrite(mosfet_pins[nr], pwm);
160 Serial.print("\tOK");
162 Serial.print("\tIGNORED");
170 if (Serial.available()) {
171 digitalWrite(led_pin, HIGH);
172 int in = Serial.read();
175 tone(buzzer_pin, 1800, 100);
177 tone(buzzer_pin, 2200, 100);
180 case 'q': mosfet(0, 255); break;
181 case 'a': mosfet(0, 127); break;
182 case 'z': mosfet(0, 0); break;
183 case 'w': mosfet(1, 255); break;
184 case 's': mosfet(1, 127); break;
185 case 'x': mosfet(1, 0); break;
186 case 'e': mosfet(2, 255); break;
187 case 'd': mosfet(2, 127); break;
188 case 'c': mosfet(2, 0); break;
191 m1 = (m1 + 10) % 255;
192 analogWrite(mosfet1_pin, m1);
193 Serial.print("MOSFET 1 = ");
197 m1 = (m1 - 10) % 255;
198 analogWrite(mosfet1_pin, m1);
199 Serial.print("MOSFET 1 = ");
204 Serial.print("unknown command ");
207 digitalWrite(led_pin, LOW);
210 int ldr = analogRead(ldr_pin);
213 if ( ldr_count > LDR_SIZE ) {
214 ldr = ldr_sum / ldr_count;
218 if ( abs(ldr - last_ldr) > 5 ) {
219 Serial.print("LDR = ");
226 int pir = digitalRead(pir_pin);
227 if ( pir != last_pir) {
229 Serial.print("PIR = ");
233 long int ms = millis();
235 if (pir_millis > 0 && ms - pir_millis > PIR_TIMEOUT * 1000 ) {
236 Serial.println("PIR timeout, fade-out");
237 pir_millis = -1; // mark that we are in timeout
238 for(int i=0; i<=2; i++) {
239 analogWrite(mosfet_pins[i], 0);
243 if (pir_millis < 0) {
244 Serial.println("PIR fade-in after timeout");
245 for(int i=0; i<=2; i++) {
246 analogWrite(mosfet_pins[i], mosfet_pwm[i]);