Need help! LED 2812 on a quadcopter project Work in Progress


hi start wip thread on quadcopter/led project.

basically placing 2812' programmable led's quadcopter display , entertainment purposes (for now) ;).

i programming , adapting led sequences wish toggle through taranis radio. preferably 6-8 sequences (rainbow, blinking, etc).

i managed ppm input radio of ukhelibob

initially make things simple testing code 2 sequence:

1. off state/no lights/color

2. rainbow sequence strandtest of neopixels.

i seek on these  problems.
1. cannot turn off. thought strip.show turn "off" according neopixel library turns out not that, not unless coding wrong. has idea how this?

2. noticed code made implemented in such way sequence selected should finish first before can read other or next ppm input change display/sequence. want happen ability change sequence/display anytime want toggling switch in radio.

here code wrote constructive input appreciated. in advance. please note noob in arduino coding :d

code: [select]
#include <adafruit_neopixel.h>

#define pin 6
int signalpin = 9;
int signal;


adafruit_neopixel strip = adafruit_neopixel(60, pin, neo_grb + neo_khz800);


void setup() {
  pinmode (signalpin, input);
  serial.begin(9600);
  strip.begin();
  strip.show(); // initialize pixels 'off'
  
}
  void loop () {
     signal = pulsein(signalpin, high);
     serial.println(signal);
     switch (signal) {
     case (982):
     strip.show();// turn off pixels
     break;
     case (1489):
     rainbowcycle(20);
     break;
  }
  }
  void rainbowcycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of colors on wheel
    for(i=0; i< strip.numpixels(); i++) {
      strip.setpixelcolor(i, wheel(((i * 256 / strip.numpixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

uint32_t wheel(byte wheelpos) {
  wheelpos = 255 - wheelpos;
  if(wheelpos < 85) {
   return strip.color(255 - wheelpos * 3, 0, wheelpos * 3);
  } else if(wheelpos < 170) {
    wheelpos -= 85;
   return strip.color(0, wheelpos * 3, 255 - wheelpos * 3);
  } else {
   wheelpos -= 170;
   return strip.color(wheelpos * 3, 255 - wheelpos * 3, 0);
  }
}

you rang ?

i can't strip.show() function have no experience of hardware.

as need wait until sequence finished need rid of loop , use technique other delay().  here program wrote control leds on quad.
code: [select]
#define on high
#define off low

#define double_flash 0
#define all_off 1
#define left_right 2
#define in_out 3
#define all_on 4
#define random 5

#define leds_on_1 0
#define leds_off_1 1
#define leds_on_2 2
#define leds_off_2 3
#define wait_1 1

byte receiverpin = 11;
unsigned long duration;
byte inputstate = low;
byte previnputstate = low;
int pattern = 1;
unsigned long rearledstateinterval;
unsigned long frontledstateinterval;
int frontledstate = on;

byte rearledpins[] = {
  a0, a1, a2, a3};

byte frontledpins[] = {
  8, 9};

void setup()
{
  serial.begin(115200);
  for (int pin = 0; pin < 4; pin++)
  {
    pinmode(rearledpins[pin], output);
  }
  
    for (int pin = 0; pin < 2; pin++)
  {
    pinmode(frontledpins[pin], output);
  }
  
  delay(1000);
}

void loop()
{
  duration = pulsein(receiverpin, high);
  duration = map(duration, 1080, 1870, 0, 100);

  if (duration > 50)
  {
    inputstate = low;
  }
  else
  {
    inputstate = high;
  }
  if (inputstate != previnputstate)
  {
    rearall(off);
    pattern++;
    if (pattern > random)
    {
      pattern = double_flash;
    }
  }
  previnputstate = inputstate;

  switch (pattern)
  {
  case double_flash:
    {
      static byte state = leds_on_1;

      switch(state)
      {
      case leds_on_1:    //all on wait
        rearall(on);
        static unsigned long statestart = millis();
        rearledstateinterval = 100;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_off_1;
          statestart = millis();
        }
        break;   //end of leds_on_1 state

      case leds_off_1:
        rearall(off);
        rearledstateinterval = 200;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_on_2;
          statestart = millis();
        }      
        break;   //end of leds_off_1 state

      case leds_on_2:
        rearall(on);
        rearledstateinterval = 100;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_off_2;
          statestart = millis();
        }
        break;   //end of leds_on_2 state

      case leds_off_2:      //all off wait longer after second flash
        rearall(off);
        rearledstateinterval = 500;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_on_1;
          statestart = millis();
        }      
        break;   //end of leds_off_2 state
      }
      break;    //end of double_flash
    }

  case all_off:
    {
      rearall(off);    
      break;
    }

  case left_right:
    {    
      static byte state = leds_on_1;
      switch(state)
      {
      case leds_on_1:
        left(on);
        right(off);
        static unsigned long statestart = millis();
        rearledstateinterval = 300;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_on_2;
          statestart = millis();
        }
        break;    //end of leds_on_1 state

      case leds_on_2:
        left(off);
        right(on);
        rearledstateinterval = 300;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_on_1;
          statestart = millis();
        }
        break;    //end of leds_on_2 state
      }
      break;    //end of left/right states
    }

  case in_out:
    {
      static byte rearledstate = leds_on_1;
      switch(rearledstate)
      {
      case leds_on_1:
        outers(on);
        inners(off);
        static unsigned long rearledstatestart = millis();
        rearledstateinterval = 300;
        if (millis() - rearledstatestart >= rearledstateinterval)
        {
          rearledstate = leds_on_2;
          rearledstatestart = millis();
        }
        break;      //end of leds_on_1 rearledstate

      case leds_on_2:
        outers(off);
        inners(on);
        rearledstateinterval = 300;
        if (millis() - rearledstatestart >= rearledstateinterval)
        {
          rearledstate = leds_on_1;
          rearledstatestart = millis();
        }
        break;    //end of leds_on_2 rearledstate
      }    //end of in/out rearledstates
      break;    //end of in/out case
    }

  case all_on:
    {
      rearall(on);
      break;
    }

  case random:
    {
      static byte rearledstate = leds_on_1;
      switch(rearledstate)
      {
      case leds_on_1:
        digitalwrite(rearledpins[random(0, 4)], random(0, 2));
        static unsigned long rearledstatestart = millis();
        rearledstate = wait_1;
        rearledstateinterval = 50;
        break;

      case wait_1:
        if (millis() - rearledstatestart >= rearledstateinterval)
        {
          rearledstate = leds_on_1;
          rearledstatestart = millis();
        }
        break;
      }  //end of random rearledstates
      break;   //end of random case

    }  
  }    //end of rear leds patterns switch


  switch (frontledstate)
  {
  case on:
    front(on);
    frontledstateinterval = 80;
    static unsigned long frontledstatestart = millis();
    if (millis() - frontledstatestart >= frontledstateinterval)
    {
      frontledstate = off;
    frontledstatestart = millis();
    }
    break;    //end of front leds on

  case off:
    front(off);
    frontledstateinterval = 500;
    if (millis() - frontledstatestart >= frontledstateinterval)
    {
      frontledstate = on;
      frontledstatestart = millis();
    }
    break;    //end of front leds off
  }      //end of front leds switch
  
}      //end of loop()

