Trying to understand X10 DIM

Connecting Arduino to TW523
40Watt light bulb to lamp module set to House code P, device 1
TW523 and lamp module connected to the same plug.

I have this simple code:

#include <x10.h>
#include <x10constants.h>

#define zcPin 2         // the zero crossing detect pin
#define dataPin 3       // the X10 data out pin

x10 myHouse =  x10(zcPin, dataPin);

void setup() {
  Serial.begin(9600);
  myHouse.write(HOUSE_P, UNIT_1, 1);               
  myHouse.write(HOUSE_P, OFF, 1);
}

void loop() {
   
  for(int i=1; i<21; i++){
    myHouse.write(HOUSE_P, UNIT_1, 1);
    myHouse.write(HOUSE_P, ON, 1); 
    delay(3000);
    Serial.println(i);
    delay(1000);
    myHouse.write(HOUSE_P, DIM, i);
    delay(1000);
  }
}

What I am expecting is the light to turn ON and DIM=1, ON again and DIM=2, ON again and DIM=3, etc...
What is actually happening is the light turns ON and it DIMs at each cycle of i between 2 to 6. when i=6 the light is very DIM almost off and no difference between i=6 to 20
The light never go back to ON between cycles.

I've tried setting the ON command with 3 repeats.
I've tried different house codes.

How does the DIM and BRIGHT commands work? is the repeat time = 1 to 100 to generate 1% to 100%?
And why doesn't my code work as it should?

Maybe this needs some attention:

See x10 pdf bottom of page 3.
http://www.x10pro.com/pro/pdf/technote.pdf
Bright and Dim are exceptions to this rule and should be transmitted
continuously (at least twice) with NO gaps between
codes. See figure 3.

Could anyone give me an example of how I can do this?
I have read this document over and over and it doesn't get into my head.

(note: I don't have a x10 setup but this is my interpretation)

void write(byte houseCode, byte numberCode, int numRepeats)

where numRepeats is the number of times the command is sent

myHouse.write(HOUSE_P, DIM, 6); // sends the dim command 6 times (not 6%)

Lets try an experiment, let us know what happens
example:

#include <x10.h>
#include <x10constants.h>

#define zcPin 2         // the zero crossing detect pin
#define dataPin 3       // the X10 data out pin

x10 myHouse =  x10(zcPin, dataPin);

===================
void setup() {
  Serial.begin(9600);
    myHouse.write(HOUSE_P, UNIT_1, 3);   // 3 repeats (select unit #1)
    myHouse.write(HOUSE_P, ON, 3);         // 3 repeats ( turn on the light)
}

void loop() {
   
    myHouse.write(HOUSE_P, DIM, 3);         // send the DIM command 3 times every 20 seconds
    delay(20000);                                        // 
  }
}

Have you looked at the examples? x10Fade?

/*
  X10 dimmer
 
 Dims and brightens an incandescent lamp on an X10
 lamp module.  Example was built using a PL513
 X10 One-Way Interface Module from http://www.smarthome.com 
 as the modem, and a Powerhouse X10 Lamp Module from Smarthome
 to plug the lamp in.
 
 created 15 June 2007
 modified 6 May 2011
 by Tom Igoe
 */


#include <x10.h>

const int rxPin = 3;    // data receive pin
const int txPin = 4;    // data transmit pin
const int zcPin = 2;    // zero crossing pin

void setup() {
  Serial.begin(9600);
  x10.begin(rxPin, txPin, zcPin);
}

void loop() {

  x10.beginTransmission(A);
  Serial.println("Lights on:");
  // send a "fade up" command 19 times:
  for (int steps = 0; steps < 19; steps++) {
    x10.write(BRIGHT);
  }
  delay(500);
  Serial.println("Lights off:");
  // send a "fade down" command 19 times:
  for (int steps = 0; steps < 19; steps++) {
    x10.write(DIM); 
  }
  x10.endTransmission();
  delay(500);
}

Hi Larry,

In your first example, every 20 seconds the light dimmed a little more for up to 8 cycles after which its luminosity was like a small candle. Every other cycle didn't make a difference.
Doing this also gave the same results:

void loop() {
    Serial.println("DIM");
    for(int i=1; i<4; i++){
    myHouse.write(HOUSE_P, DIM, 1);         // send the DIM command 3 times every 20 seconds
    }
    delay(20000);          
     
  }

These examples leads me to thing every time a DIM command is repeated, it DIMs some more and in this case it took 8 cycle of 3 repeats to completely dim the light so 24 times, I tried to do:

myHouse.write(HOUSE_P, DIM, 24);

but unexpectedly it took this 3 cycles to DIM the light as low as previous.
I have done a lot of different tests and I am unable to get to a conclusion of how exactly it works.

Something else I have observed, once the light is dimmed to any level the ON command has no affect, I had to make a small script and issue an Off then On before I can do some more DIM testing.

In your second code example this is an old version of X10 library that does not support reading from the device, although I didn't get to this yet the example code does not work because I have the newer version of the library.

