Need help in interactive motion lighting

Hello everyone,

I 'm working on my first Arduino project to control 16 LEDs with a TLC5940 which receives data according to 2 tilt sensors. So, what I 'm planning to do is: Tilt sensors off --> LEDs off, tilt sensors on --> LEDs on, tilt sensors off --> LEDs fading from 4095 to 0. I have successfully planned to do this for all 16 LEDs and one tilt sensor. The problem appears when I divide the LEDs into 2 groups of 8 and I connect the second sensor to the circuit. Each group works individually waiting for the other to finish it's cycle in order for it to start it's own. What I mean is that there are 2 different distinctive loops here, while I ' d like them to mix up.

Could anyone tell me what am I missing here please? I 'd really appreciate that.

Sounds like your program isn't doing what you intended. Perhaps if you post the code somebody could find the error.

johnwasser:
Sounds like your program isn’t doing what you intended. Perhaps if you post the code somebody could find the error.

What you have written is a rough “program specification”, what it must accomplish. Now you need to convert that into a computer program that can execute that specification.

So now it kind of depends on your programming experiance, if you have some, then it’s time to get started programming. However if your are new to programming maybe it’s best to put off this project for awhile and start with some kind of programming tutorial that will show you in a structured manner what commands are avalible, how they work and how they can be useful to perform various tasks.

Now you need to decide which path is best per your experiance level. If just starting out with programming the second path is what I would suggest.

Yo ZZZzz

ZZZzz:
Each group works individually waiting for the other to finish it’s cycle in order for it to start it’s own. What I mean is that there are 2 different distinctive loops here, while I ’ d like them to mix up.
Could anyone tell me what am I missing here please? I 'd really appreciate that.

I think I know what you are on about. You have to start splitting up your program to share time and data between tasks and you are wondering how to do that.

First step: If you are still using delay() or looking at examples that use delay(), you need to stop. Delay() is what stops you doing multiple things at once. There are some tutorials around for using millis() and if() tests instead of delay() but I think you will learn cooler stuff and learn it faster if you use a library that uses the technique first.

I started with EventFuse http://arduino.cc/playground/Code/EventFuse because it had some extra power I wanted to use.

but Simple Timer looks easier http://arduino.cc/playground/Code/SimpleTimer and will do what you need.

The examples provided are useful but to really use the library it’s best to know how it works. Look at the source files to get a better idea of the limitations of the library.

Timing is one part of the program but I think the other part you really need is a useful data structure to simplify things. You can use arrays and #defines together to make code more readable. Maybe you would have something like this.

//  For 2 x TLC Groups with Leds 0-7 and Leds  8-15
#define groups 2
// these are variables for each group and the number is the position of the element in the array
#define tilt_sensor_pin 0 
#define tilt_sensor_state 1
#define tlc_channel_start 2
#define tlc_channel_end 3
// {pin2, Off, Led 0, Led 7} 
byte tlcgroup[groups][4] = {{ 2, 0, 0, 7}, { 3, 0, 8, 15}};

and then later in the code this

void Check_TiltSensors() {
  for (int i = 0; i < groups; i++) {
    tlcgroup[i][tilt_sensor_state] = digitalRead(tlcgroup[i][tilt_sensor_pin]);  }}

void Update_TLC() {
  for (int i = 0; i< groups; i++) {
    if (tlcgroup[i][tilt_sensor_state] == 1) { 
      for (byte channel = tlcgroup[i][tlc_channel_start]; channel <= tlcgroup[i][tlc_channel_end]; channel++) {
        //Tlc.set(channel,4095); 
      }}}}

void setup() { }
  timer.setInterval(50, Check_TiltSensor); //check every 50ms
  timer.setInterval(5, Update_TLC); } // update every 5ms 

void loop() {
  timer.run();
}

This is how I’d start to do what you want to do with what I know now. I think there are more advanced ways to approach this but I am still learning too.

Thank you guys all, for your quick responses. Things start to get clearer now. Vicx, thanks for everything, they are very useful, since I 'm kinda new not only to electronics but coding as well. You were right, the delay function I used for the LEDs' fading was messing everything up, so I used a code I found on the forum for controlling fading by the use of 'sin' or 'cos'. Here is the code which creates a very nice fade.

