880048ce43661a9ab578636080f39284810d51e5
[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 }; // 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;
11
12 #define TOUCHPAD 1 // set this to 0 if debugging without touchpad
13
14 #if TOUCHPAD
15 #include <ps2.h>
16 PS2 mouse(8, 7); // PS2 synaptics clock, data
17 #endif
18
19 int mosfet_pwm[] = { 0, 0, 0 }; // current state of mosfet pwm
20
21 void setup() {
22   Serial.begin(115200);
23   Serial.println("setup");
24
25   pinMode(led_pin, OUTPUT);
26   digitalWrite(led_pin, HIGH); // signal reset
27
28   pinMode(buzzer_pin, OUTPUT);
29
30   for(int i=0; i<=2; i++) pinMode(mosfet_pins[i], OUTPUT);
31
32   pinMode(ldr_pin, INPUT);
33   pinMode(pir_pin, INPUT);
34
35 #if TOUCHPAD
36   Serial.println("Synaptics touchpad init");
37
38   mouse.write(0xff);  // reset
39   mouse.read();  // ack byte
40   mouse.read();  // blank */
41
42   mouse.read();  // blank */
43   mouse.write(0xf0);  // remote mode -- send motion data only on $EB (read data) command
44   mouse.read();  // ack
45   delayMicroseconds(100);
46   mouse.write(0xe8); // set resolution
47   mouse.read();  // ack byte
48   mouse.write(0x03); // rr  ( rr * 64  +  ss * 16  +  tt * 4  +  uu  == modebyte )
49   mouse.read();  // ack byte
50   mouse.write(0xe8);
51   mouse.read();  // ack byte
52   mouse.write(0x00); // ss
53   mouse.read();  // ack byte
54   mouse.write(0xe8);
55   mouse.read();  // ack byte
56   mouse.write(0x01); // tt
57   mouse.read();  // ack byte
58   mouse.write(0xe8);
59   mouse.read();  // ack byte
60   mouse.write(0x00); // uu
61   mouse.read();  // ack byte
62   mouse.write(0xf3); // set samplerate 20 (stores mode) 
63   mouse.read();  // ack byte
64   mouse.write(0x14);
65   mouse.read();  // ack byte
66   delayMicroseconds(100); 
67
68 #endif
69
70   Serial.println("Commands: b - beep, qwe/asd/zxc - MOSFETs");
71
72   digitalWrite(led_pin, LOW);
73
74 }
75
76 void mosfet(int nr, int value) {
77   int pwm = mosfet_pwm[nr];
78   if ( pwm == value ) {
79     Serial.println("ignored");
80     return;
81   }
82   Serial.print("MOSFET ");
83   Serial.print(nr);
84   Serial.print(" = ");
85   Serial.println(value);
86   int sleep = 1000 / abs(pwm - value);
87   int step = pwm < value ? 1 : -1;
88   for(int i=pwm; i != value; i += step) {
89     analogWrite(mosfet_pins[nr], i);
90     Serial.println(i);
91     delay(sleep);
92   }
93   analogWrite(mosfet_pins[nr], value);
94   mosfet_pwm[nr] = value;
95 }
96
97
98 unsigned int last_cx = 0;
99 unsigned int last_cy = 0;         
100
101 int last_ldr = 0;
102 // number of LDR reading to average
103 #define LDR_SIZE 100
104 static int ldr_sum = 0;
105 static int ldr_count = 0;
106
107 int last_pir = 0;
108 void loop() {
109
110 #if TOUCHPAD
111   byte mstat1;
112   byte mstat2;
113   byte mxy;
114   byte mx;                    //x coordinate
115   byte my;                    //y coordinate
116   byte mz;                    //z pressure value
117
118   unsigned int cx,cy;
119
120   mouse.write(0xeb); // read data
121   mouse.read();
122   
123   mstat1 = mouse.read();
124   mxy = mouse.read();
125   mz = mouse.read();
126   mstat2 = mouse.read();
127   mx = mouse.read();
128   my = mouse.read();
129
130
131   // collect the bits for x and y
132   cx = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx ); // 1100-5800
133   cy = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my ); // 800-5000
134
135   if ( last_cx != cx || last_cy != cy ) {
136     last_cx = cx;
137     last_cy = cy;
138     Serial.print("X=");
139     Serial.print(cx, DEC);
140     Serial.print("\tY=");
141     Serial.print(cy, DEC);
142     Serial.print("\tZ=");
143     Serial.print(mz, DEC);
144     if ( cx > 1100 && cy > 800 ) {
145       int nr  = ( cx - 1100 ) / (( 5800 - 1100 ) / 3);
146       int pwm = ( cy - 800  ) / (( 5000 - 800  ) / 255);
147       pwm -= 1; // allow off
148       Serial.print("\tmosfet = ");
149       Serial.print(nr);
150       Serial.print("\tpwm = ");
151       Serial.print(pwm);
152       if ( nr >= 0 && nr <= 2 && pwm >= 0 && pwm <= 255 ) {
153         analogWrite(mosfet_pins[nr], pwm);
154         Serial.print("\tOK");
155       } else {
156         Serial.print("\tIGNORED");
157       }
158     }
159     Serial.println();
160   }
161 #endif
162
163
164   if (Serial.available()) {
165     digitalWrite(led_pin, HIGH);
166     int in = Serial.read();
167     switch (in) {
168       case 'b':
169         tone(buzzer_pin, 1800, 100);
170         delay(200);
171         tone(buzzer_pin, 2200, 100);
172         delay(500);
173         break;
174       case 'q': mosfet(0, 255); break;
175       case 'a': mosfet(0, 127); break;
176       case 'z': mosfet(0, 0); break;
177       case 'w': mosfet(1, 255); break;
178       case 's': mosfet(1, 127); break;
179       case 'x': mosfet(1, 0); break;
180       case 'e': mosfet(2, 255); break;
181       case 'd': mosfet(2, 127); break;
182       case 'c': mosfet(2, 0); break;
183
184       /*
185               m1 = (m1 + 10) % 255;
186               analogWrite(mosfet1_pin, m1);
187               Serial.print("MOSFET 1 = ");
188               Serial.println(m1);
189               break;
190             case 'a':
191               m1 = (m1 - 10) % 255;
192               analogWrite(mosfet1_pin, m1);
193               Serial.print("MOSFET 1 = ");
194               Serial.println(m1);
195               break;
196       */
197       default:
198         Serial.print("unknown command ");
199         Serial.println(in);
200     }
201     digitalWrite(led_pin, LOW);
202   }
203
204   int ldr = analogRead(ldr_pin);
205   ldr_sum += ldr >> 2;
206   ldr_count++;
207   if ( ldr_count > LDR_SIZE ) {
208     ldr = ldr_sum / ldr_count;
209     ldr_count = 0;
210     ldr_sum = 0;
211
212     if ( abs(ldr - last_ldr) > 5 ) {  
213       Serial.print("LDR = ");
214       Serial.println(ldr);
215     }
216     last_ldr = ldr;
217   }
218
219
220   int pir = digitalRead(pir_pin);
221   if ( pir != last_pir) {
222     last_pir = pir;
223     Serial.print("PIR = ");
224     Serial.println(pir);
225   }
226
227 }