I understand X10 is not reliable and would be tempted to just go a different route using wireless but I have about 20 light switches, lamp modules, appliance modules, 3 way switches around the house that was all working well for the past 8 years. It was controlled by an old system http://www.secant.ca/En/Company/ connected to a TW523 (the same module I am testing with) since the keypad of my old system went down with a $200 cost to replace I figured i would just replace this big system that is enclosed in a 12" X 18" box with a tiny Arduino.

Perhaps if I find the way to read from the TW523, if I could read the DIM level of a lamp I could repeat the DIM command until the DIM level is a the right setting but I am starting to wonder if there is a DIM level... There must be as on my old system I was able to select x% and I always got the same % value.

Glad you had some success.

Something else I have observed, once the light is dimmed to any level the ON command has no affect, I had to make a small script and issue an Off then On before I can do some more DIM testing.

This makes sense, since a Dimmed light is ON.

In your second code example this is an old version of X10 library that does not support reading from the device, although I didn't get to this yet the example code does not work because I have the newer version of the library.

Can you give a LINK to the library you have?

Did you try BRIGHT?

I didn't really have success... still wondering how it works!

The library I have is this one https://github.com/DougC/arduino-x10

If you do the following:

//===========
void setup()
{
  Serial.begin(9600);
    myHouse.write(HOUSE_P, UNIT_1, 3); // 3 repeats (select unit #1)  >>>repeats are necessary for noisy power line conditions
    myHouse.write(HOUSE_P, ON, 3);     // 3 repeats ( turn on the light)
    myHouse.write(HOUSE_P, DIM, 3);    // 3 repeats (more than 1 is needed, see x10 text)
}
//===========
void loop()
{
//no code here
}

I assume the light will turn on and DIM to some level(and stay at that level).
If you push Reset on the Arduino the light should DIM to a lower level (from the previous level).
Each time you push the Reset the light should DIM lower and lower.

To get the light back to full bright you either have to do a number of BRIGHT commands or you have to turn off the light and then turn it back on.

If you want to get some kind of control over the % of brightness write a Function that iterates an "number" of times. This "number" would be determined by experimentation for example it may take 5 iterations of, myHouse.write(HOUSE_P, DIM, 3); delay(500); to go to 50% brightness.

FYI
Have you seen this:
http://playground.arduino.cc/X10/CM17A
http://www.x10.com/automation/ck18a_s_ps32.html

I understand the code example you indicated very well, no problem there.
I can turn on and off any device in my house.

What I don't understand is the Bright and Dim command, I am unable to compute a value that is = to a specific brightness
If I want to DIM a light exactly 50% I can do trial and error until I get it on one light but then when I apply this value to other lights in my house I get different results.
In my previous system I was able to set 30% or 40% or 50%, etc... and no matter to which light it was applied to (as long as it is incandescent or halogen) I always got the same results.
I don't understand why if i Dim with 3 repeats 8 times it does not have the same affect as if I Dim with 24 repeats 1 time.
But Dim 1 repeat for 3 times = Dim 3 repeat for 1 time.
I am not getting any logical results.

I don't think sending the codes to a Firecracker which will relay to another wireless device will resolve my problem.

I am working with a TW523 because this device worked well with my previous system without the need for filters or repeaters...

I don't understand why if I Dim with 3 repeats 8 times it does not have the same affect as if I Dim with 24 repeats 1 time. But Dim 1 repeat for 3 times = Dim 3 repeat for 1 time.

The lack of repeatability is upsetting.
It is my understanding that the X10 DIM and BRIGHT commands require 2 repeats, you then issue them a number of times to achieve a certain level (but it looks like your experience shows otherwise).
To be consistent you will have to use candle light :wink:

FYI, you probably seen this LINK:

http://code.google.com/p/arduino-x10/source/browse/trunk/Libraries/X10ex/X10ex.cpp

I believe I have it all figured out finely!
The reason why I was always getting different results is because for some reason the TW523 remembers the status of a device, so every time I ran my program, it Dimmed or Brightened from the previous value.

I have resolved this by calling a function that fully brightens the light before processing the dim command to a specific value and concluded the value of brightness or Dimness is between 1 to 100 so if I dim 50 times the light will be at 50% of its brightness.

I have also came to conclusion that when calling the DIM or BRIGHT command, using a repeat value of greater than 1 does not work properly and I believe it is because of a bad time delay between the repeats.
when I use a loop to dim or brighten with a repeat value of 1 it is now working.

I have not yet applied this to my complete house but I have tested on one light over and over with always the same results.

I like it when a plan comes together!
The tw523 has no memory. It is the module itself that remembers the on/off & the dim/bright levels. It's-good to know that the way to use dim/bright should be used with 1 repeat. By the way the older x10 modules do not have the ability to transmit so you cannot read from them. It's probably a good idea to have a spare tw523 on hand.

Good for you for sticking with your project!

PS
I made a micro chip controller x10 receiver that has 32 relays (2 house codes) works great! 1-16 are on/off and 17-32 are momentary on.