Lesson 11 Christmas Tree lights code Example

This is the Code of Christmas Tree

// By Roee Bloch  CHRISTMAS TREE
//All right Reserved
// Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com
//
// This source is subject to the Roee Bloch License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Roee Bloch</author>
// <email>roeebloch@walla.co.il</email>
// <date>June 2015</date>

#define strips 5 // number of strip lamps to be used
#define mydelay 400 // delay of 1000 ms between changes
#define randompin 12 // when this digital pin conneted to GND will run RANDOM PATTERNS


int myrandom, mode = 1; // if mode=0 => random mode 1 is normal
byte cal = 1,my_top;
int num, func_num = 0,top,random_delay;
byte mypins[] = {2, 3, 4, 5, 6, 7, 8, 9};   //maximum 8 outputs for this program from LSB to MSB
void setup()
{
  pinMode(randompin, INPUT);           // set pin to input
  digitalWrite(randompin, HIGH);       // turn on pullup resistors
  Serial.begin(9600);
  if ((strips <= 8) && (strips > 1)) // check if number of Relays is between 2 and 8
  {
    for (int x = 0; x < strips; x++) // decalre all RELAYS as OUTPUTS in one loop
    {
 //     Serial.print("IO=");
 //     Serial.println(int(mypins[x]));
      pinMode (mypins[x], OUTPUT);
    }
  }
  Serial.println("started");
  pinMode (2, OUTPUT);
  top = pow(2, strips)-1; // max number for chosen strips
  my_top=byte(top); // for mask
}
void loop()
{
  digitalWrite(randompin, HIGH);
  if (digitalRead(randompin) == 0) //random mode
  {
    Serial.println("Random mode");
    mode = 0;
    func_num = random(1, 9); // random run
    random_delay= random(1,11)*100;
    Serial.print("Random delay is:");
    Serial.println(random_delay);
  }
  else
  {
    Serial.println("Regular mode");
    mode = 1;
    func_num++; // go pattern one by one 1,2,3,....
    random_delay=mydelay;
  }
  Serial.println(func_num);
  switch (func_num)
  {
    case 1: pattern1();
      break;
    case 2: pattern2();
      break;
    case 3: pattern3();
      break;
    case 4: pattern4();
      break;
    case 5: pattern5();
      break;
    case 6: pattern6();
      break;
    case 7: pattern7();
      break;
    case 8: func_num = 0;
      cal = 1;
      break;
  }
}

void LightLeds(byte x) {
  int mystart;
  String myNumber = String(x, BIN); // convert byte to string in order to work with it easily
  int binLength = myNumber.length();
  if (binLength < 8) {
    digitalWrite(mypins[binLength], 0);
  }
  for (int q = binLength, mystart = 0; q > 0; q--, mystart++)
  {
    if  (myNumber[q - 1] == '1')
    {
      digitalWrite(mypins[mystart], 1); // need to get to correct bit
    }
    else
    {
      digitalWrite(mypins[mystart], 0); // need to get to correct bit
    }
  }
}
void pattern1() // move left
{
  cal=1;
  for (int a = 1; a <= strips; a++)
  {
    LightLeds(cal);
    delay(random_delay);
    cal = cal * 2;
  }
  cal = 1;
  all_off();
}
void pattern2() // move right
{
  cal = pow(2, strips);
  for (int a = strips; a >= 0; a--)
  {
    delay(random_delay);
    cal = cal >> 1;
    LightLeds(cal);
  }
  all_off();
}
void mystop()
{
  Serial.println("stopped");
loop2:
  goto loop2;
}
void all_off()
{
  for (int i = 0; i < 8; i++)
  {
    digitalWrite(mypins[i], 0);
  }
}
void pattern3() //binary up
{
  cal = 1;
  byte limit;
  limit=my_top+1;
//  int top = pow(2, strips);
//  my_top=byte(top); // for mask
  for (int i = 1; i <=limit; i++)
  {
    delay(random_delay);
    cal = i;
    LightLeds(cal);
  }
}
void pattern4() //blink half half
{
  for (int i = 1; i < 6; i++)
  {
    delay(random_delay);
    LightLeds(170 & my_top); // equal to 10101010
    delay(random_delay);
    LightLeds(85  & my_top);// equal to 1010101
  }
  all_off();
}
void pattern5() // blink all together
{
  for (int i = 1; i < 6; i++)
  {
    delay(random_delay);
    LightLeds(my_top);
    delay(random_delay);
    all_off();
  }
}
void pattern6()
{
  for (int i = 1; i < 6; i++) // two and two
  { 
    delay(random_delay);
    LightLeds(204 & my_top);
    delay(random_delay);
    LightLeds(51 & my_top);
  }
  all_off();
}
void pattern7() //binary down
{
  cal = 1;
  for (int i = my_top; i >= 0; i--)
  {
    delay(random_delay);
    cal = i;
    LightLeds(i);
  }
  all_off();
}

Video for Theory: lesson-11-christmas-tree-part-1
Video of First Run: lesson-11-christmas-tree-lights-part-2
Video with Strong Leds: lesson-11-christmas-tree-lights-part-3