lego ir 4channel transmiter with mixer


hello guy's
just finish test diy controller lego power functions
it has 4channels reversable , 2 mixer. it's rc style radio transmitter

there many library dirty job translate software line real comunication receiver, decided work out in sketch

i'm not skilled programmer maybe you'll find newbie errors way work well!!

here code want share

enjoy
salvo

code: [select]

/*
  lego ir speed remote control arduino

 futures:
 4 tx channel (a, b, c, d)

 reverse a,b,c,d
 mixer   a+b  c+d


 the circuit:
 * channel analog input 0
 * channel b analog input 1
 * channel c analog input 2
 * channel d analog input 3
 * pin 4 mixer a
 * pin 5 mixer b
 * pin 6 ir transistor input (can't supply ir diode directly arduino!!!)
 * pin 7 reverse
 * pin 8 b reverse
 * pin 9 c reverse
 * pin 10 d reverse



 */


const int mixa = 4;
const int mixb = 5;
const int ir = 6;
const int reva = 7;
const int revb = 8;
const int revc = 9;
const int revd = 10;



int nibble1;
int nibble2;
int nibble3;
int nibble4;
int nibble5;
int nibble6;
int nibble7;
int nibble8;
int nibble;
int pota ;
int potb ;
int potc ;
int potd ;
int ch1;
int ch2;
int ch3;
int ch4;



void setup()
{
  serial.begin(9600);
  pinmode(ir, output);
  pinmode (reva, input_pullup);
  pinmode (revb, input_pullup);
  pinmode (revc, input_pullup);
  pinmode (revd, input_pullup);
  pinmode (mixa, input_pullup);
  pinmode (mixb, input_pullup);

}

void loop()
{
  get_a();
  get_b();
  
  if (digitalread(mixa) == low) {
    mixer_a();
  }
  
   if (digitalread(mixb) == low) {
    mixer_b();
  }

  if (digitalread (reva) == high)
  {
    reverse_a();

  }

  if (digitalread (revb) == high)
  {
    reverse_b();
  }

  if (digitalread (revc) == high)
  {
    reverse_c();
  }

  if (digitalread (revd) == high)
  {
    reverse_d();

  }

  

  
  mapping_a();
  mapping_b ();
  nibble1 = 4 |1 ;
  nibble2 = pota;
  nibble3 = potb;
  nibble4 = (15 ^ nibble1 ^ nibble2 ^ nibble3);
  nibble5 = 4 | 0;
  nibble6 = potc;
  nibble7 = potd;
  nibble8 = (15 ^ nibble5 ^ nibble6 ^ nibble7);
  send_message();


}

void get_a()
{
  pota = analogread(a0) / 58;
  potb = analogread(a1) / 58;

  if (pota < 1) {
    pota = 1;
  }

  if (pota > 15) {
    pota = 15;
  }
  if (potb < 1) {
    potb = 1;
  }
  if (potb > 15) {
    potb = 15;
  }
}

void get_b() {
  potc = analogread(a2) / 58;
  potd = analogread(a3) / 58;

  if (potc < 1) {
    potc = 1;
  }

  if (potc > 15) {
    potc = 15;
  }
  if (potd < 1) {
    potd = 1;
  }
  if (potd > 15) {
    potd = 15;
  }
}

void reverse_a ()
{
  if (pota < 8) {
    pota = 8 + (8 - pota);
  }
  else
  {
    if (pota > 8)
    {
      pota = 8 - (pota - 8);
    }
  }

}


void reverse_b ()
{
  if (potb < 8)
  {
    potb = 8 + (8 - potb);
  }
  else
  {
    if (potb > 8)
    {
      potb = 8 - (potb - 8);
    }
  }
}
void reverse_c ()
{
  if (potc < 8) {
    potc = 8 + (8 - potc);
  }
  else
  {
    if (potc > 8)
    {
      potc = 8 - (potc - 8);
    }
  }

}

void reverse_d ()
{
  if (potd < 8) {
    potd = 8 + (8 - potd);
  }
  else
  {
    if (potd > 8)
    {
      potd = 8 - (potd - 8);
    }
  }

}
void mixer_a ()
{
 
     int delta_b;
    
     delta_b = potb  - 8;
    
    
  ch1 = pota + delta_b;
  ch2 = pota - delta_b;
  
  if (ch1 < 1)
  {ch1 = 1;}
  if(ch2 < 1)
  {ch2 = 1;}
  if (ch1 > 15)
  {ch1 = 15;}
 if (ch2 > 15)
  {ch2 = 15;}
  
pota= ch1;
potb= ch2;
  
}

void mixer_b ()
{
 
     int delta_d;
    
     delta_d = potd  - 8;
    
    
  ch3 = potc + delta_d;
  ch4 = potc - delta_d;
  
  if (ch3 < 1)
  {ch3 = 1;}
  if(ch4 < 1)
  {ch4 = 1;}
  if (ch3 > 15)
  {ch3 = 15;}
 if (ch4 > 15)
  {ch4 = 15;}
  
potc= ch3;
potd= ch4;
  
}

void mapping_a ()
{

  //pot max = 993, pot min = 125


  if (pota < 8) {
    pota = 8 - pota;
  }
  if (pota > 8) {
    pota = (8 - (pota - 8)) + 8;
  }

  //pot max = 993, pot min = 125

  if (potb < 8) {
    potb = 8 - potb;
  }
  if (potb > 8) {
    potb = (8 - (potb - 8)) + 8;
  }
}


