bla

Ingediend door robert op vr, 10/26/2018 - 11:13

#include <I2cDiscreteIoExpander.h>

#define led1 3  // PWM
#define led2 5  // PWM
#define led3 6  // PWM
#define led4 9  // PWM
#define led5 10 // PWM
#define led6 11 // PWM

#define TRES 100

//char amountleds = 10; // leds via arduino
char amountleds = 12; // leds via arduino + 12 + 13
///char amountleds = 12; // leds via arduino
//char amountleds_tot = 15; // including i2c
//char amountleds_tot = 23; // including i2c +8+8
char amountleds_tot = 25; // including i2c +8+8 + 12+ 13
///char amountleds_tot = 28; // including i2c
//char leds[] = {3, 5, 6, 9, 10, 11, 2, 4, 7, 8};
//char leds[] = {3, 5, 6, 9, 10, 11, 2, 4, 7, 8, 12, 13}; // + 12 + 13
char leds[] = {3, 5, 6, 9, 10, 11, 2, 4, 7, 8, 12, 13}; // + 12 + 13
// 3, 5, 6, 8, 10, 11 hwpwm, 2, 4, 7, 8 12,13 softpwm
// 3, 5, 6, 8, 10, 11 hwpwm, 2, 4, 7, 8 softpwm
///char leds[] = {3, 5, 6, 9, 10, 11, 2, 4, 7, 8, 12, 13};
//char leds[] = {3, 5, 6, 9, 10, 11, 2, 4, 7, 8};
// 3, 5, 6, 8, 10, 11 hwpwm, 2, 4, 7, 8 softpwm

int brightness = 255;
int fade = -5;
long rand1; // random led
long rand2; // random timing

int tstvar = 0x0000; // i2c expander test var.

// i2c

I2cDiscreteIoExpander expander;

void flicker(int n) {
  char dir = 0;
  char cycle = 0;
  if (n<6){
    while (!cycle){
      
      analogWrite(leds[n], brightness);
      brightness = brightness + fade;

      if (brightness <= TRES || brightness == 255) {
        fade = -fade;
        if (dir == 1){ cycle = 1; }
        dir = 1;
      }
 
      delay(5);
    }
  } else {
    unsigned long time0 = 0;
    unsigned long time1 = millis();

    digitalWrite(leds[n], HIGH);
    int i = 0;
    
    while (!cycle){
      if (i>= brightness){
        digitalWrite(leds[n], LOW);  
      }
      time1 = millis();
        if (time1 - time0 >= 5) {
          time0 = time1;
    
          brightness = brightness + fade;
          if (brightness <= TRES || brightness == 255) {
            fade = -fade;
            if (dir == 1){ cycle = 1; }
            dir = 1;
          }
        }  
        i++;
        if (i==255){
          digitalWrite(leds[n], HIGH);
          i=0;
        }
      }
    }

    digitalWrite(leds[n], HIGH); // fixes bug which leaves led off
  }

void flicker_i2c(int n){

  uint8_t status;
  static uint16_t data;
  char dir = 0;
  char cycle = 0;
  char onoff = 0;
  int i = 0;
  unsigned long time0 = 0;
  unsigned long time1 = millis();
  unsigned long testtime = 0;
  brightness = 255;
  uint16_t ledon;
  uint16_t ledoff;

  ledon = 0x00;
  ledoff = 1<<n;

 
// expander.digitalWrite((1<<i)); // One led off
// expander.digitalWrite(0x00); // All on
/*      
  while (!cycle){

    if ((i>= brightness) && !onoff){

    expander.digitalWrite(ledoff);
    onoff = 1;
    }
    
    time1 = millis();
    if (time1 - time0 >= 5) {
      time0 = time1;
   
      brightness += fade;
        if (brightness <= TRES || brightness == 255) {
          fade = -fade;
          if (dir){ cycle = 1; }
          dir = 1;
        }
      }  

      i++;
      if (i==255){
        expander.digitalWrite(ledon);
        onoff=0;
        i=0;
      }
    }

  expander.digitalWrite(ledon);
  */
}

void setup() {

  Serial.begin(19200);
  Wire.begin();

  delay(50);

///* Test for only i2c expander, rest off
  for(int i=0;i<amountleds;i++){
    pinMode(leds[i], OUTPUT);

    if(i<=6){
      analogWrite(leds[i], brightness);
    } else {
      digitalWrite(leds[i], HIGH);
    }
  }

  expander.digitalWrite(0x00); // All on\
//*/  

  expander.digitalWrite(0xFFFF); // All off
  expander.digitalWrite(0xFF00); // test i2c  
  randomSeed(analogRead(0));
}

void loop() {

// test for only i2c expander
rand1 = amountleds-1;
//rand1 = 25;
//rand2 = random(1000);
rand2 = 200;
  if (rand1<10){
    flicker(rand1);
  } else {
    flicker_i2c(rand1-amountleds);
  }
  delay(rand2);

 

/*

//  rand1 = random(amountleds_tot);
  rand1 = random(amountleds); // only arduino
  //rand2 = random(10000);  
  rand2 = 100;
  //rand2 = random(analogRead(1)<<4);
  //Serial.println(rand2);

//  if (rand1<amountleds){
//    flicker(rand1);
//  } else {
//    flicker_i2c(rand1-amountleds);
//  }

  //flicker(rand1);
//    flicker_i2c(rand1-amountleds);
    
  delay(rand2);
}
*/

/* werkt niet
//  expander.digitalWrite(0x0000); // All on
//  expander.digitalWrite(0xFFFF); // All off  
  delay(500);
for (int c=0;c++;c<8){
tstvar = tstvar + 1<<c;
expander.digitalWrite(0<<8);
 
delay(500);
tstvar = tstvar - 1<<c;
expander.digitalWrite(1<<8);
}

//  expander.digitalWrite(0x0000); // All on
//  delay(500);
//  expander.digitalWrite(0xFFFF); // All off
//  delay(500);


*/

/*  
   // 'looplicht'
  for (int i=0;i<16;i++){
    //expander.digitalWrite(0xFF & (~1<<i));
    //expander.digitalWrite(0x00 | ~(1<<i));
    expander.digitalWrite(~(1<<i));
    delay(rand2);
    //expander.digitalWrite(0x00 | (~1<<i));
    expander.digitalWrite(0xFF & ~(1<<i));
    delay(rand2);
  }

*/


}