LT3965 - 8 开关矩阵 LED 调光器

特点

  • 8 个独立的 17V 330mΩ NMOS 开关
  • 每个开关对 1 至 4 个 LED 进行独立接通 / 关断 / 调光控制
  • 具可编程的开路 LED 和短路 LED 故障报告功能的 I2C 多支路串行接口
  • 16 个唯一的 I2C 地址
  • VDD 范围为 2.7V 至 5.5V 和 VIN 范围为 8V 至 60V
  • 数字可编程 256:1 PWM 调光
  • PWM 调光状态之间的褪色过渡
  • 用于 PWM 调光的任选内部时钟发生器或外部时钟源
  • 开路 LED 过压保护
  • 无闪烁 PWM 调光

典型应用

LT3965 Typical Application

描述

LT®3965 是一款 LED 旁路开关器件,用于采用一个公共电流源对 LED 灯串中的个别 LED 进行调光。它具有 8 个独立地控制的浮动源 17V/330mΩ NMOS 开关。这 8 个开关可采用并联和 / 或串联连接以对 LED 灯串中的一个或多个 LED 实施电流旁路。LT3965 使用 I2C 串行接口与主控器进行通信。可对 8 个通道中的每一个进行独立编程以对处于“恒定导通”、“恒定关断”、“无褪色变换的调光” 和 “具褪色变换的调光” 模式中的 LED 灯串实施旁路。利用褪色选项在PWM调光状态之间提供 11位分辨率对数转换。针对 PWM 调光,LT3965 提供了一个内部时钟发生器,而且也支持外部时钟源。LT3965 可为每个通道报告开路 LED 和短路 LED 等故障情况。4 个地址选择引脚允许 16 个 LT3965 器件共享 I2C 总线。该器件采用 28 引脚 TSSOP 封装。

封装

CAD 符号和封装布局: 下面可下载的 Zip 文件包含原理图符号和 PCB 封装布局 (可与 Mentor Graphics PADS® v9.5 或更高版本、和 Cadence ORCAD® v16.5 或更高版本兼容)。

如需了解完整与最新的封装信息及图例,请查阅我们的封装网页

器件型号 封装 Code 温度 封装图 RoHS 数据
LT3965EFE#PBF TSSOP-28 FE E 05-08-1663 (EA) Yes
LT3965EFE#TRPBF TSSOP-28 FE E 05-08-1663 (EA) Yes
LT3965IFE#PBF TSSOP-28 FE I 05-08-1663 (EA) Yes
LT3965IFE#TRPBF TSSOP-28 FE I 05-08-1663 (EA) Yes


LT3965 Package Drawing

订购信息

  • 以 PBF 结尾的器件型号表示这些是无铅型器件。如需了解有关含铅涂层器件的信息,请与凌力尔特公司联系。

  • 型号当中包含 TR 或 TRM 的器件分别采用卷带装500 片微型卷带装的形式进行装运。

  • 如需了解更多的细节,请查阅我们一般订购信息或产品数据手册。

各种封装型式和定价

器件型号 封装 温度 价格 (以 1 ~ 99 片为批量) 价格 (以 1000 片为批量) * RoHS 数据
LT3965EFE#PBF TSSOP-28 E $5.64 $3.95 Yes
LT3965EFE#TRPBF TSSOP-28 E $5.70 $4.01 Yes
LT3965IFE#PBF TSSOP-28 I $6.22 $4.35 Yes
LT3965IFE#TRPBF TSSOP-28 I $6.28 $4.41 Yes
购买要求提供样品

* 美国价目表所列之价格仅供预算之用,单位为美元 (针对规定批量的美国 FOB 单价),随时可能变更。国际销售价格有可能因为地方税额、费用和汇率而有所不同。如需了解特定批量的价格或货运报价,请您与凌力尔特当地的销售办事处或授权分销商联系

演示电路板

凌力尔特的演示电路板可免费提供给合资格的客户,请您与当地的销售办事处或分销商联系,申请获取演示电路板。另外,某些演示电路板也可通过信用卡在此网站上购买。演示电路板仅供评估之用。器件在实际终端应用中的正确与可靠运作的验证仍然是客户的责任。

器件型号 描述 价格 Documentation
DC2218A LT3965EFE/LT3797 Demo Board | 2-String Buck LED Driver with Matrix LED Dimmer; 9V ≤ VIN ≤ 30V, Up to 30VLED @ 500mA (8 LEDs/String) $599.00
购买
点击这里查看我们的演示板完整列表

应用

  • 汽车 LED 前照灯组
  • 大型 LED 显示器
  • 自动化相机闪光灯设备
  • RGBW 色彩混合照明

产品通知

请登录您的 MyLinear 帐户,以获得数据表更新通知、新文档发布、以及针对您喜爱产品的 LTspice 模型通告。 如果您还没有 MyLinear 帐户,可以立即 注册

忘记密码吗? 请单击这里
需要帮助吗? 如果有任何问题或意见,请发送电子邮件至 mylinear@linear.com

设计工具

Linduino

Linduino 是一款可兼容 Arduino 的平台,其用于开发和分配用于 SPI 和 I2C 兼容型集成电路的固件库和代码。Linduino One 电路板可连接 300 多款 QuikEval 演示板卡并支持多种产品类型,包括模数转换器 (ADC)数模转换器 (DAC)功率监视器等等。用于个别器件的固件库采用 C 语言编写并专为可移植到众多的处理器和微控制器而设计。每个固件库具有一种演示程序,此演示程序可上载至 Linduino One 平台以简便快捷地完成电路和软件的验证。

点击这里以了解 Linduino 的更多相关信息

 

代码

Linduino 是凌力尔特的 Arduino 兼容型系统,适用于开发和分发针对凌力尔特集成电路的固件库和代码范例。下面的代码程序可以下载或者“拷贝并粘贴”到您的设计方案之中。请访问 Linduino 主页以获取演示板、手册和设置信息。

该器件得到了代码支持: 提供了可用于该器件的代码示例。下面的代码有可能依存于完整库中提供的其他驱动程序。

Download LT3965 - DC2218A.ino

/*!
Linear Technology DC2218A Demonstration Board.
LT3965 - 8-Switch Matrix LED Dimmer

@verbatim

  Setup:

   Follow the procedure below:
  1.  Set the PATTERN SELECT rotary switch S1 to any position between 0 and 6.
      Position 7 is reserved for use with the GUI (graphical user interface).
  2.  Connect 12V power to either the EMIVIN and GND banana jacks or to the
      J7 2.1mm (inside diameter) barrel jack.
  3.  Observe the red LED indicator light (D32). When it begins flashing,
      then the board is ready to start up.
  4.  When the red LED (D32) is flashing, push the Start button S3.
  5.  Observe the LED patterns and adjust the speed, brightness and pattern of
      the LEDs with the manual blue potentiometer R14.
  6.  Change the Pattern Select rotary switch to positions 0-6 to observe
      different patterns.
  7.  Press the Reset button S2 to start over again.



USER INPUT DATA FORMAT:
 decimal : 1024
 hex     : 0x400
 octal   : 02000  (leading 0 "zero")
 binary  : B10000000000
 float   : 1024.0

@endverbatim

http://www.linear.com/product/LT3965

http://www.linear.com/product/LT3965#demoboards

REVISION HISTORY
$Revision: 4641 $
$Date: 2016-01-29 14:38:11 -0800 (Fri, 29 Jan 2016) $

Copyright (c) 2013, Linear Technology Corp.(LTC)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of Linear Technology Corp.

The Linear Technology Linduino is not affiliated with the official Arduino team.
However, the Linduino is only possible because of the Arduino team's commitment
to the open-source community.  Please, visit http://www.arduino.cc and
http://store.arduino.cc , and consider a purchase that will help fund their
ongoing work.
*/

/*! @file
    @ingroup LT3965
*/


#include <stdio.h>
#include "Linduino.h"
#include "LT_I2C.h"
#include "LT3965.h"


//******************Function Declarations******************
//******************Display Pattern Functions******************
void fadewave();
void analog();
void sidebend();
void sidebendAuto();
void dazzle();
void pulse();
void wave();
void steeringRightAndLeft();
void sectionalBlanking();
void orbit();
void GraphicUI();
int readpot();
void setThresh();


uint8_t updown;
int lastread;
int currentread;


//Setup message bytes
int16_t An2, An1, An0;
uint8_t inputByte_0;
uint8_t inputByte_1;
uint8_t inputByte_2;
uint8_t inputByte_3;
uint8_t inputByte_4;

uint8_t input;
uint8_t fade;
int set = 0;
uint8_t values[4];

bool startsequence = FALSE;

uint8_t startup = 0;
long interval = 250;
long previousTime = 0;
uint8_t ledState;