void mapping_b ()
{

  //pot max = 993, pot min = 125


  if (potc < 8) {
    potc = 8 - potc;
  }
  if (potc > 8) {
    potc = (8 - (potc - 8)) + 8;
  }

  //pot max = 993, pot min = 125

  if (potd < 8) {
    potd = 8 - potd;
  }
  if (potd > 8) {
    potd = (8 - (potd - 8)) + 8;
  }
}


void ir_mark()
{
  digitalwrite (ir, high);
  delaymicroseconds (10);
  digitalwrite (ir, low);
  delaymicroseconds (10);
  digitalwrite (ir, high);
  delaymicroseconds (10);
  digitalwrite (ir, low);
  delaymicroseconds (10);
  digitalwrite (ir, high);
  delaymicroseconds (10);
  digitalwrite (ir, low);
  delaymicroseconds (10);
  digitalwrite (ir, high);
  delaymicroseconds (10);
  digitalwrite (ir, low);
  delaymicroseconds (10);
  digitalwrite (ir, high);
  delaymicroseconds (10);
  digitalwrite (ir, low);
  delaymicroseconds (10);
  digitalwrite (ir, high);
  delaymicroseconds (10);
  digitalwrite (ir, low);
  delaymicroseconds (10);
}

void nibble_send ()
{
  if (bitread (nibble, 3) == 0) {
    zero();
  }
  else
  {
    uno();
  }

  if (bitread (nibble, 2) == 0) {
    zero();
  }
  else
  {
    uno();
  }

  if (bitread (nibble, 1) == 0) {
    zero();
  }
  else
  {
    uno();
  }

  if (bitread (nibble, 0) == 0) {
    zero();
  }
  else
  {
    uno();
  }

}

void uno ()
{
  ir_mark();
  delaymicroseconds (555);
}

void 0 ()
{
  ir_mark();
  delaymicroseconds (265);
}

void  start_stop () {
  ir_mark();
  delaymicroseconds (1028);

}


void message_a ()
{
  start_stop();
  nibble = nibble1;
  nibble_send();
  nibble = nibble2;
  nibble_send();
  nibble = nibble3;
  nibble_send();
  nibble = nibble4;
  nibble_send();
  start_stop();
}

void message_b ()
{
  start_stop();
  nibble = nibble5;
  nibble_send();
  nibble = nibble6;
  nibble_send();
  nibble = nibble7;
  nibble_send();
  nibble = nibble8;
  nibble_send();
  start_stop();
}

void send_message ()
{
  message_a();
  delay (16);
  message_b();
  delay (16);
  message_a();
  delay (16);
  message_b();
  delay (16);
  message_a();
  delay (16);
  message_b();
  delay (16);
  message_a();
  delay (16);
  message_b();
  delay (16);
  message_a();
  delay (16);
  message_b();


}







hello all,

the starter of thread pushed me process lego powerfunctions on trains + older lego rc trains ir protocol no explaination on internet.
with tools reverse engineering bitstreams results in this:

start = stop   bitstream 36khz 10 bits en pause 1930 mu s
uno bitstream 36khz 10 bits , pause 280 mu s
zero bitstream 36khz 10 bits en pause 844 mu s
1 bit has duty-cycle 16 mu s high , 11,5 mu s low

pauze (common factor 18 ms, dus verhoudingen
channel actie bitstreams blok-pauze-blok-pauze-blok-pauze-blok)
1 start 1111 1111 1110 0001 stop 3+2+2
     1000 0111 1111 1110 1001 1000 4+5+3
1 down start 1111 1111 1101 0010 stop 2+2+3  
       start 0111 1111 1101 1010 stop 3+2+3
1 stop start 0111 1111 1111 1000 stop       3+2+2
        start 1111 1111 1111 0000 stop --------
1 horn start 1111 1111 1100 0011 stop --------
       start 0111 1111 1100 1011 stop --------
2 up   start 1110 1111 1110 0010 stop 2+3+5
        start 0110 1111 1110 1010 stop --------
2 down start 1110 1111 1101 0011 stop 5+3+3
        start 0110 1111 1101 1011 stop --------
2 stop start 1110 1111 1111 0001 stop 5+4+4
       start 0110 1111 1111 1001 stop --------
2 horn start 1110 1111 1100 0100 stop --------
       start 0110 1111 1100 1100 stop --------
3 start 1101 1111 1110 0011 stop 5+4+2
       start 0101 1111 1110 1011 stop --------
3 down start 1101 1111 1101 0100 stop 3+2+2
       start 0101 1111 1101 1100 stop --------
3 stop start 1101 1111 1111 0010 stop 5+4+5
       start 0101 1111 1111 1010 stop --------
3 horn start 1101 1111 1100 0101 stop --------
        start 0101 1111 1100 1101 stop --------
4 start 1100 1111 1110 0100 stop 2+3+5
       start 0100 1111 1110 1100 stop --------
4 down start 1100 1111 1110 0101 stop 3+2+3
      start 0100 1111 1110 1101 stop --------
4 stop start 0100 1111 1111 1011 stop 3+2+3
       start 1100 1111 1111 0011 stop 2+3+3
4 horn start 1100 1111 1100 0110 stop --------
         start 0100 1111 1100 1110 stop --------


(stop- button keeps repeating when hold, others (up, down, horn) send once message

1e nibble: bit1 bit2 bit3 bit4
kanaal 1 toggle   1    1    1
kanaal 2 toggle   1    1    0
kanaal 3 toggle   1    0    1
kanaal 4 toggle   1    0    0

2e nibble            1   1    1     1

3e nibble            1   1  
up    1      0
down    0      1
stop    1      1
horn    1      0

4e nibble toggle   ?    ?      ?    (checksum ???)
                               togglebit reverse 1st nibble bit1


Arduino Forum > Development > Other Software Development > lego ir 4channel transmiter with mixer


arduino

Comments