added PS/2 Synaptics touch pad for fade in/out
[Arduino] / Desk_LED_dimmer / Desk_LED_dimmer.ino
1 /*
2     Over complicated desk LED dimmer
3     2016-01-10 Dobrica Pavlinusic <dpavlin@rot13.org>
4 */
5
6 const int led_pin = 13;
7 const int buzzer_pin = 4;
8 const int mosfet_pins[] = { 9, 10, 6 };
9 int mosfet_pwm[] = { 0, 0, 0 };
10
11 #include <ps2.h>
12 PS2 mouse(8, 7); // PS2 synaptics clock, data
13
14 void setup() {
15   Serial.begin(115200);
16
17   pinMode(led_pin, OUTPUT);
18   pinMode(buzzer_pin, OUTPUT);
19
20   for(int i=0; i<sizeof(mosfet_pins); i++) pinMode(mosfet_pins[i], OUTPUT);
21
22
23   mouse.write(0xff);  // reset
24   mouse.read();  // ack byte
25   mouse.read();  // blank */
26   mouse.read();  // blank */
27   mouse.write(0xf0);  // remote mode
28   mouse.read();  // ack
29   delayMicroseconds(100);
30   mouse.write(0xe8);
31   mouse.read();  // ack byte
32   mouse.write(0x03); // x1  ( x1 * 64  +  x2 * 16  +  x3 * 4  +  x4   == modebyte )
33   mouse.read();  // ack byte
34   mouse.write(0xe8);
35   mouse.read();  // ack byte
36   mouse.write(0x00); // x2
37   mouse.read();  // ack byte
38   mouse.write(0xe8);
39   mouse.read();  // ack byte
40   mouse.write(0x01); // x3
41   mouse.read();  // ack byte
42   mouse.write(0xe8);
43   mouse.read();  // ack byte
44   mouse.write(0x00); // x4
45   mouse.read();  // ack byte
46   mouse.write(0xf3); // set samplerate 20 (stores mode) 
47   mouse.read();  // ack byte
48   mouse.write(0x14);
49   mouse.read();  // ack byte
50   delayMicroseconds(100); 
51
52
53   Serial.println("Commands: b - beep, qwe/asd/zxc - MOSFETs");
54
55 }
56
57 void mosfet(int nr, int value) {
58   int pwm = mosfet_pwm[nr];
59   if ( pwm == value ) {
60     Serial.println("ignored");
61     return;
62   }
63   Serial.print("MOSFET ");
64   Serial.print(nr);
65   Serial.print(" = ");
66   Serial.println(value);
67   int sleep = 1000 / abs(pwm - value);
68   int step = pwm < value ? 1 : -1;
69   for(int i=pwm; i != value; i += step) {
70     analogWrite(mosfet_pins[nr], i);
71     Serial.println(i);
72     delay(sleep);
73   }
74   analogWrite(mosfet_pins[nr], value);
75   mosfet_pwm[nr] = value;
76 }
77
78
79 unsigned int last_cx = 0;
80 unsigned int last_cy = 0;         
81
82
83 void loop() {
84
85   byte mstat1;
86   byte mstat2;
87   byte mxy;
88   byte mx;                    //x coordinate
89   byte my;                    //y coordinate
90   byte mz;                    //z pressure value
91
92   unsigned int cx,cy;
93
94   mouse.write(0xeb);
95   mouse.read();
96   
97   mstat1 = mouse.read();
98   mxy = mouse.read();
99   mz = mouse.read();
100   mstat2 = mouse.read();
101   mx = mouse.read();
102   my = mouse.read();
103
104
105   // collect the bits for x and y
106   cx = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx ); // 1100-5800
107   cy = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my ); // 800-5000
108
109   if ( last_cx != cx || last_cy != cy ) {
110     last_cx = cx;
111     last_cy = cy;
112     Serial.print("X=");
113     Serial.print(cx, DEC);
114     Serial.print("\tY=");
115     Serial.print(cy, DEC);
116     Serial.print("\tZ=");
117     Serial.print(mz, DEC);
118     if ( cx > 1100 && cy > 800 ) {
119       int nr  = ( cx - 1100 ) / (( 5800 - 1100 ) / 3);
120       int pwm = ( cy - 800  ) / (( 5000 - 800  ) / 255);
121       pwm -= 1; // allow off
122       Serial.print("\tmosfet = ");
123       Serial.print(nr);
124       Serial.print("\tpwm = ");
125       Serial.print(pwm);
126       if ( nr >= 0 && nr <= 2 && pwm >= 0 && pwm <= 255 ) {
127         analogWrite(mosfet_pins[nr], pwm);
128         Serial.print("\tOK");
129       } else {
130         Serial.print("\tIGNORED");
131       }
132     }
133     Serial.println();
134   }
135   
136
137   if (Serial.available()) {
138     digitalWrite(led_pin, HIGH);
139     int in = Serial.read();
140     switch (in) {
141       case 'b':
142         tone(buzzer_pin, 1800, 100);
143         delay(200);
144         tone(buzzer_pin, 2200, 100);
145         delay(500);
146         break;
147       case 'q': mosfet(0, 255); break;
148       case 'a': mosfet(0, 127); break;
149       case 'z': mosfet(0, 0); break;
150       case 'w': mosfet(1, 255); break;
151       case 's': mosfet(1, 127); break;
152       case 'x': mosfet(1, 0); break;
153       case 'e': mosfet(2, 255); break;
154       case 'd': mosfet(2, 127); break;
155       case 'c': mosfet(2, 0); break;
156
157       /*
158               m1 = (m1 + 10) % 255;
159               analogWrite(mosfet1_pin, m1);
160               Serial.print("MOSFET 1 = ");
161               Serial.println(m1);
162               break;
163             case 'a':
164               m1 = (m1 - 10) % 255;
165               analogWrite(mosfet1_pin, m1);
166               Serial.print("MOSFET 1 = ");
167               Serial.println(m1);
168               break;
169       */
170       default:
171         Serial.print("unknown command ");
172         Serial.println(in);
173     }
174     digitalWrite(led_pin, LOW);
175   }
176 }