// Setup the hardware I2C interface.
// LT3965_i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines.
void LT3965_i2c_enable()
{
  TWSR = (HARDWARE_I2C_PRESCALER_1 & 0x03);  //! 1) set the prescaler bits
  TWBR = 12;            //! 2) set the bit rate
}


//! Initialize Linduino
void setup()
{
  int8_t ack = 0;                                // I2C acknowledge bit

  pinMode(REDLED, OUTPUT);
  pinMode(LOADPIN, OUTPUT);
  pinMode(BUTTONPIN, INPUT);
  pinMode(PWMPIN, OUTPUT);
  pinMode(ALERTPIN, INPUT);

  digitalWrite(REDLED, HIGH);
  digitalWrite(PWMPIN, LOW);    // Pull pwm pin on LT3797 low to turn on with LEDs in OFF state
  digitalWrite(LOADPIN, HIGH);

  analogReference(EXTERNAL);                     // Set analog reference voltage to voltage sensed on AREF pin
  currentread = analogRead(A3);
  digitalWrite(PWMPIN, LOW);
  lastread = currentread;
  updown = DOWN;

  delay(50);

  An0 = analogRead(A0);               // Read 8 position switch pin 1
  An1 = analogRead(A1);               // Read 8 position switch pin 2
  An2 = analogRead(A2);               // Read 8 position switch pin 3

  if (!(An0 > 120 && An1 > 120 && An2 > 120))
  {
    int alertStatus = digitalRead(ALERTPIN);
    while (alertStatus)
    {
      alertStatus = digitalRead(ALERTPIN);
    }
  }

  LT3965_i2c_enable();                       // Enable the I2C port
  quikeval_I2C_connect();                    // Connect I2C to main data port
  delay(50);

  // Change Open-Circuit Threshold to 4.5V and turn all LEDs ON; ADDRESS1
  ack |= i2c_scwriteshort(address1, CHANNEL0, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL1, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL2, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL3, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL4, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL5, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL6, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL7, 0b0000);
  // Change Open-Circuit Threshold to 4.5V and turn all LEDs ON; ADDRESS2
  ack |= i2c_scwriteshort(address2, CHANNEL0, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL1, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL2, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL3, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL4, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL5, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL6, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL7, 0b0000);
  // Change Short-Circuit Threshold to 1V and turn all LEDs ON; ADDRESS1
  ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, 1);
  // Change Short-Circuit Threshold to 1V and turn all LEDs ON; ADDRESS2
  ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, 1);

  delay(50);

  if (!(An0 > 120 && An1 > 120 && An2 > 120))
  {
    // require a push-button press before doing anything. LED will blink until button is pressed, then stay illuminated.
    while (digitalRead(BUTTONPIN))
    {
      unsigned long currentTime = millis();
      if (currentTime - previousTime > interval)
      {
        previousTime = currentTime;
        ledState = ! ledState;
        digitalWrite(REDLED, ledState);
      }
    }
  }
  digitalWrite(REDLED, LOW);

  An0 = analogRead(A0);           // Read 8 position switch pin 1
  An1 = analogRead(A1);                 // Read 8 position switch pin 2
  An2 = analogRead(A2);                 // Read 8 position switch pin 3
}

//! Repeats Linduino loop
void loop()
{
  int8_t ack = 0;                           // I2C acknowledge bit
  int switch_pos;                         // New switch possition
  int prev_switch_pos;            // Previous swicth position

  // require two consecutive switch readings, 50ms apart, before choosing a pattern.
  do
  {
    An0 = analogRead(A0);           // Read 8 position switch pin 1
    An1 = analogRead(A1);             // Read 8 position switch pin 2
    An2 = analogRead(A2);             // Read 8 position switch pin 3

    prev_switch_pos = switch_pos;

    //Interpret 3 bits from 8 position switch as decimal integer
    if (An0 > 120 && An1 < 120 && An2 < 120)
      switch_pos = 1;
    else if (An0 < 120 && An1 > 120 && An2 < 120)
      switch_pos = 2;
    else if (An0 > 120 && An1 > 120 && An2 < 120)
      switch_pos = 3;
    else if (An0 < 120 && An1 < 120 && An2 > 120)
      switch_pos = 4;
    else if (An0 > 120 && An1 < 120 && An2 > 120)
      switch_pos = 5;
    else if (An0 < 120 && An1 > 120 && An2 > 120)
      switch_pos = 6;
    else if (An0 > 120 && An1 > 120 && An2 > 120)
      switch_pos = 7;
    else
      switch_pos = 0;

    delay(50);
  }
  while (prev_switch_pos != switch_pos);

  // Call function associated with corresponding switch position value
  switch (switch_pos)
  {
    case 0:
      orbit();
      break;
    case 1:
      wave();  // Select a DAC to update
      break;
    case 2:
      steeringRightAndLeft();  // Select a DAC to update
      break;
    case 3:
      sectionalBlanking();  // Select a DAC to update
      break;
    case 4:
      sidebend();  // Select a DAC to update
      break;
    case 5:
      analog();  // Select a DAC to update
      break;
    case 6:
      fadewave();  // Select a DAC to update
      break;
    case 7:
      GraphicUI();  // Select a DAC to update
      break;
  }
}

//! Function to write I2C bytes which are received from GUI.
int8_t write_bytes(uint8_t size, uint8_t byte1, uint8_t byte2, uint8_t byte3)
{
  int8_t ret = 0 ;
  uint8_t data[3];
  if (i2c_start() != 0)                               //I2C START
    return (1);                                      //Stop and return 0 if START fail

  if (size == 2)
  {
    ret |= i2c_write(byte1);
    ret |= i2c_write(byte2);
  }
  else if (size == 3)
  {
    ret |= i2c_write(byte1);
    ret |= i2c_write(byte2);
    ret |= i2c_write(byte3);
  }
  else if (size == 1)
  {
    ret |= i2c_write(byte1);
  }

  i2c_stop();                                        //I2C STOP
  if (ret != 0)                                       //Returns 1 if failed
    return (1);
  return (0);                                        //Returns 0 if success
}

//! Function to read I2C bytes depending on the bytes received from GUI.
int8_t read_bytes(uint8_t size, uint8_t byte1, uint8_t byte2, uint8_t byte3)
{
  int8_t ret = 0 ;
  if (size == 250 && byte1 == 1 && byte2 == 1 && byte3 == 1)  // ALERT - ACMODE READ
  {
    uint8_t fault;
    fault = 1;
    fault = digitalRead(2);     // digital pin2 is connected to ALERT/
    if (!fault) // fault condition
      Serial.write(0);
    else
      Serial.write(1);
  }
  else
  {
    uint8_t x;
    if (i2c_start() != 0)                               //I2C START
      return (1);                                      //Stop and return 0 if START fail

    // write byte1 and read size number of bytes
    ret |= i2c_write(byte1);
    if (ret == 0)                                       // If response = ACK (slave responded)
    {
      for (x = 0; x < (size - 1); ++x)
      {
        Serial.write(i2c_read(WITH_ACK));
      }
      Serial.write(i2c_read(WITH_NACK));
    }
    else                        // If response = NACK (no slave responded
    {
      for (x = 0; x < size; ++x)
      {
        Serial.write(0);                // The GUI is waiting to read back bytes, sending dummy bytes
      }
    }
    i2c_stop();                                       //I2C STOP
  }
  if (ret != 0)                                     //Returns 1 if failed
    return (1);
  return (0);                                       //Returns 0 if success
}

