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
16 PS2 mouse(8, 7); // PS2 synaptics clock, data
19 int mosfet_pwm[] = { 255, 255, 0 }; // initial and current state of mosfet pwm
23 Serial.println("setup");
25 pinMode(led_pin, OUTPUT);
26 digitalWrite(led_pin, HIGH); // signal reset
28 pinMode(buzzer_pin, OUTPUT);
30 for(int i=0; i<=2; i++) {
31 pinMode(mosfet_pins[i], OUTPUT);
32 analogWrite(mosfet_pins[i], mosfet_pwm[i]);
35 pinMode(ldr_pin, INPUT);
36 pinMode(pir_pin, INPUT);
39 Serial.println("Synaptics touchpad init");
41 mouse.write(0xff); // reset
42 mouse.read(); // ack byte
43 mouse.read(); // blank */
45 mouse.read(); // blank */
46 mouse.write(0xf0); // remote mode -- send motion data only on $EB (read data) command
48 delayMicroseconds(100);
49 mouse.write(0xe8); // set resolution
50 mouse.read(); // ack byte
51 mouse.write(0x03); // rr ( rr * 64 + ss * 16 + tt * 4 + uu == modebyte )
52 mouse.read(); // ack byte
54 mouse.read(); // ack byte
55 mouse.write(0x00); // ss
56 mouse.read(); // ack byte
58 mouse.read(); // ack byte
59 mouse.write(0x01); // tt
60 mouse.read(); // ack byte
62 mouse.read(); // ack byte
63 mouse.write(0x00); // uu
64 mouse.read(); // ack byte
65 mouse.write(0xf3); // set samplerate 20 (stores mode)
66 mouse.read(); // ack byte
68 mouse.read(); // ack byte
69 delayMicroseconds(100);
73 Serial.println("Commands: b - beep, qwe/asd/zxc - MOSFETs");
75 digitalWrite(led_pin, LOW);
79 void mosfet(int nr, int value) {
80 int pwm = mosfet_pwm[nr];
82 Serial.println("ignored");
85 Serial.print("MOSFET ");
88 Serial.println(value);
89 int sleep = 1000 / abs(pwm - value);
90 int step = pwm < value ? 1 : -1;
91 for(int i=pwm; i != value; i += step) {
92 analogWrite(mosfet_pins[nr], i);
96 analogWrite(mosfet_pins[nr], value);
97 mosfet_pwm[nr] = value;
101 unsigned int last_cx = 0;
102 unsigned int last_cy = 0;
105 // number of LDR reading to average
107 static int ldr_sum = 0;
108 static int ldr_count = 0;
117 byte mx; //x coordinate
118 byte my; //y coordinate
119 byte mz; //z pressure value
123 mouse.write(0xeb); // read data
126 mstat1 = mouse.read();
129 mstat2 = mouse.read();
134 // collect the bits for x and y
135 cx = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx ); // 1100-5800
136 cy = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my ); // 800-5000
138 if ( last_cx != cx || last_cy != cy ) {
142 Serial.print(cx, DEC);
143 Serial.print("\tY=");
144 Serial.print(cy, DEC);
145 Serial.print("\tZ=");
146 Serial.print(mz, DEC);
147 if ( cx > 1100 && cy > 800 ) {
148 int nr = ( cx - 1100 ) / (( 5800 - 1100 ) / 3);
149 int pwm = ( cy - 800 ) / (( 5000 - 800 ) / 255);
150 pwm -= 1; // allow off
151 Serial.print("\tmosfet = ");
153 Serial.print("\tpwm = ");
155 if ( nr >= 0 && nr <= 2 && pwm >= 0 && pwm <= 255 ) {
156 analogWrite(mosfet_pins[nr], pwm);
157 Serial.print("\tOK");
159 Serial.print("\tIGNORED");
167 if (Serial.available()) {
168 digitalWrite(led_pin, HIGH);
169 int in = Serial.read();
172 tone(buzzer_pin, 1800, 100);
174 tone(buzzer_pin, 2200, 100);
177 case 'q': mosfet(0, 255); break;
178 case 'a': mosfet(0, 127); break;
179 case 'z': mosfet(0, 0); break;
180 case 'w': mosfet(1, 255); break;
181 case 's': mosfet(1, 127); break;
182 case 'x': mosfet(1, 0); break;
183 case 'e': mosfet(2, 255); break;
184 case 'd': mosfet(2, 127); break;
185 case 'c': mosfet(2, 0); break;
188 m1 = (m1 + 10) % 255;
189 analogWrite(mosfet1_pin, m1);
190 Serial.print("MOSFET 1 = ");
194 m1 = (m1 - 10) % 255;
195 analogWrite(mosfet1_pin, m1);
196 Serial.print("MOSFET 1 = ");
201 Serial.print("unknown command ");
204 digitalWrite(led_pin, LOW);
207 int ldr = analogRead(ldr_pin);
210 if ( ldr_count > LDR_SIZE ) {
211 ldr = ldr_sum / ldr_count;
215 if ( abs(ldr - last_ldr) > 5 ) {
216 Serial.print("LDR = ");
223 int pir = digitalRead(pir_pin);
224 if ( pir != last_pir) {
226 Serial.print("PIR = ");