Go Down

Topic: Help - checking program/code for lilpad arduino project (Read 382 times) previous topic - next topic

tkmegwny

I am fumbling my way through a very simple project in which I am placing mico led's in a dress with conductive thread. I am wondering if I might get some feedback on the code, mainly, will this work? Unfortunately I have basically zero programming, electronics experience, this has been quite a painful process. First, for reference, I will cite some of the materials I am using,

LilyPad Arduino 328 Main Board
LilyPad FTDI Basic Breakout Board - 5V
LilyPad LED Micro - White
Conductive thread 28 Ohms/Ft

( For some reason in place of the #8 appears a smiley face, there's no way to fix it in this post :Z  )


#define LED1 5
#define LED2 6
#define LED3 7
#define LED4 8
#define LED5 9
#define LED6 10
#define LED7 11
#define LED8 12
#define LED9 13
#define LED10 2
#define LED11 3
#define LED12 4



#define interval 11000
#define dead_time 1000

unsigned long PWM_counter = 0;

int offset = 0;
int step_size = 200;
unsigned long on_time = 0;
unsigned long cycle_start = 0;
char dir = 1;

int offset2 = 0;
int step_size2 = 200;
unsigned long on_time2 = 0;
unsigned long cycle_start2 = 0;
char dir2 = 1;

byte LED_tracker1 = 1;
byte LED_tracker2 = 1;

void setup() {
  Serial.begin(57600);
  pinMode(LED1, OUTPUT);     
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(LED10, OUTPUT);
  pinMode(LED11, OUTPUT);
  pinMode(LED12, OUTPUT);
  offset = random(2500,7500);
  offset2 = random(2500,7500);
}

void loop()
{
  PWM_counter = micros();

  if( (PWM_counter + offset - cycle_start) >= interval) {  // completed cycle, start over
    on_time += step_size*dir;
    cycle_start = PWM_counter;
    if (on_time >= (interval - dead_time) ) {
      dir *= -1;
      on_time = interval - dead_time;
    }
    else if (on_time <= 0) {
      on_time = 0;
      dir *= -1;
      LED_tracker1 = LED_tracker2;
      while (LED_tracker1 == LED_tracker2) LED_tracker1 = random(1,10);
      offset2 = random(0,4000);
    }
  }
  else if( (PWM_counter + offset - cycle_start) >= (dead_time + on_time) ) {  // time to switch LED off
    //digitalWrite(LED1, LOW);
    LED_off(LED_tracker1);
  }
  else if( (PWM_counter + offset - cycle_start) >= dead_time) {  // time to switch LED on
    //digitalWrite(LED1, HIGH);
    LED_on(LED_tracker1);
  } 
 
  if( (PWM_counter + offset2 - cycle_start2) >= interval) {  // completed cycle, start over
    on_time2 += step_size2*dir2;
    cycle_start2 = PWM_counter;
    if (on_time2 >= (interval - dead_time) ) {
      dir2 *= -1;
      on_time2 = interval - dead_time;
    }
    else if (on_time2 <= 0) {
      on_time2 = 0;
      dir2 *= -1;
      LED_tracker2 = LED_tracker1;
      while (LED_tracker2 == LED_tracker1) LED_tracker2 = random(1,10);
      offset2 = random(0,4000);
    }
  }
  else if( (PWM_counter + offset2 - cycle_start2) >= (dead_time + on_time2) ) {  // time to switch LED off
    //digitalWrite(LED1, LOW);
    LED_off(LED_tracker2);
  }
  else if( (PWM_counter + offset2 - cycle_start2) >= dead_time) {  // time to switch LED on
    //digitalWrite(LED1, HIGH);
    LED_on(LED_tracker2);
  }
}

void LED_on(byte LED)
{
  if (LED == 1) digitalWrite(LED1, HIGH);
  if (LED == 2) digitalWrite(LED2, HIGH);
  if (LED == 3) digitalWrite(LED3, HIGH);
  if (LED == 4) digitalWrite(LED4, HIGH);
  if (LED == 5) digitalWrite(LED5, HIGH);
  if (LED == 6) digitalWrite(LED6, HIGH);
  if (LED == 7) digitalWrite(LED7, HIGH);
  if (LED == 8) digitalWrite(LED8, HIGH);
  if (LED == 9) digitalWrite(LED9, HIGH);
  if (LED == 10) digitalWrite(LED10, HIGH);
  if (LED == 11) digitalWrite(LED11, HIGH);
  if (LED == 12) digitalWrite(LED12, HIGH);

}

void LED_off(byte LED)
{
  if (LED == 1) digitalWrite(LED1, LOW);
  if (LED == 2) digitalWrite(LED2, LOW);
  if (LED == 3) digitalWrite(LED3, LOW);
  if (LED == 4) digitalWrite(LED4, LOW);
  if (LED == 5) digitalWrite(LED5, LOW);
  if (LED == 6) digitalWrite(LED6, LOW);
  if (LED == 7) digitalWrite(LED7, LOW);
  if (LED == 8) digitalWrite(LED8, LOW);
  if (LED == 9) digitalWrite(LED9, LOW);
  if (LED == 10) digitalWrite(LED10, LOW);
  if (LED == 11) digitalWrite(LED11, LOW);
  if (LED == 12) digitalWrite(LED12, LOW);
}



Osgeld

i didnt see anything off hand, though I just glanced at it


Quote
( For some reason in place of the #8 appears a smiley face, there's no way to fix it in this post :Z  )


yes there is, use the edit function, highlight your code and hit the # icon in the second row of icons at the top of the editor box


Far-seeker

Just some advice for future posts...

When you write a post there's a little button with a "#" on it.  You can use that to create a code box.  If you put your code within the text tags that appear after you click this button; not only will all smileys be ignored but the code will be in a box with it's own scroll bar, making your posts more compact and easier to read.

Go Up