//! Function to talk to GUI.
void GraphicUI()      // DO NOT EDIT THIS FUNCTION
{

  int8_t ack = 0;       // I2C acknowledge bit
  uint8_t data_SCMREG;
  uint8_t brightness;
  uint8_t channel;
  uint8_t x = 0;
  int toggle = 255;
  uint8_t values[3];
  uint8_t ARA;
  char read_id;
  setThresh();
  Serial.begin(115200);                    // Initialize the serial port to the PC at baud rate of 115200 bps

  An0 = analogRead(A0);
  An1 = analogRead(A1);
  An2 = analogRead(A2);

  while (An0 > 120 && An1 > 120 && An2 > 120)
  {
    if (Serial.available() > 0)
    {
      inputByte_0 = Serial.read();
      delay(10);

      if (inputByte_0 == 'i')   // Making Quikeval compatible
      {
        Serial.print("USBSPI,PIC,01,01,DC,DC590,----------------------\r\n");
      }
      else if (inputByte_0 == 'I')    // Making Quikeval compatible
      {
        Serial.print("LT3965,Cls,D3965,01,01,DC,DC2218A,--------------\r\n");
      }
      else if (inputByte_0 == 16)
      {
        inputByte_1 = Serial.read();
        delay(10);
        inputByte_2 = Serial.read();
        delay(10);
        inputByte_3 = Serial.read();
        delay(10);
        inputByte_4 = Serial.read();
        delay(10);

        //Detect Command type
        if (inputByte_1 == 128)
        {
          Serial.print("HELLO FROM ARDUINO");

          ack |= i2c_scwriteshort(address1, CHANNEL0, 0b0000);
          ack |= i2c_scwriteshort(address1, CHANNEL1, 0b0000);
          ack |= i2c_scwriteshort(address1, CHANNEL2, 0b0000);
          ack |= i2c_scwriteshort(address1, CHANNEL3, 0b0000);
          ack |= i2c_scwriteshort(address1, CHANNEL4, 0b0000);
          ack |= i2c_scwriteshort(address1, CHANNEL5, 0b0000);
          ack |= i2c_scwriteshort(address1, CHANNEL6, 0b0000);
          ack |= i2c_scwriteshort(address1, CHANNEL7, 0b0000);

          ack |= i2c_scwriteshort(address2, CHANNEL0, 0b0000);
          ack |= i2c_scwriteshort(address2, CHANNEL1, 0b0000);
          ack |= i2c_scwriteshort(address2, CHANNEL2, 0b0000);
          ack |= i2c_scwriteshort(address2, CHANNEL3, 0b0000);
          ack |= i2c_scwriteshort(address2, CHANNEL4, 0b0000);
          ack |= i2c_scwriteshort(address2, CHANNEL5, 0b0000);
          ack |= i2c_scwriteshort(address2, CHANNEL6, 0b0000);
          ack |= i2c_scwriteshort(address2, CHANNEL7, 0b0000);

          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, 0);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, 0);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, 0);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, 0);
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, 0);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, 0);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, 0);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, 0);

          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, 0);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, 0);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, 0);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, 0);
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, 0);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, 0);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, 0);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, 0);

          ack |= i2c_acwrite(address1, 0b00000000);
          ack |= i2c_acwrite(address2, 0b00000000);

          startsequence = TRUE;
          delay(50);
          digitalWrite(PWMPIN, HIGH);
          delay(50);
          digitalWrite(LOADPIN, LOW);
          delay(10);
        }
        else
        {
          write_bytes(inputByte_1, inputByte_2, inputByte_3, inputByte_4);
        }

      }
      else if (inputByte_0 == 17)
      {
        inputByte_1 = Serial.read();
        delay(10);
        inputByte_2 = Serial.read();
        delay(10);
        inputByte_3 = Serial.read();
        delay(10);
        inputByte_4 = Serial.read();
        delay(10);

        read_bytes(inputByte_1, inputByte_2, inputByte_3, inputByte_4);
      }
    }
    An0 = analogRead(A0);
    An1 = analogRead(A1);
    An2 = analogRead(A2);
  }
}

//! Func Desc: All LEDs illuminated to the same brightness level depending on potentiometer value.
void analogAuto()
{
  setThresh();

  int8_t ack = 0;                               // I2C acknowledge bit
  uint8_t i;
  int j;
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3
  int analog_in = readpot();          // Read value of potentiometer

  while (An0 > 120 && An1 < 120 && An2 > 120)
  {
    if (!startsequence)
    {
      ack |= i2c_acwrite(address1, 0);
      ack |= i2c_acwrite(address2, 0);
      delay(10);
      digitalWrite(PWMPIN, HIGH);
      delay(10);
      startsequence = TRUE;
    }

    for (j = 0; j <= 511; j++)
    {
      An0 = analogRead(A0);
      An1 = analogRead(A1);
      An2 = analogRead(A2);
      analog_in = readpot();
      if (j > 255)
        i = 511 - j;
      else
        i = j;


      if (i < 1)
      {
        ack |= i2c_acwrite(address1, 0b00000000);
        ack |= i2c_acwrite(address2, 0b00000000);
      }
      else if (i > 254)
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0001, i);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0001, i);
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0001, i);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0001, i);
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0001, i);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0001, i);
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0001, i);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0001, i);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0001, i);
      }
      else
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, i);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, i);
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, i);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, i);
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, i);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, i);
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, i);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, i);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, i);
      }
      delay(5);
      if (i < 1)
        delay(500);
      if (i == 255)
        delay(500);
    }
  }
}

//! All sixteen LEDs adjust PWM dimming duty cycle and brightness based upon
//! the position of the steering wheel (interactive potentiometer). This pattern
//! represents a light that bends around the side of a car, projecting a
//! turning headlight pattern that is linked to the steering wheel.
void sidebendAuto()
{
  setThresh();

  int8_t ack = 0;       // I2C acknowledge bit
  int ch[8];
  int i;
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3
  int analog_in;


  while (An0 < 120 && An1 < 120 && An2 > 120)
  {
    if (!startsequence)
    {
      ack |= i2c_acwrite(address1, 0);
      ack |= i2c_acwrite(address2, 0);
      delay(10);
      digitalWrite(PWMPIN, HIGH);
      delay(10);
      startsequence = TRUE;
    }

    for (int a = 0; a < 3; a++)
    {
      An0 = analogRead(A0);
      An1 = analogRead(A1);
      An2 = analogRead(A2);
      int increment = 5;
      int analogStart = 400;
      int analogMax = 530;

      if (a == 2)
      {
        analogMax = 650;
        increment = 8;
      }

      delay(500);

      for (analog_in = analogStart; analog_in > analogStart - 1; analog_in += increment)
      {

        delay(25);
        An0 = analogRead(A0);
        An1 = analogRead(A1);
        An2 = analogRead(A2);
        ch[0] = (analog_in) - 425;
        ch[1] = (analog_in) - 450;
        ch[2] = (analog_in) - 475;
        ch[3] = (analog_in) - 500;
        ch[4] = (analog_in) - 525;
        ch[5] = (analog_in) - 550;
        ch[6] = (analog_in) - 575;
        ch[7] = (analog_in) - 600;

        for (i = 0; i < 8; i++)
        {
          if (ch[i] < 0)
            ch[i] = 0;
          else if (ch[i] > 255)
            ch[i] = 255;
          An0 = analogRead(A0);
          An1 = analogRead(A1);
          An2 = analogRead(A2);

        }

        if (ch[0] == 0)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[0]);
        }
        else if (ch[0] == 255)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0001, ch[0]);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0001, ch[0]);
        }
        else
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[0]);
        }

        if (ch[1] == 0)
        {
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[1]);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
        }
        else if (ch[1] == 255)
        {
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0001, ch[1]);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0001, ch[1]);
        }
        else
        {
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[1]);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);
        }

        if (ch[2] == 0)
        {
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[2]);
        }
        else if (ch[2] == 255)
        {
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0001, ch[2]);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0001, ch[2]);
        }
        else
        {
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[2]);
        }

        if (ch[3] == 0)
        {
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[3]);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
        }
        else if (ch[3] == 255)
        {
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0001, ch[3]);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0001, ch[3]);
        }
        else
        {
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[3]);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);
        }

        if (ch[4] == 0)
        {
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[4]);
        }
        else if (ch[4] == 255)
        {
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0001, ch[4]);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0001, ch[4]);
        }
        else
        {
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[4]);
        }

        if (ch[5] == 0)
        {
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[5]);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
        }
        else if (ch[5] == 255)
        {
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0001, ch[5]);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0001, ch[5]);
        }
        else
        {
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[5]);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);
        }

        if (ch[6] == 0)
        {
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[6]);
        }
        else if (ch[6] == 255)
        {
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0001, ch[6]);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0001, ch[6]);
        }
        else
        {
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[6]);
        }

        if (ch[7] == 0)
        {
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[7]);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
        }
        else if (ch[7] == 255)
        {
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0001, ch[7]);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0001, ch[7]);
        }
        else
        {
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[7]);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);
        }
        An0 = analogRead(A0);
        An1 = analogRead(A1);
        An2 = analogRead(A2);
        if (analog_in >= analogMax)
        {
          delay(250);
          increment *= -1;
        }
      }
    }
  }
}

//! Consecutive LEDs alternate fading up and down. Speed of fading controlled by
//! the position of the potentiometer.
void dazzle()
{
  setThresh();

  int8_t ack = 0;       // I2C acknowledge bit
  int8_t bright = -127;
  uint8_t brightness;
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3
  int analog_in = analogRead(A3);         // Read value of potentiometer

  while ( An0 > 120 && An1 < 120 && An2 < 120)
  {
    An0 = analogRead(A0);     // Read 8 position switch pin 1
    An1 = analogRead(A1);     // Read 8 position switch pin 2
    An2 = analogRead(A2);     // Read 8 position switch pin 3
    analog_in = analogRead(A3);           // Read value of potentiometer

    brightness = abs(bright) * 2;

    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, brightness);     // Single channel write command to control a single LED
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, brightness);
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, brightness);
    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, brightness);
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, brightness);
    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, brightness);
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, brightness);
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, brightness);

    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, 255 - brightness);
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, 255 - brightness);
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, 255 - brightness);
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, 255 - brightness);
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, 255 - brightness);
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, 255 - brightness);
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, 255 - brightness);
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, 255 - brightness);

    delay(analog_in / 20);

    bright = bright + 2;
    if (bright >= 127)
      bright = -127;
  }
}