int value, value2 ;
int ledpin = 10;                           // light connected to digital pin 10
int ledpin2 = 11;                           // light connected to digital pin 11
long time=0;

int periode = 2000;
int displace = 500;

void setup()
{
  // nothing for setup
}

void loop()
{
  time = millis();
  value = 128+127*cos(2*PI/periode*time);
  value2 = 128+127*cos(2*PI/periode*(displace-time));
  analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
  analogWrite(ledpin2, value2);           // sets the value (range from 0 to 255)
}

Cheers!

Hello there,

I ‘m still facing some troubles with the code, which doesn’t seem to respond to the 2 different groups of LEDs. Both of my tilt sensors light all the LEDs of the TLC up. Unfortunately, I haven’ t worked with the timer library but i don’t really think that this is the problem. I 'm posting the code, could someone help please?

Thanks

#include "Tlc5940.h"

//  For 2 x TLC Groups with Leds 0-7 and Leds  8-15
#define groups 2
// these are variables for each group and the number is the position of the element in the array
#define tilt_sensor_pin 0  
#define tilt_sensor_state 1
#define tilt_sensor_PreviousState 2
#define tlc_channel_start 3
#define tlc_channel_end 4
// {pin2, Off, Off, Led 0, Led 7} 
byte tlcgroup[groups][5] = {
  { 2, 0, 0, 0, 7}, { 5, 0, 0, 8, 15}};
 

// Variables will change:
long lastTimeMoved = 0; 
int shakeTime=50;
int value, value2, mapped1, mapped2;
long time=0;

int periode = 6500;
int displace = 500;

void setup() { 
  Tlc.init();
  for (int i = 0; i < groups; i++) {
    pinMode(tlcgroup[i][tilt_sensor_pin], INPUT);
  }
}

void loop() {

  time = millis();
  value = 128+127*cos(2*PI/periode*time);
  value2 = 128+127*cos(2*PI/periode*(displace-time));

  mapped1 = map(value, 0, 255, 0, 4095);
  mapped2 = map(value, 0, 255, 0, 4095);

  for (int i = 0; i < groups; i++) {
    tlcgroup[i][tilt_sensor_state] = digitalRead(tlcgroup[i][tilt_sensor_pin]); 
    if (tlcgroup[i][tilt_sensor_PreviousState] != tlcgroup[i][tilt_sensor_state]){
      lastTimeMoved = millis(); 
      tlcgroup[i][tilt_sensor_PreviousState] = tlcgroup[i][tilt_sensor_state];
    }
  }

  if (millis() - lastTimeMoved > shakeTime && millis() - lastTimeMoved < 2500){
    for (int i = 0; i< groups; i++) {
      for (byte channel = tlcgroup[i][tlc_channel_start]; channel <= tlcgroup[i][tlc_channel_end]; channel++) {
        Tlc.set(channel,mapped1); 
        Tlc.update(); 

        Tlc.set(channel,mapped2);
        Tlc.update(); 
      }
    }
  }

  if (millis() - lastTimeMoved > 2500) {
    for (int i = 0; i< groups; i++) {
      for (byte channel = tlcgroup[i][tlc_channel_start]; channel <= tlcgroup[i][tlc_channel_end]; channel++) {
        Tlc.set(channel, 0);
        Tlc.update(); 
      }
    }
  }

}

could someone help please?

You need to provide a clear(er) description of the problem. What is this code supposed to do? What does it actually do?

  value = 128+127*cos(2*PI/periode*time);
  value2 = 128+127*cos(2*PI/periode*(displace-time));

All this is performed using integer arithmetic. 2 * PI / 500 is 0.

PaulS:   value = 128+127*cos(2*PI/periode*time);   value2 = 128+127*cos(2*PI/periode*(displace-time));

All this is performed using integer arithmetic. 2 * PI / 500 is 0.

Both documentation and experimentation indicate the opposite

Documentation If one of the numbers (operands) are of the type float, floating point math will be used.

Code

int periode = 20;
int displace = 500;
long time=123456;


void setup() { 
  Serial.begin(9600) ;
  Serial.println(PI) ;
  Serial.println(PI/periode) ;
  Serial.println(2*PI) ;
  Serial.println(cos(2*PI/periode*time) );
  Serial.println(128+127*cos(2*PI/periode*time) );
}
void loop() { }

Output from monitor

3.14
0.16
6.28
0.31
167.41