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;
13 #define TOUCHPAD 1 // set this to 0 if debugging without touchpad
14 #define PIR_TIMEOUT 10 // s
18 PS2 mouse(8, 7); // PS2 synaptics clock, data
22 int mosfet_pwm[] = { 127, 127, 127 }; // initial and current state of mosfet pwm
24 void MOSFET_PWM(int i, int pwm) {
25 analogWrite(mosfet_pins[i], pwm);
26 Serial.print("MOSFET=");
35 Serial.println("setup");
37 pinMode(led_pin, OUTPUT);
38 digitalWrite(led_pin, HIGH); // signal reset
40 pinMode(buzzer_pin, OUTPUT);
42 for(int i=0; i<=2; i++) {
43 pinMode(mosfet_pins[i], OUTPUT);
44 analogWrite(mosfet_pins[i], mosfet_pwm[i]);
47 pinMode(ldr_pin, INPUT);
48 pinMode(pir_pin, INPUT);
51 Serial.println("Synaptics touchpad init");
53 mouse.write(0xff); // reset
54 mouse.read(); // ack byte
55 mouse.read(); // blank */
57 mouse.read(); // blank */
58 mouse.write(0xf0); // remote mode -- send motion data only on $EB (read data) command
60 delayMicroseconds(100);
61 mouse.write(0xe8); // set resolution
62 mouse.read(); // ack byte
63 mouse.write(0x03); // rr ( rr * 64 + ss * 16 + tt * 4 + uu == modebyte )
64 mouse.read(); // ack byte
66 mouse.read(); // ack byte
67 mouse.write(0x00); // ss
68 mouse.read(); // ack byte
70 mouse.read(); // ack byte
71 mouse.write(0x01); // tt
72 mouse.read(); // ack byte
74 mouse.read(); // ack byte
75 mouse.write(0x00); // uu
76 mouse.read(); // ack byte
77 mouse.write(0xf3); // set samplerate 20 (stores mode)
78 mouse.read(); // ack byte
80 mouse.read(); // ack byte
81 delayMicroseconds(100);
85 Serial.println("Commands: b - beep, qwe/asd/zxc - MOSFETs");
87 digitalWrite(led_pin, LOW);
91 void mosfet(int nr, int value) {
92 int pwm = mosfet_pwm[nr];
94 Serial.println("ignored");
97 Serial.print("MOSFET ");
100 Serial.println(value);
101 int sleep = 1000 / abs(pwm - value);
102 int step = pwm < value ? 1 : -1;
103 for(int i=pwm; i != value; i += step) {
107 MOSFET_PWM(nr, value);
108 mosfet_pwm[nr] = value;
112 unsigned int last_cx = 0;
113 unsigned int last_cy = 0;
116 // number of LDR reading to average
118 static int ldr_sum = 0;
119 static int ldr_count = 0;
122 long int pir_millis = millis();
131 byte mx; //x coordinate
132 byte my; //y coordinate
133 byte mz; //z pressure value
137 mouse.write(0xeb); // read data
140 mstat1 = mouse.read();
143 mstat2 = mouse.read();
148 // collect the bits for x and y
149 cx = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx ); // 1100-5800
150 cy = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my ); // 800-5000
152 if ( last_cx != cx || last_cy != cy ) {
156 Serial.print(cx, DEC);
157 Serial.print("\tY=");
158 Serial.print(cy, DEC);
159 Serial.print("\tZ=");
160 Serial.print(mz, DEC);
161 if ( cx > 1100 && cy > 800 ) {
162 int nr = ( cx - 1100 ) / (( 5800 - 1100 ) / 3);
163 int pwm = ( cy - 800 ) / (( 5000 - 800 ) / 255);
164 pwm -= 1; // allow off
165 Serial.print("\tmosfet = ");
167 Serial.print("\tpwm = ");
169 if ( nr >= 0 && nr <= 2 && pwm >= 0 && pwm <= 255 ) {
170 Serial.println("\tOK");
172 mosfet_pwm[nr] = pwm;
174 Serial.println("\tIGNORED");
181 if (Serial.available()) {
182 digitalWrite(led_pin, HIGH);
183 int in = Serial.read();
186 tone(buzzer_pin, 1800, 100);
188 tone(buzzer_pin, 2200, 100);
191 case 'q': mosfet(0, 255); break;
192 case 'a': mosfet(0, 127); break;
193 case 'z': mosfet(0, 0); break;
194 case 'w': mosfet(1, 255); break;
195 case 's': mosfet(1, 127); break;
196 case 'x': mosfet(1, 0); break;
197 case 'e': mosfet(2, 255); break;
198 case 'd': mosfet(2, 127); break;
199 case 'c': mosfet(2, 0); break;
202 m1 = (m1 + 10) % 255;
203 analogWrite(mosfet1_pin, m1);
204 Serial.print("MOSFET 1 = ");
208 m1 = (m1 - 10) % 255;
209 analogWrite(mosfet1_pin, m1);
210 Serial.print("MOSFET 1 = ");
215 Serial.print("unknown command ");
218 digitalWrite(led_pin, LOW);
221 int ldr = analogRead(ldr_pin);
224 if ( ldr_count > LDR_SIZE ) {
225 ldr = ldr_sum / ldr_count;
229 if ( abs(ldr - last_ldr) > 5 ) {
230 Serial.print("LDR = ");
237 int pir = digitalRead(pir_pin);
238 if ( pir != last_pir) {
240 Serial.print("PIR = ");
244 long int ms = millis();
246 if (pir_millis > 0 && ms - pir_millis > PIR_TIMEOUT * 1000 ) {
247 Serial.println("PIR timeout, fade-out");
248 pir_millis = -1; // mark that we are in timeout
249 for(int i=0; i<=2; i++) {
254 if (pir_millis < 0) {
255 Serial.println("PIR fade-in after timeout");
256 for(int i=0; i<=2; i++) {
257 MOSFET_PWM(i, mosfet_pwm[i]);