//! Center headlight LEDs stay on to face directly forward while the right and
//! left side LEDs turn on with brightness proportional to the amount that the
//! steering wheel (interactive potentiometer) is turned.
void steeringRightAndLeft()
{
  setThresh();

  int8_t ack = 0;             // I2C acknowledge bit
  int ch[6];                        // only need 8 channels - four right and four left
  int i;
  uint8_t j = 0;
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3
  int analog_in = analogRead(A3);         // Read value of potentiometer



  while (An0 < 120 && An1 > 120 && An2 < 120)
  {
    An0 = analogRead(A0);           // Read 8 position switch pin 1
    An1 = analogRead(A1);     // Read 8 position switch pin 2
    An2 = analogRead(A2);     // Read 8 position switch pin 3
    analog_in = analogRead(A3);           // Read value of potentiometer

    ch[0] = 250 - (analog_in);
    ch[1] = 325 - (analog_in);
    ch[2] = 400 - (analog_in);
    ch[3] = (analog_in) - 650;
    ch[4] = (analog_in) - 725;
    ch[5] = (analog_in) - 800;

    // calculate right and left bending lights update
    for (i = 0; i < 6; i++)
    {
      if (ch[i] < 0)
        ch[i] = 0;
      else if (ch[i] > 255)
        ch[i] = 255;
    }

    if (!startsequence)
    {
      ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, 1);
      ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, 1);
      ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, 1);
      ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, 1);
      ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, 1);
      ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, 1);
      ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, 1);
      ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, 1);
      ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, 1);

      delay(500);
      digitalWrite(PWMPIN, HIGH);
      delay(10);
      digitalWrite(LOADPIN, LOW);
      delay(10);

      while (j < 255)
      {
        if (ch[0] >= j)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[0]);
        }
        if (ch[1] >= j)
        {
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[1]);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);
        }
        if (ch[2] >= j)
        {
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[2]);
        }

        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);

        if (ch[3] >= j)
        {
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[3]);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[3]);
        }
        if (ch[4] >= j)
        {
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[4]);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[4]);
        }
        if (ch[5] >= j)
        {
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[5]);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[5]);
        }
        j++;
        delay(8);
      }
      // delay(2500);
      startsequence = TRUE;
    }

    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0001, 255);
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0001, 255);
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0001, 255);
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0001, 255);

    // write to right and left bending lights update
    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);
    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[0]);
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[1]);
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[2]);

    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[3]);
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[3]);
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[4]);
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[4]);
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[5]);
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[5]);
  }
}


//! All LEDs are illluminated to full brightness simulating a car
//! with its high-beams turned on. Two LEDs will turn off creating
//! a blank zone. This pattern shows how segments of a high-beam
//! LED cluster can be turned off to avoid blinding oncomming traffic.
void sectionalBlanking()
{
  setThresh();

  int8_t ack = 0;             // I2C acknowledge bit
  uint8_t j = 0;
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3
  int analog_in = analogRead(A3);         // Read value of potentiometer

  if (!startsequence)         // Checks if start-up sequence has already been run to fade LEDs on
  {
    while (j < 255)
    {
      if (j < 1)
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, j);

        digitalWrite(PWMPIN, HIGH);
        delay(10);
        digitalWrite(LOADPIN, LOW);
        delay(10);
      }
      else if (j >= 254)
      {
        if (analog_in <= 146)
        {
          // Swtich OFF LED5 and LED8 of BOARD1
          ack |= i2c_acwrite(address1, 0b11110110);
          ack |= i2c_acwrite(address2, 0b11111111);
        }
        else if (analog_in > 146 && analog_in <= 292)
        {
          // Swtich OFF LED5 and LED4 of BOARD1
          ack |= i2c_acwrite(address1, 0b11100111);
          ack |= i2c_acwrite(address2, 0b11111111);
        }
        else if (analog_in > 292 && analog_in <= 438)
        {
          // Swtich OFF LED4 and LED1 of BOARD1
          ack |= i2c_acwrite(address1, 0b01101111);
          ack |= i2c_acwrite(address2, 0b11111111);
        }
        else if (analog_in > 438 && analog_in <= 584)
        {
          // Swtich OFF LED1 of BOARD1 and LED8 of BOARD2
          ack |= i2c_acwrite(address1, 0b01111111);
          ack |= i2c_acwrite(address2, 0b11111110);
        }
        else if (analog_in > 584 && analog_in <= 730)
        {
          // Swtich OFF LED8 and LED5 of BOARD2
          ack |= i2c_acwrite(address1, 0b11111111);
          ack |= i2c_acwrite(address2, 0b11110110);
        }
        else if (analog_in > 730 && analog_in <= 876)
        {
          // Swtich OFF LED5 and LED4 of BOARD2
          ack |= i2c_acwrite(address1, 0b11111111);
          ack |= i2c_acwrite(address2, 0b11100111);
        }
        else
        {
          // Swtich OFF LED4 and LED1 of BOARD2
          ack |= i2c_acwrite(address1, 0b11111111);
          ack |= i2c_acwrite(address2, 0b01101111);
        }
        startsequence = TRUE;
      }
      else
      {
        if (analog_in <= 146)
        {
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        }
        else if (analog_in > 146 && analog_in <= 292)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        }
        else if (analog_in > 292 && analog_in <= 438)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        }
        else if (analog_in > 438 && analog_in <= 584)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        }
        else if (analog_in > 584 && analog_in <= 730)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        }
        else if (analog_in > 730 && analog_in <= 876)
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        }
        else
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
        }
      }
      delay(2);
      j++;
    }
  }

  while (An0 > 120 && An1 > 120 && An2 < 120)
  {
    An0 = analogRead(A0);       // Read 8 position switch pin 1
    An1 = analogRead(A1);       // Read 8 position switch pin 2
    An2 = analogRead(A2);       // Read 8 position switch pin 3
    analog_in = analogRead(A3);

    if (analog_in <= 146)
    {
      // Swtich OFF LED5 and LED8 of BOARD1
      ack |= i2c_acwrite(address1, 0b11110110);
      ack |= i2c_acwrite(address2, 0b11111111);
    }
    else if (analog_in > 146 && analog_in <= 292)
    {
      // Swtich OFF LED5 and LED4 of BOARD1
      ack |= i2c_acwrite(address1, 0b11100111);
      ack |= i2c_acwrite(address2, 0b11111111);
    }
    else if (analog_in > 292 && analog_in <= 438)
    {
      // Swtich OFF LED4 and LED1 of BOARD1
      ack |= i2c_acwrite(address1, 0b01101111);
      ack |= i2c_acwrite(address2, 0b11111111);
    }
    else if (analog_in > 438 && analog_in <= 584)
    {
      // Swtich OFF LED1 of BOARD1 and LED8 of BOARD2
      ack |= i2c_acwrite(address1, 0b01111111);
      ack |= i2c_acwrite(address2, 0b11111110);
    }
    else if (analog_in > 584 && analog_in <= 730)
    {
      // Swtich OFF LED8 and LED5 of BOARD2
      ack |= i2c_acwrite(address1, 0b11111111);
      ack |= i2c_acwrite(address2, 0b11110110);
    }
    else if (analog_in > 730 && analog_in <= 876)
    {
      // Swtich OFF LED5 and LED4 of BOARD2
      ack |= i2c_acwrite(address1, 0b11111111);
      ack |= i2c_acwrite(address2, 0b11100111);
    }
    else
    {
      // Swtich OFF LED4 and LED1 of BOARD2
      ack |= i2c_acwrite(address1, 0b11111111);
      ack |= i2c_acwrite(address2, 0b01101111);
    }
  }
}

