start with first two mosfets fully enabled
[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[] = { 255, 255, 0 }; // initial and 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++) {
31     pinMode(mosfet_pins[i], OUTPUT);
32     analogWrite(mosfet_pins[i], mosfet_pwm[i]);
33   }
34
35   pinMode(ldr_pin, INPUT);
36   pinMode(pir_pin, INPUT);
37
38 #if TOUCHPAD
39   Serial.println("Synaptics touchpad init");
40
41   mouse.write(0xff);  // reset
42   mouse.read();  // ack byte
43   mouse.read();  // blank */
44
45   mouse.read();  // blank */
46   mouse.write(0xf0);  // remote mode -- send motion data only on $EB (read data) command
47   mouse.read();  // ack
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
53   mouse.write(0xe8);
54   mouse.read();  // ack byte
55   mouse.write(0x00); // ss
56   mouse.read();  // ack byte
57   mouse.write(0xe8);
58   mouse.read();  // ack byte
59   mouse.write(0x01); // tt
60   mouse.read();  // ack byte
61   mouse.write(0xe8);
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
67   mouse.write(0x14);
68   mouse.read();  // ack byte
69   delayMicroseconds(100); 
70
71 #endif
72
73   Serial.println("Commands: b - beep, qwe/asd/zxc - MOSFETs");
74
75   digitalWrite(led_pin, LOW);
76
77 }
78
79 void mosfet(int nr, int value) {
80   int pwm = mosfet_pwm[nr];
81   if ( pwm == value ) {
82     Serial.println("ignored");
83     return;
84   }
85   Serial.print("MOSFET ");
86   Serial.print(nr);
87   Serial.print(" = ");
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);
93     Serial.println(i);
94     delay(sleep);
95   }
96   analogWrite(mosfet_pins[nr], value);
97   mosfet_pwm[nr] = value;
98 }
99
100
101 unsigned int last_cx = 0;
102 unsigned int last_cy = 0;         
103
104 int last_ldr = 0;
105 // number of LDR reading to average
106 #define LDR_SIZE 100
107 static int ldr_sum = 0;
108 static int ldr_count = 0;
109
110 int last_pir = 0;
111 void loop() {
112
113 #if TOUCHPAD
114   byte mstat1;
115   byte mstat2;
116   byte mxy;
117   byte mx;                    //x coordinate
118   byte my;                    //y coordinate
119   byte mz;                    //z pressure value
120
121   unsigned int cx,cy;
122
123   mouse.write(0xeb); // read data
124   mouse.read();
125   
126   mstat1 = mouse.read();
127   mxy = mouse.read();
128   mz = mouse.read();
129   mstat2 = mouse.read();
130   mx = mouse.read();
131   my = mouse.read();
132
133
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
137
138   if ( last_cx != cx || last_cy != cy ) {
139     last_cx = cx;
140     last_cy = cy;
141     Serial.print("X=");
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 = ");
152       Serial.print(nr);
153       Serial.print("\tpwm = ");
154       Serial.print(pwm);
155       if ( nr >= 0 && nr <= 2 && pwm >= 0 && pwm <= 255 ) {
156         analogWrite(mosfet_pins[nr], pwm);
157         Serial.print("\tOK");
158       } else {
159         Serial.print("\tIGNORED");
160       }
161     }
162     Serial.println();
163   }
164 #endif
165
166
167   if (Serial.available()) {
168     digitalWrite(led_pin, HIGH);
169     int in = Serial.read();
170     switch (in) {
171       case 'b':
172         tone(buzzer_pin, 1800, 100);
173         delay(200);
174         tone(buzzer_pin, 2200, 100);
175         delay(500);
176         break;
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;
186
187       /*
188               m1 = (m1 + 10) % 255;
189               analogWrite(mosfet1_pin, m1);
190               Serial.print("MOSFET 1 = ");
191               Serial.println(m1);
192               break;
193             case 'a':
194               m1 = (m1 - 10) % 255;
195               analogWrite(mosfet1_pin, m1);
196               Serial.print("MOSFET 1 = ");
197               Serial.println(m1);
198               break;
199       */
200       default:
201         Serial.print("unknown command ");
202         Serial.println(in);
203     }
204     digitalWrite(led_pin, LOW);
205   }
206
207   int ldr = analogRead(ldr_pin);
208   ldr_sum += ldr >> 2;
209   ldr_count++;
210   if ( ldr_count > LDR_SIZE ) {
211     ldr = ldr_sum / ldr_count;
212     ldr_count = 0;
213     ldr_sum = 0;
214
215     if ( abs(ldr - last_ldr) > 5 ) {  
216       Serial.print("LDR = ");
217       Serial.println(ldr);
218     }
219     last_ldr = ldr;
220   }
221
222
223   int pir = digitalRead(pir_pin);
224   if ( pir != last_pir) {
225     last_pir = pir;
226     Serial.print("PIR = ");
227     Serial.println(pir);
228   }
229
230 }