joi, 23 ianuarie 2014

Variator de tensiune pentru bec cu Arduino (VI)

   Am completat variatorul de tensiune in 16 trepte cu 2 butoane si afisaj alfanumeric LCD 16x2 cu un sistem de comanda de la distanta in infrarosu, folosind o telecomanda pentru mp3 player de masina cu codare NEC, pentru a stine, scadea/creste intensitatea si aprinde la maxim un bec cu incandescenta.
   Partea de sketch este o combinatie a celor prezentate in articolele Telecomanda infrarosu HX1838 si Variator de tensiune pentru bec cu Arduino (V):
/*
AC Light Control
 Updated by Robert Twomey <rtwomey@u.washington.edu>
 Thanks to http://www.andrewkilpatrick.org/blog/?page_id=445 
 and http://www.hoelscher-hi.de/hendrik/english/dimmer.htm
 adapted sketch by niq_ro from
 http://www.tehnic.go.ro 
 http://www.niqro.3x.ro 
 http://nicuflorica.blogspot.com 

IR Remote Kit Test
 Uses YourDuino.com IR Infrared Remote Control Kit 2
 http://arduino-direct.com/sunshop/index.php?l=product_detail&p=153
 based on code by Ken Shirriff - http://arcfn.com1
 Get Library at: https://github.com/shirriff/Arduino-IRremote
*/

#include <LiquidCrystal.h>
// use LiquidCrystal.h library for alphanumerical display 1602
LiquidCrystal lcd(13,12,11,10,9,8);
/*                                     -------------------
                                       |  LCD  | Arduino |
                                       -------------------
 LCD RS pin to digital pin 13          |  RS   |   D13   |
 LCD Enable pin to digital pin 12      |  E    |   D12   |
 LCD D4 pin to digital pin 11          |  D4   |   D11   |
 LCD D5 pin to digital pin 10          |  D5   |   D10   |
 LCD D6 pin to digital pin 9           |  D6   |    D9   |
 LCD D7 pin to digital pin 8           |  D7   |    D8   |
 LCD R/W pin to ground                 |  R/W  |   GND   |
                                       -------------------
*/

#include "IRremote.h"
/*-----( Declare Constants )-----*/
int receiver = 7; // pin 1 of IR receiver to Arduino digital pin 7
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'
/*-----( Declare Variables )-----*/

#include <TimerOne.h>           // Avaiable from http://www.arduino.cc/playground/Code/Timer1

volatile int i=0;               // Variable to use as a counter
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 3;                 // Output to Opto Triac
int buton1 = 4;                 // first button at pin 4
int buton2 = 5;                 // second button at pin 5
int dim2 = 0;                   // led control
int dim = 128;                  // Dimming level (0-128)  0 = on, 128 = 0ff
int pas = 8;                    // step for count;
// version: 4m7 (15.04.2013 - Craiova, Romania) - 16 steps, 4 button & LED blue to red (off to MAX) 
// version: 7m5 (23.01.2014 - Craiova, Romania) - 16 steps, 2 button & LCD1602

int freqStep = 75;    // This is the delay-per-brightness step in microseconds.

void setup() {  // Begin setup
Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(buton1, INPUT);  // set buton1 pin as input
  pinMode(buton2, INPUT);  // set buton1 pin as input
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  Timer1.attachInterrupt(dim_check, freqStep);      
  // Use the TimerOne Library to attach an interrupt

 lcd.begin(16, 2); // set up the LCD's number of columns and rows: 
 lcd.clear(); // clear the screen
 lcd.setCursor(2, 0); // put cursor at colon 0 and row 0
 lcd.print("16 steps AC"); // print a text
 lcd.setCursor(0, 1); // put cursor at colon 0 and row 1
 lcd.print("dimmer for bulb"); // print a text
 delay (3000);
 lcd.clear(); // clear the screen
 lcd.setCursor(1, 0); // put cursor at colon 0 and row 0
 lcd.print("this sketch is"); // print a text
 lcd.setCursor(1, 1); // put cursor at colon 0 and row 1
 lcd.print("made by niq_ro"); // print a text
 delay (3000);
 lcd.clear(); // clear the screen
}

void zero_cross_detect() {    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  i=0;
  digitalWrite(AC_pin, LOW);
}                                 

// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=dim) {                     
      digitalWrite(AC_pin, HIGH);  // turn on light       
      i=0;  // reset time step counter                         
      zero_cross=false;    // reset zero cross detection
    } 
    else {
      i++;  // increment time step counter                     
    }                                
  }    
}                                      

/*-----( Declare User-written Functions )-----*/
void translateIR() // takes action based on IR code received