//! Func Desc: Varying number of brightened LEDs depending on pot
void sidebend()
{
  setThresh();

  int8_t ack = 0;       // I2C acknowledge bit
  int ch[8];
  int i;
  uint8_t j = 0;
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3
  int analog_in = readpot();          // Read value of potentiometer

  while (An0 < 120 && An1 < 120 && An2 > 120)
  {
    An0 = analogRead(A0);
    An1 = analogRead(A1);
    An2 = analogRead(A2);

    analog_in = 1023 - readpot();

    ch[0] = (analog_in) - 425;
    ch[1] = (analog_in) - 450;
    ch[2] = (analog_in) - 475;
    ch[3] = (analog_in) - 500;
    ch[4] = (analog_in) - 525;
    ch[5] = (analog_in) - 550;
    ch[6] = (analog_in) - 575;
    ch[7] = (analog_in) - 600;

    for (i = 0; i < 8; i++)
    {
      if (ch[i] < 0)
        ch[i] = 0;
      else if (ch[i] > 255)
        ch[i] = 255;
    }

    if (!startsequence)
    {
      if (ch[0] <= 0)
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, 0);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, 1);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, 1);
      }
      if (ch[1] <= 0)
      {
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, 0);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, 1);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, 1);
      }
      if (ch[2] <= 0)
      {
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, 0);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, 1);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, 1);
      }
      if (ch[3] <= 0)
      {
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, 0);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, 1);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, 1);
      }
      if (ch[4] <= 0)
      {
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, 0);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, 1);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, 1);
      }
      if (ch[5] <= 0)
      {
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, 0);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, 1);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, 1);
      }
      if (ch[6] <= 0)
      {
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, 0);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, 1);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, 1);
      }
      if (ch[7] <= 0)
      {
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, 0);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, 0);
      }
      else
      {
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, 1);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, 1);
      }

      delay(500);
      digitalWrite(PWMPIN, HIGH);
      delay(10);
      digitalWrite(LOADPIN, LOW);
      delay(10);

      while (j < 255)
      {
        if ((ch[0] >= j) && (ch[0] > 0))
        {
          ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
        }
        if ((ch[1] >= j) && (ch[1] > 0))
        {
          ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, j);
        }
        if ((ch[2] >= j) && (ch[2] > 0))
        {
          ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
        }
        if ((ch[3] >= j) && (ch[3] > 0))
        {
          ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
        }
        if ((ch[4] >= j) && (ch[4] > 0))
        {
          ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
        }
        if ((ch[5] >= j) && (ch[5] > 0))
        {
          ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, j);
        }
        if ((ch[6] >= j) && (ch[6] > 0))
        {
          ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
        }
        if ((ch[7] >= j) && (ch[7] > 0))
        {
          ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
          ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        }
        j++;
        delay(8);
      }
      startsequence = TRUE;
    }

    if (ch[0] == 0)
    {
      ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
      ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[0]);
    }
    else if (ch[0] == 255)
    {
      ack |= i2c_scwritelong(address1, CHANNEL0, 0b0001, ch[0]);
      ack |= i2c_scwritelong(address1, CHANNEL1, 0b0001, ch[0]);
    }
    else
    {
      ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);
      ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[0]);
    }

    if (ch[1] == 0)
    {
      ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[1]);
      ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
    }
    else if (ch[1] == 255)
    {
      ack |= i2c_scwritelong(address1, CHANNEL2, 0b0001, ch[1]);
      ack |= i2c_scwritelong(address1, CHANNEL3, 0b0001, ch[1]);
    }
    else
    {
      ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[1]);
      ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);
    }

    if (ch[2] == 0)
    {
      ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
      ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[2]);
    }
    else if (ch[2] == 255)
    {
      ack |= i2c_scwritelong(address1, CHANNEL4, 0b0001, ch[2]);
      ack |= i2c_scwritelong(address1, CHANNEL5, 0b0001, ch[2]);
    }
    else
    {
      ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);
      ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[2]);
    }

    if (ch[3] == 0)
    {
      ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[3]);
      ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
    }
    else if (ch[3] == 255)
    {
      ack |= i2c_scwritelong(address1, CHANNEL6, 0b0001, ch[3]);
      ack |= i2c_scwritelong(address1, CHANNEL7, 0b0001, ch[3]);
    }
    else
    {
      ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[3]);
      ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);
    }

    if (ch[4] == 0)
    {
      ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
      ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[4]);
    }
    else if (ch[4] == 255)
    {
      ack |= i2c_scwritelong(address2, CHANNEL0, 0b0001, ch[4]);
      ack |= i2c_scwritelong(address2, CHANNEL1, 0b0001, ch[4]);
    }
    else
    {
      ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);
      ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[4]);
    }

    if (ch[5] == 0)
    {
      ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[5]);
      ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
    }
    else if (ch[5] == 255)
    {
      ack |= i2c_scwritelong(address2, CHANNEL2, 0b0001, ch[5]);
      ack |= i2c_scwritelong(address2, CHANNEL3, 0b0001, ch[5]);
    }
    else
    {
      ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[5]);
      ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);
    }

    if (ch[6] == 0)
    {
      ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
      ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[6]);
    }
    else if (ch[6] == 255)
    {
      ack |= i2c_scwritelong(address2, CHANNEL4, 0b0001, ch[6]);
      ack |= i2c_scwritelong(address2, CHANNEL5, 0b0001, ch[6]);
    }
    else
    {
      ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);
      ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[6]);
    }

    if (ch[7] == 0)
    {
      ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[7]);
      ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
    }
    else if (ch[7] == 255)
    {
      ack |= i2c_scwritelong(address2, CHANNEL6, 0b0001, ch[7]);
      ack |= i2c_scwritelong(address2, CHANNEL7, 0b0001, ch[7]);
    }
    else
    {
      ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[7]);
      ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);
    }
  }
}

//! Func Desc: All LEDs lighted up to the same level depending on pot value.
void analog()
{
  setThresh();

  int8_t ack = 0;                               // I2C acknowledge bit
  uint8_t i;
  uint8_t j = 0;
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3
  int analog_in = readpot();          // Read value of potentiometer

  if (!startsequence)         // Checks if start-up sequence has already been run to fade LEDs on
  {
    analog_in = 1023 - readpot();
    i = analog_in / 4;

    while (j <= i)
    {
      if (j < 1)
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, j);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, j);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, j);
        digitalWrite(PWMPIN, HIGH);
        delay(10);
        digitalWrite(LOADPIN, LOW);
        delay(10);
      }
      else if (j > 254)
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0001, j);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0001, j);
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0001, j);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0001, j);
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0001, j);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0001, j);
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0001, j);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0001, j);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0001, j);
        startsequence = TRUE;
        break;
      }
      else
      {
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, j);
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, j);
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, j);
        if (j == i)
        {
          startsequence = TRUE;
          break;
        }
      }
      delay(2);
      j++;
    }
  }

  while (An0 > 120 && An1 < 120 && An2 > 120)
  {
    An0 = analogRead(A0);
    An1 = analogRead(A1);
    An2 = analogRead(A2);
    analog_in = 1023 - readpot();
    i = analog_in / 4;
    //    i = 128;

    if (i < 1)
    {
      ack |= i2c_acwrite(address1, 0b00000000);
      ack |= i2c_acwrite(address2, 0b00000000);
    }
    else if (i > 254)
    {
      ack |= i2c_scwritelong(address1, CHANNEL0, 0b0001, i);
      ack |= i2c_scwritelong(address1, CHANNEL1, 0b0001, i);
      ack |= i2c_scwritelong(address1, CHANNEL2, 0b0001, i);
      ack |= i2c_scwritelong(address1, CHANNEL3, 0b0001, i);
      ack |= i2c_scwritelong(address1, CHANNEL4, 0b0001, i);
      ack |= i2c_scwritelong(address1, CHANNEL5, 0b0001, i);
      ack |= i2c_scwritelong(address1, CHANNEL6, 0b0001, i);
      ack |= i2c_scwritelong(address1, CHANNEL7, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL0, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL1, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL2, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL3, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL4, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL5, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL6, 0b0001, i);
      ack |= i2c_scwritelong(address2, CHANNEL7, 0b0001, i);
    }
    else
    {
      ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, i);
      ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, i);
      ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, i);
      ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, i);
      ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, i);
      ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, i);
      ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, i);
      ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, i);
      ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, i);
    }
  }
}


