I’ve been working on this project for quite some time now, and I have first working system up and running. It allows me to control two light sources (multiple lamps each) by wall switches (I previously had ordinary “cross switches” witch I modified to work as push buttons) or by a IR remote. I’m using Sony 12bit IR code, so every Sony TV or universal remote will work. Other lighting is PWM driven LEDs in wall lamps, they are controlled by remote or by rotary encoder in custom built unit which looks like incandescent bulb dimmer. In video below is the first working device, but I did a major re-design. I will post updated version, program code, schematics and pcb files as soon as I’ll have some free time.
First version of the controller:
Step 1: Software.
Here is the full code (Sorry for the comments in Latvian):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
int ir_pin = 2; //Sensor pin 1 wired through a 220 ohm resistor int led_pin = 13; //"Ready to Receive" flag, not needed but nice int debug = 0; //Serial connection must be started to debug int start_bit = 2000; //Start bit threshold (Microseconds) int bin_1 = 1000; //Binary 1 threshold (Microseconds) int bin_0 = 400; //Binary 0 threshold (Microseconds) const int rly_pin = 6; const int SPKR = 12; const int sw_pin = 8; const int ledsw_pin = 7; int sw = 0; int prevsw = 0; int ledsw = 0; int prevledsw = 0; int fadeValue = 0; volatile int fadetarget = 255; volatile int offtrg = 0; int pwm_pin = 5; int fadestep = 25; // solis pa kādu mainās gaisma LED+ un LED- int fadeSpeed = 1; int prevValue; volatile int gaisma = 0; int minimumFade = 1; // minimaalais atljautais fade value // variables encoderim --- // const int pin_A = 3; // pin 3 const int pin_B = 4; // pin 11 unsigned char encoder_A; unsigned char encoder_B; unsigned char encoder_A_prev=0; int rfeedback; // --- END --- // // --------------------- zaluzijas boolean cha_up = 0; boolean cha_down = 0; boolean chb_up = 0; boolean chb_down = 0; boolean cha_up_prev = 0; boolean cha_down_prev = 0; boolean chb_up_prev = 0; boolean chb_down_prev = 0; int in1 = A1; int in2 = A2; int in3 = A3; int in4 = A4; int senseA = A0; int senseB = A5; float volt_per_amp = 1.65; // resolution according to L298n datasheet float currentRaw; // the raw analogRead ranging from 0-1023 float currentVolts; // raw reading changed to Volts float currentAmps; // Voltage reading changed to Amps void setup() { pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(ir_pin, INPUT); digitalWrite(ir_pin, HIGH); digitalWrite(led_pin, LOW); //not ready yet Serial.begin(115200); pinMode(SPKR, OUTPUT); pinMode(led_pin, OUTPUT); pinMode(rly_pin, OUTPUT); pinMode(sw_pin, INPUT); digitalWrite(rly_pin, LOW); attachInterrupt(0, getIR, LOW); // interrupt 0 pinMode(pwm_pin, OUTPUT); // --- Variables encoderim --- // pinMode(pin_A, INPUT); digitalWrite(pin_A, HIGH); pinMode(pin_B, INPUT); digitalWrite(pin_B, HIGH); attachInterrupt(1, encoder, CHANGE); // --- END --- // } void beep(int garums, int frekvence){ for (int i=0; i < garums; i++) { // gjenereejam vajadziigo beepu digitalWrite(SPKR, HIGH); delayMicroseconds(frekvence); digitalWrite(SPKR, LOW); delayMicroseconds(frekvence); } } void loop() { if (led_pin == LOW || rfeedback <= 25) { rfeedback++; }else{ digitalWrite(led_pin, HIGH); rfeedback = 0; } delay(7); sledzis(); ledswitch(); fader(); rullocontrol(); } void fader() { if (fadetarget > fadeValue){ fadeValue += fadeSpeed; analogWrite(pwm_pin, fadeValue); if (fadeValue == 255) beep(100, 250); // Serial.print("Feidojam LED uz: "); Serial.println(fadeValue); } if (fadetarget < fadeValue){ fadeValue -= fadeSpeed; analogWrite(pwm_pin, fadeValue); if (fadeValue == minimumFade) beep(100, 250); // Serial.print("Feidojam LED uz: "); Serial.println(fadeValue); } } void sledzis(){ sw = digitalRead(sw_pin); if (sw == 0 && prevsw == 1){ beep(100, 250); if (gaisma == LOW){ digitalWrite(rly_pin, HIGH); gaisma = HIGH; }else{ digitalWrite(rly_pin, LOW); gaisma = LOW; } Serial.print("Gaismas statuss: "); Serial.println(gaisma); delay(200); } prevsw = sw; } void ledswitch(){ ledsw = digitalRead(ledsw_pin); if (ledsw == 0 && prevledsw == 1){ beep(100, 250); if (fadetarget == 0){ fadetarget = prevValue; offtrg = 0; Serial.println("Iesleedzam LED gaismas!"); }else{ fadetarget = 0; offtrg = 1; prevValue = fadeValue; Serial.println("Izsleedzam LED gaismas!"); } delay(200); } prevledsw = ledsw; } void getIR() { detachInterrupt(0); static unsigned long last_interrupt_time = 0; unsigned long interrupt_time = millis(); // If interrupts come faster than 200ms, assume it's a bounce and ignore if (interrupt_time - last_interrupt_time > 100) { int tempKey = getIRKey(); Serial.print("No pults sanjeemaam: "); Serial.println(tempKey); // if (tempKey > 0) beep(100, 250); if(tempKey == 2320){ // ------------------- Gaismas on/off key if (gaisma == LOW){ digitalWrite(rly_pin, HIGH); gaisma = HIGH; }else{ digitalWrite(rly_pin, LOW); gaisma = LOW; } Serial.print("Gaismas statuss: "); Serial.println(gaisma); } if(tempKey == 16){ // -------------------- LED Off key if (fadetarget == 0){ fadetarget = prevValue; offtrg = 0; Serial.println("Iesleedzam LED gaismas!"); }else{ fadetarget = 0; offtrg = 1; prevValue = fadeValue; Serial.println("Izsleedzam LED gaismas!"); }} if(tempKey == 2064){ // ------------------ LED + key if (offtrg == 1){ fadetarget = fadeValue; offtrg = 0; } fadetarget = constrain((fadetarget + fadestep), minimumFade, 255); Serial.print("LED gaismas liimenis: "); Serial.println(fadetarget); } if(tempKey == 1040){ // ---------------- LED - key if (offtrg == 1){ fadetarget = fadeValue; offtrg = 0; } fadetarget = constrain((fadetarget - fadestep), minimumFade, 255); Serial.print("LED gaismas liimenis: "); Serial.println(fadetarget); } if(tempKey == 1168){ // ---------------- ChA + key cha_up = !cha_up; } if(tempKey == 3216){ // ---------------- ChA - key cha_down = !cha_down; } if(tempKey == 144){ // ---------------- ChB + key chb_up = !chb_up; } if(tempKey == 2192){ // ---------------- ChB - key chb_down = !chb_down; } } last_interrupt_time = interrupt_time; attachInterrupt(0, getIR, LOW); // interrupt 0 } void rullocontrol(){ // ------------------------- Control of blinds movement if ((cha_up == 1) && (cha_up_prev == 0)){ digitalWrite(in1, HIGH); digitalWrite(in2, LOW); } if ((cha_up == 0) && (cha_up_prev == 1)){ digitalWrite(in1, LOW); digitalWrite(in2, LOW); } if ((cha_down == 1) && (cha_down_prev == 0)){ digitalWrite(in1, LOW); digitalWrite(in2, HIGH); } if ((cha_down == 0) && (cha_down_prev == 1)){ digitalWrite(in1, LOW); digitalWrite(in2, LOW); } if ((chb_up == 1) && (chb_up_prev == 0)){ digitalWrite(in3, HIGH); digitalWrite(in4, LOW); } if ((chb_up == 0) && (chb_up_prev == 1)){ digitalWrite(in3, LOW); digitalWrite(in4, LOW); } if ((chb_down == 1) && (chb_down_prev == 0)){ digitalWrite(in3, LOW); digitalWrite(in4, HIGH); } if ((chb_down == 0) && (chb_down_prev == 1)){ digitalWrite(in3, LOW); digitalWrite(in4, LOW); } cha_up_prev = cha_up; cha_down_prev = cha_down; chb_up_prev = chb_up; chb_down_prev = chb_down; // ------------------------- Lets check the load on L298N and stop movement if out of limits currentRaw = analogRead(senseA); currentVolts = currentRaw *(5.0/1024.0); currentAmps = currentVolts/volt_per_amp; if (currentAmps > 1.9 || currentAmps < 0.3){ cha_up = 0; cha_down = 0; } currentRaw = analogRead(senseB); currentVolts = currentRaw *(5.0/1024.0); currentAmps = currentVolts/volt_per_amp; if (currentAmps > 1.9 || currentAmps < 0.3){ chb_up = 0; chb_down = 0; } } void encoder(){ encoder_A = digitalRead(pin_A); encoder_B = digitalRead(pin_B); if((!encoder_A) && (encoder_A_prev)){ if(encoder_B) { // Clockwise fadetarget = constrain((fadetarget + 10), minimumFade, 255); } else { // Counterclockwise fadetarget = constrain((fadetarget - 10), minimumFade, 255); } } if((encoder_A) && (!encoder_A_prev)){ if(!encoder_B) { // Clockwise fadetarget = constrain((fadetarget + 10), minimumFade, 255); } else { // Counterclockwise fadetarget = constrain((fadetarget - 10), minimumFade, 255); } } encoder_A_prev = encoder_A; // Noglabaajam ieprieksheejo staavokli } int getIRKey() { int data[12]; data[0] = pulseIn(ir_pin, LOW); //Saakam meeriit bitus data[1] = pulseIn(ir_pin, LOW); data[2] = pulseIn(ir_pin, LOW); data[3] = pulseIn(ir_pin, LOW); data[4] = pulseIn(ir_pin, LOW); data[5] = pulseIn(ir_pin, LOW); data[6] = pulseIn(ir_pin, LOW); data[7] = pulseIn(ir_pin, LOW); data[8] = pulseIn(ir_pin, LOW); data[9] = pulseIn(ir_pin, LOW); data[10] = pulseIn(ir_pin, LOW); data[11] = pulseIn(ir_pin, LOW); digitalWrite(led_pin, LOW); if(debug == 1) { Serial.println("-----"); } for(int i=0;i<=11;i++) { //Parse them if (debug == 1) { Serial.println(data[i]); } if(data[i] > bin_1) { //is it a 1? data[i] = 1; } else { if(data[i] > bin_0) { //is it a 0? data[i] = 0; } else { data[i] = 2; //Flag the data as invalid; I don't know what it is! } } } for(int i=0;i<=11;i++) { //Pre-check data for errors if(data[i] > 1) { return -1; //Return -1 on invalid data } } int result = 0; int seed = 1; for(int i=11;i>=0;i--) { //Convert bits to integer if(data[i] == 1) { result += seed; } seed = seed * 2; } return result; //Return key number } |