This is the Code of Christmas Tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
// 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