Video on upgrading some cheap battery powered lights on a Christmas tree. Source code further down.
Source code
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 |
#define LEDPin 3 #define DOT_DURATION 200 // Time in milliseconds of a dot in morse code static const char *alpha[] = { ".-", //A "-...", //B "-.-.", //C "-..", //D ".", //E "..-.", //F "--.", //G "....", //H "..", //I ".---", //J "-.-", //K ".-..", //L "--", //M "-.", //N "---", //O ".--.", //P "--.-", //Q ".-.", //R "...", //S "-", //T "..-", //U "...-", //V ".--", //W "-..-", //X "-.--", //Y "--..", //Z }; static const char *num[] = { "-----", //0 ".----", //1 "..---", //2 "...--", //3 "....-", //4 ".....", //5 "-....", //6 "--...", //7 "---..", //8 "----.", //9 }; void setup() { // put your setup code here, to run once: pinMode(LEDPin,OUTPUT); } void loop() { // put your main code here, to run repeatedly: MorseCode("MERRY CHRISTMAS"); // OUTPUT THIS IN MORSE CODE Flash(10,1,0.25); // Flash 10 times, On of 1 seconds, off for 0.25s Flash(20,0.1,0.1); // Flash 20 times, On of 0.5seconds, off for 0.5 Pulse(10,1,1); // Pulse (fade up then down) 10 times, 1 second fade up, 1s fade down Flash(10,1,0.5); // Flash 20 times, On of 1 seconds, off for 0.5 FadeUp(3); // Start at off, fade to max brightness over 3 seconds Flash(5,1.5,1.5); FadeDown(3); // Start at full one, fade to off over 3 seconds Pulse(10,1.5,0.5); // Pulse (fade up then down) 10 times, 1.5 second fade up, 0.5 fade down SwitchLights(true,4); // switch on for four seconds Flash(50,0.05,0.05); FadeDown(3); Pulse(20,0.5,0.5); SwitchLights(false,4); // switch off for four seconds } void SwitchLights(bool State, float Duration) { // switch lights on or off according to state, true for on, false for off // on or off for what ever duration is (in seconds) digitalWrite(LEDPin,State); delay(Duration*1000); // convert to milliseconds } void MorseCode(char* Message) { // outputs the "Message" as a morse code, any none alphnumeric char is ignored char character; for(uint16_t i=0;i<strlen(Message);i++) { character=toupper(Message[i]); if((character>=65) &(character<=90)) { // alpha character MorseDecode(alpha[character-65]); // subtract 65 so that range of codes is 0 to 25 (for A-Z) } else { if((character>=48)&(character<=57)) { // number MorseDecode(num[character-48]); // subtract 48 so that range of codes is 0 to 9 (for 0-9) } else { if(character==32) //Space delay(DOT_DURATION*4); // time between words is 7 dot periods, as we'll have just done 3 from end of last char this would give 7 } } } } void MorseDecode(char* Morse) { // flashes the morse contained in "Morse" for(uint8_t i=0;i<strlen(Morse);i++) { digitalWrite(LEDPin,true); if(Morse[i]=='.') { delay(DOT_DURATION); // short flash for dot } else { delay(600); // long flash for dah } digitalWrite(LEDPin,false); delay(DOT_DURATION); // delay between a dot or a dash is one dot duration } delay(DOT_DURATION*3); // delay between letters of a word is 3 dot periods } void Flash(uint8_t NumFlashes,float OnDuration,float OffDuration) { // Duration is in seconds OnDuration*=1000; // convert to milliseconds OffDuration*=1000; // convert to milliseconds for(uint8_t i=0;i<NumFlashes;i++) { digitalWrite(LEDPin,true); delay(OnDuration); digitalWrite(LEDPin,false); delay(OffDuration); } } void Pulse(uint8_t NumberPulses, float OnDuration, float OffDuration) { for(uint8_t i=0;i<NumberPulses;i++) { FadeUp(OnDuration); FadeDown(OffDuration); } } void FadeUp(float FadeTime) // FadeTime is in seconds { float Value=0; float AmountToFadeBy=255/(FadeTime*100); // we are going to change the brightness every 1/100 of a second for(uint16_t i=FadeTime*100;i>0;i--) { analogWrite(LEDPin,Value); Value+=AmountToFadeBy; delay(10); } } void FadeDown(float FadeTime) // FadeTime is in seconds { float Value=255; float AmountToFadeBy=255/(FadeTime*100); // we are going to change the brightness every 1/100 of a second for(uint16_t i=FadeTime*100;i>0;i--) { analogWrite(LEDPin,Value); Value-=AmountToFadeBy; delay(10); } } |