//! LEDs illuminate around the outside of the board, giving the effect of
//! a comet with a tail traveling around the board. The traveling speed of
//! the comet is controlled by the posititon of the potentiometer. The
//! pattern will continue running after the pattern select switch is changed
//! from this position until the orbit() function has completed its pattern.
void orbit()
{
  setThresh();

  int8_t ack = 0;       // I2C acknowledge bit
  uint8_t i, j, k, l;
  int up_down = 1; //1 = up, 0 = down, 2 = startup wait, 3 = wait
  int delay_var = 5;
  uint8_t ch[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ;  //channel count variables
  int ud[16] = {1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};  //count up or down variables; 1 = up, 0 = down, 2 = wait
  int waitcount[16]; // wait count for unusued LEDs
  int analog_in = analogRead(A3);         // Read value of potentiometer
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3

  waitcount[0] = 0;
  waitcount[1] = 15;
  waitcount[2] = 14;
  waitcount[3] = 13;
  waitcount[4] = 12;
  waitcount[5] = 11;
  waitcount[6] = 10;
  waitcount[7] = 9;
  waitcount[8] = 8;
  waitcount[9] = 7;
  waitcount[10] = 6;
  waitcount[11] = 5;
  waitcount[12] = 4;
  waitcount[13] = 3;
  waitcount[14] = 2;
  waitcount[15] = 1;

  // start with this brightness
  ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, 0);

  ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, 0);


  delay(50);
  if (!startsequence)
  {
    digitalWrite(PWMPIN, HIGH);
    delay(50);
    digitalWrite(LOADPIN, LOW);
    delay(10);
    startsequence = TRUE;
  }
  delay(500);

  for (j = 0; j < 64; j++)
  {
    for (i = 0; i <= 9 ; i++)
    {
      for (k = 0; k <= 15; k++)
      {
        if (ud[k] == 1)
          ch[k] = ch[k] + 25;
        else if (ud[k] == 0)
          ch[k] = ch[k] - 5;
        else
          ch[k] = ch[k];
      }

      if (ch[0] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

      if (ch[1] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

      if (ch[2] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

      if (ch[3] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);

      if (ch[12] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

      if (ch[13] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

      if (ch[14] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

      if (ch[15] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

      if (ch[8] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

      if (ch[9] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

      if (ch[10] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

      if (ch[11] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

      if (ch[4] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

      if (ch[5] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

      if (ch[6] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

      if (ch[7] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);


      analog_in = analogRead(A3);
      delay (analog_in / 24);
    }

    // check to see if up, down, or wait and increment or change
    for (l = 0; l <= 15; l++)
    {
      if (ud[l] == 1 && ch[l] >= 250)
      {
        ud[l] = 0;
        waitcount[l]++;
      }
      else if (ud[l] == 2)
      {
        if (waitcount[l] >= 15)
        {
          ud[l] = 1;
          waitcount[l] = 0;
        }
        else
          waitcount[l]++;
      }
      else if (ud[l] == 0 && ch[l] <= 5)
      {
        ud[l] = 2;
        waitcount[l]++;
      }
      else
      {
        ud[l] = ud[l];
        waitcount[l]++;
      }
    }
  }
  // run to finish then pause
  ud[0] = 2;
  ch[0] = 0;
  waitcount[0] = 1;
  for (j = 0; j < 6; j++)
  {
    for (i = 0; i <= 9 ; i++)
    {
      for (k = 0; k <= 15; k++)
      {
        if (ud[k] == 1)
          ch[k] = ch[k] + 25;
        else if (ud[k] == 0)
          ch[k] = ch[k] - 5;
        else
          ch[k] = ch[k];
      }

      if (ch[0] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

      if (ch[1] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

      if (ch[2] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

      if (ch[3] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);

      if (ch[12] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

      if (ch[13] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

      if (ch[14] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

      if (ch[15] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

      if (ch[8] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

      if (ch[9] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

      if (ch[10] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

      if (ch[11] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

      if (ch[4] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

      if (ch[5] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

      if (ch[6] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

      if (ch[7] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);

      analog_in = analogRead(A3);
      delay (analog_in / 24);
    }

    // check to see if up, down, or wait and increment or change
    for (l = 0; l <= 15; l++)
    {
      if (ud[l] == 1 && ch[l] >= 250)
      {
        ud[l] = 0;
        waitcount[l]++;
      }
      else if (ud[l] == 2)
      {
        if (waitcount[l] >= 15)
        {
          ud[l] = 2;
          waitcount[l] = 0;
        }
        else
          waitcount[l]++;
      }
      else if (ud[l] == 0 && ch[l] <= 5)
      {
        ud[l] = 2;
        waitcount[l]++;
      }
      else
      {
        ud[l] = ud[l];
        waitcount[l]++;
      }
    }
  }
  // restart in the other direction
  for (i = 0; i < 16; ++i)
    ch[i] = 0;


  for (i = 0; i < 15; ++i)
    ud[i] = 2;
  ud[i] = 1;

  waitcount[15] = 0;
  waitcount[14] = 15;
  waitcount[13] = 14;
  waitcount[12] = 13;
  waitcount[11] = 12;
  waitcount[10] = 11;
  waitcount[9] = 10;
  waitcount[8] = 9;
  waitcount[7] = 8;
  waitcount[6] = 7;
  waitcount[5] = 6;
  waitcount[4] = 5;
  waitcount[3] = 4;
  waitcount[2] = 3;
  waitcount[1] = 2;
  waitcount[0] = 1;

  // restart with this brightness for the other direction
  ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, 0);

  ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, 0);

  delay(500);
  for (j = 0; j < 64; j++)
  {
    for (i = 0; i <= 9 ; i++)
    {
      for (k = 0; k <= 15; k++)
      {
        if (ud[k] == 1)
          ch[k] = ch[k] + 25;
        else if (ud[k] == 0)
          ch[k] = ch[k] - 5;
        else ch[k] = ch[k];
      }

      if (ch[0] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

      if (ch[1] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

      if (ch[2] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

      if (ch[3] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);

      if (ch[12] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

      if (ch[13] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

      if (ch[14] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

      if (ch[15] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

      if (ch[8] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

      if (ch[9] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

      if (ch[10] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

      if (ch[11] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

      if (ch[4] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

      if (ch[5] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

      if (ch[6] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

      if (ch[7] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);

      analog_in = analogRead(A3);
      delay (analog_in / 24);
    }

    // check to see if up, down, or wait and increment or change
    for (l = 0; l <= 15; l++)
    {
      /* analog_in = analogRead(A3); */
      if (ud[l] == 1 && ch[l] >= 250)
      {
        ud[l] = 0;
        waitcount[l]++;
      }
      else if (ud[l] == 2)
      {
        if (waitcount[l] >= 15)
        {
          ud[l] = 1;
          waitcount[l] = 0;
        }
        else
          waitcount[l]++;
      }
      else if (ud[l] == 0 && ch[l] <= 5)
      {
        ud[l] = 2;
        waitcount[l]++;
      }
      else
      {
        ud[l] = ud[l];
        waitcount[l]++;
      }
    }
  }
  // run to finish then pause
  ud[15] = 2;
  ch[15] = 0;
  waitcount[15] = 1;
  for (j = 0; j < 6; j++)
  {
    for (i = 0; i <= 9 ; i++)
    {
      for (k = 0; k <= 15; k++)
      {
        if (ud[k] == 1)
          ch[k] = ch[k] + 25;
        else if (ud[k] == 0)
          ch[k] = ch[k] - 5;
        else
          ch[k] = ch[k];
      }

      if (ch[0] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

      if (ch[1] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

      if (ch[2] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

      if (ch[3] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);

      if (ch[12] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

      if (ch[13] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

      if (ch[14] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

      if (ch[15] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

      if (ch[8] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

      if (ch[9] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

      if (ch[10] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

      if (ch[11] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

      if (ch[4] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

      if (ch[5] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

      if (ch[6] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

      if (ch[7] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);

      analog_in = analogRead(A3);
      delay (analog_in / 24);
    }

    // check to see if up, down, or wait and increment or change
    for (l = 0; l <= 15; l++)
    {
      if (ud[l] == 1 && ch[l] >= 250)
      {
        ud[l] = 0;
        waitcount[l]++;
      }
      else if (ud[l] == 2)
      {
        if (waitcount[l] >= 15)
        {
          ud[l] = 2;
          waitcount[l] = 0;
        }
        else
          waitcount[l]++;
      }
      else if (ud[l] == 0 && ch[l] <= 5)
      {
        ud[l] = 2;
        waitcount[l]++;
      }
      else
      {
        ud[l] = ud[l];
        waitcount[l]++;
      }
    }
  }
}

//! Using the PWM dim WITH FADE commands, a wave of light is created by fading
//! the LED brightnesses up and down. WITH FADE option uses the logarithmic
//! fade function of the LT3965 and allows the brightness to fade from low to
//! high or from high to low with a single I2C bus command. This pattern has
//! low bus traffic.
void fadewave()
{
  setThresh();

  int8_t ack = 0;                               // I2C acknowledge bit
  An0 = analogRead(A0);       // Read 8 position switch pin 1
  An1 = analogRead(A1);       // Read 8 position switch pin 2
  An2 = analogRead(A2);       // Read 8 position switch pin 3

  ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, 1);
  ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, 1);
  ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, 1);
  delay(250);

  if (!startsequence)
  {
    digitalWrite(PWMPIN, HIGH);
    delay(30);
    digitalWrite(LOADPIN, LOW);
    delay(30);
    startsequence = TRUE;
    delay(150);
  }

  while (An0 < 120 && An1 > 120 && An2 > 120)
  {
    An0 = analogRead(A0);
    An1 = analogRead(A1);
    An2 = analogRead(A2);

    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0011, 255);
    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0011, 255);
    delay(200);
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0011, 255);
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0011, 255);
    delay(200);
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0011, 255);
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0011, 255);
    delay(200);
    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0011, 255);
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0011, 255);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0011, 255);
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0011, 255);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0011, 255);
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0011, 255);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0011, 255);
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0011, 255);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0011, 255);
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0011, 255);
    delay(1000);

    An0 = analogRead(A0);
    An1 = analogRead(A1);
    An2 = analogRead(A2);

    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0011, 1);
    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0011, 1);
    delay(200);
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0011, 1);
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0011, 1);
    delay(200);
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0011, 1);
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0011, 1);
    delay(200);
    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0011, 1);
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0011, 1);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0011, 1);
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0011, 1);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0011, 1);
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0011, 1);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0011, 1);
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0011, 1);
    delay(200);
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0011, 1);
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0011, 1);
    delay(1000);

    An0 = analogRead(A0);
    An1 = analogRead(A1);
    An2 = analogRead(A2);
  }
}

//! A wave of light from right to left and from left to right changing PWM
//! dimming brightness quickly increasing and decreasing at rates that look
//! like a wave running from one side of the LEDs to the other. The interactive
//! potentiometer controls the speed of the wave.
void wave()  // wave function
{
  setThresh();

  int8_t ack = 0;       // I2C acknowledge bit
  uint8_t i, j, k, l;
  int up_down = 1; //1 = up, 0 = down, 2 = startup wait, 3 = wait
  int delay_var = 20;
  uint8_t ch[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  ;  //channel count variables
  int ud[16] = {1, 2, 2, 2, 2, 2, 2, 2, 2,  2, 2, 2, 2, 2, 2, 1};  //count up or down variables; 1 = up, 0 = down, 2 = wait
  int waitcount[16]; // wait count for unusued LEDs
  uint16_t analog_in;  // potentiometer reading 0 - 255?

  waitcount[0] = -8;
  waitcount[1] = 15;
  waitcount[2] = 14;
  waitcount[3] = 13;
  waitcount[4] = 12;
  waitcount[5] = 11;
  waitcount[6] = 10;
  waitcount[7] = 9;
  waitcount[8] = 9;
  waitcount[9] = 10;
  waitcount[10] = 11;
  waitcount[11] = 12;
  waitcount[12] = 13;
  waitcount[13] = 14;
  waitcount[14] = 15;
  waitcount[15] = -8;

  /// start with this brightness
  // delay (1000);
  if (ch[0] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

  if (ch[1] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

  if (ch[2] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

  if (ch[3] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);


  if (ch[4] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

  if (ch[5] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

  if (ch[6] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

  if (ch[7] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);

  if (ch[8] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

  if (ch[9] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

  if (ch[10] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

  if (ch[11] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

  if (ch[12] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

  if (ch[13] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

  if (ch[14] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

  if (ch[15] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

  if (!startsequence)
  {
    digitalWrite(PWMPIN, HIGH);
    delay(1);
    digitalWrite(LOADPIN, LOW);
    delay(1);
    startsequence = TRUE;
  }
  for (j = 0; j < 21; j++)
  {
    for (i = 0; i <= 4 ; i++)
    {
      for (k = 0; k <= 15; k++)
      {
        if (ud[k] == 1)
          ch[k] = ch[k] + 50;
        else if (ud[k] == 0)
          ch[k] = ch[k] - 5;
        else
          ch[k] = ch[k];
      }

      if (ch[0] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

      if (ch[1] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

      if (ch[2] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

      if (ch[3] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);


      if (ch[4] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

      if (ch[5] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

      if (ch[6] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

      if (ch[7] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);

      if (ch[8] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

      if (ch[9] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

      if (ch[10] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

      if (ch[11] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

      if (ch[12] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

      if (ch[13] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

      if (ch[14] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

      if (ch[15] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

      analog_in = analogRead(A3);
      delay (analog_in / 100);
    }

    // check to see if up, down, or wait and increment or change
    for (l = 0; l <= 15; l++)
    {
      if (ud[l] == 1 && ch[l] >= 250)
      {
        ud[l] = 0;
        waitcount[l]++;
      }
      else if (ud[l] == 2)
      {
        if (waitcount[l] >= 15)
        {
          ud[l] = 1;
          waitcount[l] = -8;
        }
        else
          waitcount[l]++;
      }
      else if (ud[l] == 0 && ch[l] <= 5)
      {
        ud[l] = 2;
        waitcount[l]++;
      }
      else
      {
        ud[l] = ud[l];
        waitcount[l]++;
      }
    }
  }
  // delay (1000);
  delay(50);
  // Run Wave Backwards
  waitcount[0] = 9;
  waitcount[1] = 10;
  waitcount[2] = 11;
  waitcount[3] = 12;
  waitcount[4] = 13;
  waitcount[5] = 14;
  waitcount[6] = 15;
  waitcount[7] = -8;
  waitcount[8] = -8;
  waitcount[9] = 15;
  waitcount[10] = 14;
  waitcount[11] = 13;
  waitcount[12] = 12;
  waitcount[13] = 11;
  waitcount[14] = 10;
  waitcount[15] = 9;


  ud[0] = 2;
  ud[1] = 2;
  ud[2] = 2;
  ud[3] = 2;
  ud[4] = 2;
  ud[5] = 2;
  ud[6] = 2;
  ud[7] = 1;
  ud[8] = 1;
  ud[9] = 2;
  ud[10] = 2;
  ud[11] = 2;
  ud[12] = 2;
  ud[13] = 2;
  ud[14] = 2;
  ud[15] = 2;


  /// start with this brightness
  if (ch[0] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

  if (ch[1] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

  if (ch[2] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

  if (ch[3] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);


  if (ch[4] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

  if (ch[5] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

  if (ch[6] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

  if (ch[7] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);

  if (ch[8] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

  if (ch[9] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

  if (ch[10] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

  if (ch[11] == 0)
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
  else
    ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

  if (ch[12] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

  if (ch[13] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

  if (ch[14] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

  if (ch[15] == 0)
    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
  else
    ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

  for (j = 0; j < 21; j++)
  {
    for (i = 0; i <= 4; i++)
    {
      for (k = 0; k <= 15; k++)
      {
        if (ud[k] == 1)
          ch[k] = ch[k] + 50;
        else if (ud[k] == 0)
          ch[k] = ch[k] - 5;
        else
          ch[k] = ch[k];
      }

      if (ch[0] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, ch[0]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL0, 0b0010, ch[0]);

      if (ch[1] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, ch[1]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL3, 0b0010, ch[1]);

      if (ch[2] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, ch[2]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL4, 0b0010, ch[2]);

      if (ch[3] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, ch[3]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL7, 0b0010, ch[3]);

      if (ch[4] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, ch[4]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL0, 0b0010, ch[4]);

      if (ch[5] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, ch[5]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL3, 0b0010, ch[5]);

      if (ch[6] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, ch[6]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL4, 0b0010, ch[6]);

      if (ch[7] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, ch[7]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL7, 0b0010, ch[7]);

      if (ch[8] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, ch[8]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL6, 0b0010, ch[8]);

      if (ch[9] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, ch[9]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL5, 0b0010, ch[9]);

      if (ch[10] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, ch[10]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL2, 0b0010, ch[10]);

      if (ch[11] == 0)
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, ch[11]);
      else
        ack |= i2c_scwritelong(address2, CHANNEL1, 0b0010, ch[11]);

      if (ch[12] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, ch[12]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL6, 0b0010, ch[12]);

      if (ch[13] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, ch[13]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL5, 0b0010, ch[13]);

      if (ch[14] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, ch[14]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL2, 0b0010, ch[14]);

      if (ch[15] == 0)
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, ch[15]);
      else
        ack |= i2c_scwritelong(address1, CHANNEL1, 0b0010, ch[15]);

      analog_in = analogRead(A3);
      delay (analog_in / 100);
    }

    // check to see if up, down, or wait and increment or change
    for (l = 0; l <= 15; l++)
    {
      if (ud[l] == 1 && ch[l] >= 250)
      {
        ud[l] = 0;
        waitcount[l]++;
      }
      else if (ud[l] == 2)
      {
        if (waitcount[l] >= 15)
        {
          ud[l] = 1;
          waitcount[l] = -8;
        }
        else
          waitcount[l]++;
      }
      else if (ud[l] == 0 && ch[l] <= 5)
      {
        ud[l] = 2;
        waitcount[l]++;
      }
      else
      {
        ud[l] = ud[l];
        waitcount[l]++;
      }
    }
  }

}


//! Performs an analog read on the potentiometer pin using hysteresis to
//! reduce variation in ADC values. This prevents the read value of the potentiomter
//! from bouncing between different values when the potentiometer is not
//! being moved.
int readpot(void)
{
  uint8_t tempupdown;
  currentread = analogRead(A3);

  if ((currentread -  lastread) > 0)
    tempupdown = UP;
  if ((currentread - lastread) < 0)
    tempupdown = DOWN;

  if (tempupdown  == updown)
  {
    lastread = currentread;
    return (lastread);
  }
  else
  {
    if (((abs(currentread -  lastread)) > 3))
    {
      if (updown == UP)
        updown = DOWN;
      else if (updown == DOWN)
        updown = UP;
      lastread = currentread;
      return (lastread);
    }
    else
    {
      return (lastread);
    }
  }
}

//! Sets the short circuit and open circuit thresholds defined for
//! DC2218A. This function performs a series of single channel and all
//! channel writes to ensure that threshold registers are set, as well
//! as ensuring all fault registers are cleared.
void setThresh()
{
  int8_t ack = 0;

  ack |= i2c_scwriteshort(address1, CHANNEL0, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL1, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL2, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL3, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL4, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL5, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL6, 0b0000);
  ack |= i2c_scwriteshort(address1, CHANNEL7, 0b0000);

  ack |= i2c_scwriteshort(address2, CHANNEL0, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL1, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL2, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL3, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL4, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL5, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL6, 0b0000);
  ack |= i2c_scwriteshort(address2, CHANNEL7, 0b0000);

  ack |= i2c_scwritelong(address1, CHANNEL0, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL1, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL2, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL3, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL4, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL5, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL6, 0b0000, 0);
  ack |= i2c_scwritelong(address1, CHANNEL7, 0b0000, 0);

  ack |= i2c_scwritelong(address2, CHANNEL0, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL1, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL2, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL3, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL4, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL5, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL6, 0b0000, 0);
  ack |= i2c_scwritelong(address2, CHANNEL7, 0b0000, 0);

  ack |= i2c_acwrite(address1, 0b00000000);
  ack |= i2c_acwrite(address2, 0b00000000);
}

Download LT3965 - Linduino Header File

/*!
LT3965 - Octal Matrix LED Bypass Switch

@verbatim

The LT3965 is an LED bypass switching device for dimming indiviadual LEDs in a
string using a common current source. It features eight individually controlled
floating source N-channel MOSFET switches rated for 17V/330 mΩ. The eight
switches can be connected in parallel and/or in series to bypass current around
one or more LEDs in a string. The LT3965 as a slave uses the I2C serial interface
to communicate with the master. Each of eight channels can be independently
programmed to bypass the LED string in constant on, constant off, dimming without
fade transition or dimming with fade transition mode.

WRITE PROTOCOLS (MSB FIRST):

            Byte #1                     Byte #2                         Byte #3

ACMODE WRITE      0   1   0   A4  A3  A2  A1  0(W)        B7  B6  B5  B4  B3  B2  B1  B0
SCMODE WRITE SHORT    1   0   1   A4  A3  A2  A1  0(W)    0   C3  C2  C1  B3  B2  B1  B0
SCMODE WRITE LONG   1   0   1   A4  A3  A2  A1  0(W)    1   C3  C2  C1  B11 B10 B9  B8    B7  B6  B5  B4  B3  B2  B1  B0
BCMODE WRITE      0   0   0   1   1   0   0   0(W)

W           : I2C Write (0)
R           : I2C Read  (1)
Ax          : Device Address
Bx          : Data Bits
Cx          : Channel Address


@endverbatim


http://www.linear.com/product/LT3965

http://www.linear.com/product/LT3965#demoboards

REVISION HISTORY
$Revision: 5045 $
$Date: 2016-05-04 17:56:28 -0700 (Wed, 04 May 2016) $

Copyright (c) 2013, Linear Technology Corp.(LTC)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of Linear Technology Corp.

The Linear Technology Linduino is not affiliated with the official Arduino team.
However, the Linduino is only possible because of the Arduino team's commitment
to the open-source community.  Please, visit http://www.arduino.cc and
http://store.arduino.cc , and consider a purchase that will help fund their
ongoing work.
*/

/*! @file
    @ingroup LT3965
    Header for LT3965: Octal Matrix LED Bypass Switch
*/

//! @name LT3965 I2C 7-BIT DEVICE ADDRESS
//! @{
#define address1       0b00000000 // I2C Device address 1
#define address2       0b00001111   // I2C Device address 2

#define AC_ADDR_0      0x20
#define SC_ADDR_0      0x50
#define BC_ADDR        0x0C
#define TM_ADDR        0x73
//! @}

//! @name 8 CHANNELS
//! @{
#define CHANNEL0       0X00
#define CHANNEL1       0X01
#define CHANNEL2       0X02
#define CHANNEL3       0X03
#define CHANNEL4       0X04
#define CHANNEL5       0X05
#define CHANNEL6       0X06
#define CHANNEL7       0X07
//! @}

#define UP           1
#define DOWN       0

#define TRUE         1
#define FALSE        0

#define ALERTPIN     2
#define PWMPIN       7
#define LOADPIN      9
#define BUTTONPIN    12
#define REDLED       11

//! ACMODE Write Command to write a "value" byte to device at "address"
int8_t i2c_acwrite(uint8_t address, uint8_t value);

//! ACMODE Read Command to read 3 data bytes from device at "address"
int8_t i2c_acread(uint8_t address, uint8_t *values);

//! SCMODE Write Command Short Format to write a "value" byte to device at "address"
int8_t i2c_scwriteshort(uint8_t address, uint8_t channel, uint8_t data_SCMREG);

//! SCMODE Write Command Long Format to write 2 "value" bytes to device at "address"
int8_t i2c_scwritelong(uint8_t address, uint8_t channel, uint8_t data_SCMREG, uint8_t dimming_value);

//! SCMODE Write Short + SCMODE Read Command
int8_t i2c_scwriteshort_scread(uint8_t address, uint8_t channel, uint8_t data_SCMREG, uint8_t *values);

//! BCMODE Read Command
int8_t i2c_bcread(uint8_t *value);

Download LT3965 - Linduino.CPP File

/*!
LT3965 - Octal Matrix LED Bypass Switch

@verbatim

The LT3965 is an LED bypass switching device for dimming indiviadual LEDs in a
string using a common current source. It features eight individually controlled
floating source N-channel MOSFET switches rated for 17V/330 mΩ. The eight
switches can be connected in parallel and/or in series to bypass current around
one or more LEDs in a string. The LT3965 as a slave uses the I2C serial interface
to communicate with the master. Each of eight channels can be independently
programmed to bypass the LED string in constant on, constant off, dimming without
fade transition or dimming with fade transition mode.

@endverbatim

http://www.linear.com/product/LT3965

http://www.linear.com/product/LT3965#demoboards

REVISION HISTORY
$Revision: 5074 $
$Date: 2016-05-09 16:57:13 -0700 (Mon, 09 May 2016) $

Copyright (c) 2013, Linear Technology Corp.(LTC)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of Linear Technology Corp.

The Linear Technology Linduino is not affiliated with the official Arduino team.
However, the Linduino is only possible because of the Arduino team's commitment
to the open-source community.  Please, visit http://www.arduino.cc and
http://store.arduino.cc , and consider a purchase that will help fund their
ongoing work.
*/

//! @defgroup LT3965 LT3965: Octal Matrix LED Bypass Switch

/*! @file
   @ingroup LT3965
   Library for LT3965 Octal Matrix LED Bypass Switch
*/

#include <stdio.h>
#include "Linduino.h"
#include "LT_I2C.h"
#include "LT3965.h"


// ACMODE Write Command to write a "value" byte to device at "address"
int8_t i2c_acwrite(uint8_t address, uint8_t value)
{
  int8_t ret = 0 ;
  address = address | AC_ADDR_0;
  ret |= i2c_write_byte(address, value);
  return ret;
}


// ACMODE Read Command to read 3 data bytes from device at "address"
int8_t i2c_acread(uint8_t address, uint8_t *values)
{
  int8_t ret = 0 ;
  address = address | AC_ADDR_0;
  ret |= i2c_read_block_data(address, 3, values);
  return ret;
}


// SCMODE Write Command Short Format to write a "value" byte to device at "address"
int8_t i2c_scwriteshort(uint8_t address, uint8_t channel, uint8_t data_SCMREG)
{
  int8_t ret = 0 ;
  uint8_t data = 0;

  address = address | SC_ADDR_0;
  data = (channel << 4) | data_SCMREG;      //Write value

  ret |= i2c_write_byte(address, data);
  return ret;
}


// SCMODE Write Command Long Format to write 2 "value" bytes to device at "address"
int8_t i2c_scwritelong(uint8_t address, uint8_t channel, uint8_t data_SCMREG, uint8_t dimming_value)
{
  int8_t ret = 0;
  uint8_t command = 0x80;

  address = address | SC_ADDR_0;
  command |= ((channel << 4) | data_SCMREG);

  ret |= i2c_write_byte_data(address, command, dimming_value);
  return ret;
}


// SCMODE Write Short + SCMODE Read Command
int8_t i2c_scwriteshort_scread(uint8_t address, uint8_t channel, uint8_t data_SCMREG, uint8_t *values)
{
  int8_t ret = 0;
  uint8_t command = 0;

  address = address | SC_ADDR_0;
  command |= ((channel << 4) | data_SCMREG);

  ret |= i2c_read_block_data(address, command, 2, values);
  return ret;
}


// BCMODE Read Command
int8_t i2c_bcread(uint8_t *value)
{
  int8_t ret = 0 ;
  uint8_t address = BC_ADDR;
  ret |= i2c_read_byte(address, value);
  return ret;
}

技术支持