// describing Car MP3 IR codes 
{
  switch(results.value)
  {
/*
  case 0xFFA25D:  
    Serial.println(" CH-            "); 
    break;

  case 0xFF629D:  
    Serial.println(" CH             "); 
    break;

  case 0xFFE21D:  
    Serial.println(" CH+            "); 
    break;
*/
  case 0xFF22DD:  
    {
    Serial.println(" PREV           "); 
    dim=128;
    }
    break;

  case 0xFF02FD:  
    {
    Serial.println(" NEXT           "); 
    dim=0;
    }
    break;

  case 0xFFC23D:  
    Serial.println(" PLAY/PAUSE     "); 
    break;

  case 0xFFE01F:  
    {
    Serial.println(" VOL-           "); 
    if (dim<127)  
   {
    dim = dim + pas;
    if (dim>127) 
    {
      dim=128; // in vechiul sketch era 127
    }
    }
    }
    break;

  case 0xFFA857:  
    {
    Serial.println(" VOL+           "); 
      {
  if (dim>5)  
  {
     dim = dim - pas;
  if (dim<0) 
    {
      dim=0;  // in vechiul sketch era 1
    }
   }
   }
   }
    break;

  case 0xFF906F:  
    Serial.println(" EQ             "); 
    break;

  case 0xFF6897:  
    {
    Serial.println(" 0              "); 
 //   analogWrite(ledr, 0); 
 //   analogWrite(leda, 0); 
 //   analogWrite(ledv, 0); 
     }
    break;

  case 0xFF9867:  
    Serial.println(" 100+           "); 
    break;

  case 0xFFB04F:  
    Serial.println(" 200+           "); 
    break;

  case 0xFF30CF:  
    {
    Serial.println(" 1              "); 
 //   analogWrite(leda, 255);
    }
    break;

  case 0xFF18E7:  
    {  
    Serial.println(" 2              "); 
 //   analogWrite(ledv, 255);
    }
    break;

  case 0xFF7A85:  
    {
    Serial.println(" 3              "); 
 //   analogWrite(ledr, 255);
    }
    break;

  case 0xFF10EF:  
    {
    Serial.println(" 4              "); 
 //   analogWrite(leda, 122);
    }
    break;

  case 0xFF38C7:  
    {
    Serial.println(" 5              "); 
 //   analogWrite(ledv, 122);
    }
    break;

  case 0xFF5AA5:  
    {
    Serial.println(" 6              "); 
 //   analogWrite(ledr, 122);
    }
    break;

  case 0xFF42BD:  
    {
    Serial.println(" 7              "); 
 //   analogWrite(leda, 0);
    }
    break;

  case 0xFF4AB5:  
    {
    Serial.println(" 8              "); 
 //   analogWrite(ledv, 0);
    }
    break;

  case 0xFF52AD:  
    {
    Serial.println(" 9              "); 
 //   analogWrite(ledr, 0);
    }
    break;

  default: 
    Serial.println(" other button   ");

  }
}

void loop() {  
  digitalWrite(buton1, HIGH);
  digitalWrite(buton2, HIGH);
  
 if (digitalRead(buton1) == LOW)   
   {
  if (dim<127)  
  {
    dim = dim + pas;
    if (dim>127) 
    {
      dim=128; // in vechiul sketch era 127
    }
  }
   }
  if (digitalRead(buton2) == LOW)   
   {
  if (dim>5)  
  {
     dim = dim - pas;
  if (dim<0) 
    {
      dim=0;  // in vechiul sketch era 1
    }
   }
   }
    while (digitalRead(buton1) == LOW) {  }              
    delay(10); // waiting little bit...  
    while (digitalRead(buton2) == LOW) {  }              
    delay(10); // waiting little bit...    
 
// remote
   if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR(); 
    irrecv.resume(); // receive the next value
  }  

          
 
 delay (100);
 lcd.setCursor(2, 0); // put cursor at colon 0 and row 0
 lcd.print("power is "); // print a text
 lcd.print(100*(128-dim)/128);
 lcd.print("%    "); // print a text
/*
 lcd.setCursor(1, 1); // put cursor at colon 1 and row 1
 lcd.print("dim. level="); // print a text
 lcd.print(dim);
 lcd.print("  "); // print a text
*/
lcd.setCursor(0, 1); // put cursor at colon 0 and row 1

dim2=128-dim; // variable use for graphics

if (dim2<1) lcd.print("----------------");
else 
if (dim2<9) lcd.print("*---------------");
else 
if (dim2<17) lcd.print("-*--------------");
else
if (dim2<25) lcd.print("--*-------------");
else
if (dim2<33) lcd.print("---*------------");
else
if (dim2<41) lcd.print("----*-----------");
else
if (dim2<49) lcd.print("-----*----------");
else
if (dim2<57) lcd.print("------*---------");
else
if (dim2<65) lcd.print("-------*--------");
else
if (dim2<73) lcd.print("--------*-------");
else
if (dim2<81) lcd.print("---------*------");
else
if (dim2<89) lcd.print("----------*-----");
else
if (dim2<97) lcd.print("-----------*----");
else
if (dim2<105) lcd.print("------------*---");
else
if (dim2<113) lcd.print("-------------*--");
else
if (dim2<121) lcd.print("--------------*-");
else
if (dim2>127) lcd.print("---------------*");
}
   Filmuletele care prezinta modulde comportare amontajelor sunt:
ac light dimmer with Arduino (XIX)
ac light dimmer with Arduino (XX)
24.01.2014
   Schema de conectare a modulelor este:
   Schema modulului de detectare a trecerii prin zero a tensiunii retelei si partea de comanda efectiva a becului este:

Niciun comentariu:

Trimiteți un comentariu