void rearall(byte ledstate)
{
  outers(ledstate);
  inners(ledstate);
}

void left(byte ledstate)
{
  digitalwrite(rearledpins[2], ledstate);
  digitalwrite(rearledpins[3], ledstate);
}

void right(byte ledstate)
{
  digitalwrite(rearledpins[0], ledstate);
  digitalwrite(rearledpins[1], ledstate);
}

void inners(byte ledstate)
{
  digitalwrite(rearledpins[1], ledstate);
  digitalwrite(rearledpins[2], ledstate);
}

void outers(byte ledstate)
{
  digitalwrite(rearledpins[0], ledstate);
  digitalwrite(rearledpins[3], ledstate);
}

void front(byte ledstate)
{
  digitalwrite(frontledpins[0], ledstate);
  digitalwrite(frontledpins[1], ledstate);
}


at first sight complicated take section , study it.  it more complicated need because of nested switch/cases how did @ time allow mark/space ratio of flashes uneven.
code: [select]
 case double_flash:
    {
      static byte state = leds_on_1;

      switch(state)
      {
      case leds_on_1:    //all on wait
        rearall(on);
        static unsigned long statestart = millis();
        rearledstateinterval = 100;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_off_1;
          statestart = millis();
        }
        break;   //end of leds_on_1 state

      case leds_off_1:
        rearall(off);
        rearledstateinterval = 200;
        if (millis() - statestart >= rearledstateinterval)
        {
          state = leds_on_2;
          statestart = millis();
        }      
        break;   //end of leds_off_1 state


it uses millis() timing , switch/case code current state executed each time through loop().  because of section of code executes each time through loop()
code: [select]
 duration = pulsein(receiverpin, high);
  duration = map(duration, 1080, 1870, 0, 100);

  if (duration > 50)
  {
    inputstate = low;
  }
  else
  {
    inputstate = high;
  }
  if (inputstate != previnputstate)
  {
    rearall(off);
    pattern++;
    if (pattern > random)
    {
      pattern = double_flash;
    }
  }
  previnputstate = inputstate;

 so can respond input without waiting current sequence finish.  replace loops in rainbowcycle() function counters incremented when timing period ends (millis() - starttime >= requiredperiod)

one thing noted relying on exact values pulsein() activate sequences.  this not reliable.  i tested value using < , > , mapped raw small variation on change of switch position wider 1 make easier spot change.


Arduino Forum > Using Arduino > LEDs and Multiplexing > Need help! LED 2812 on a quadcopter project Work in Progress


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code