Components and supplies
Arduino Nano R3
LED (generic)
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Flash Light contrecoller Norsak_12
arduino
small change, white led 5 min dimming down after 60 min
1// By Christian Roy 2// Norsak LED controler 3// rev_09 4//#include <avr/sleep.h> 5 6#include <avr/power.h> 7#include <avr/sleep.h> 8#include <Vcc.h> 9#include <avr/wdt.h> 10 11byte sw1 = 2 ;//pin 12byte LED_White = 11;//pin 13byte LED_Red1 = 6;//pin 14byte LED_Red2 = 7;//pin 15byte LED_Green1 = 5;//pin 16byte LED_Green2 = 4;//pin 17byte Read_Batt = 3;//pin 18float LED_W_Max; 19const float VccMin = 2.7; // Minimum expected Vcc level, in Volts. 20const float VccMax = 4.2; // Maximum expected Vcc level, in Volts. 21const float VccCorrection = 1.0 / 1.0; // Measured Vcc by multimeter divided by reported Vcc 22Vcc vcc(VccCorrection); 23byte LED_W_pwr; 24boolean dimmer = false ; 25int timer; 26int Tsel; 27byte select; 28int batt = 0 ; //pin 29int SolPanel = 1; //pin 30float vSol; // voltage at the solar panel 31float V10; 32boolean RedGreen = false; 33 34/////////////////////////////////////////////////////////////// 35void setup() 36/////////////////////////////////////////////////////////////// 37{ 38 analogReference(INTERNAL);//equal to 1.1 volts on ATmega328P 39 //pinMode(LED_BUILTIN, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 40 pinMode(sw1, INPUT_PULLUP); 41 pinMode(LED_White, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 42 analogWrite(LED_White, 0); //0 = OFF & 255 ON 43 pinMode (LED_Red1, INPUT); // High Impedence LED Off 44 pinMode (LED_Red2, INPUT); // High Impedence LED Off 45 pinMode (LED_Green1, INPUT); // High Impedence LED Off 46 pinMode (LED_Green2, INPUT); // High Impedence LED Off 47 //digitalWrite(LED_BUILTIN, LOW); 48 49 50 Serial.begin(9600); 51 attachInterrupt(0, SW_ISR, CHANGE); 52 /* 53 The 5 different modes are: 54 SLEEP_MODE_IDLE -the least power savings 55 SLEEP_MODE_ADC 56 SLEEP_MODE_PWR_SAVE 57 SLEEP_MODE_STANDBY 58 SLEEP_MODE_PWR_DOWN -the most power savings 59 */ 60} 61 62void loop() { 63 64 /////////////////////////////////////////////////////////////// 65 sleepNow(); 66 /////////////////////////////////////////////////////////////// 67 68 Tsel = 0; 69 select = 0;// SELECT 70 71 while (digitalRead(sw1) == LOW) 72 { 73 select ++; 74 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On LED On 75 delay (12); 76 pinMode (LED_Green1, INPUT); // High Impedence LED Off 77 delay (600); 78 } 79 80 delay (750); 81 switch (select) 82 { 83 //////////////////////////////////////////// 84 /// Green RED /// 85 //////////////////////////////////////////// 86 case 1: 87 attachInterrupt(0, SW_ISR, CHANGE); 88 while (1) 89 { 90 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 91 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 92 delay (50); 93 pinMode (LED_Red1, INPUT); // High Impedence LED Off 94 pinMode (LED_Red2, INPUT); // High Impedence LED Off 95 delay (450); 96 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 97 pinMode (LED_Green2, OUTPUT); digitalWrite (LED_Green2, LOW); // LED On 98 delay (50); 99 pinMode (LED_Green1, INPUT); // High Impedence LED Off 100 pinMode (LED_Green2, INPUT); // High Impedence LED Off 101 delay (450); 102 } 103 break; 104 105 106 //////////////////////////////////////////// 107 /// WHITE LED /// 108 //////////////////////////////////////////// 109 case 2: 110 LED_W_pwr = 2;// minimum led white 111 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 112 delay (2510); //waitnig for power LED up 113 while (digitalRead(sw1) == LOW) // adjust white LED power 114 { 115 LED_W_pwr += 1; 116 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 117 delay(50); 118 if (LED_W_pwr > 240)// max led power 119 { 120 analogWrite(LED_White, 0); 121 delay (500); 122 analogWrite(LED_White, 240); // max led power 123 delay (500); 124 while (digitalRead(sw1) == LOW) {}; 125 break; 126 } 127 } 128 attachInterrupt(0, SW_ISR, CHANGE); 129 delay (3600000); // 60 min 130 //diming off// 131 for (int LED_W_dimer = LED_W_pwr ; LED_W_dimer > 0; LED_W_dimer--) { // 360 = 30 min. 132 delay (300000/LED_W_pwr); 133 analogWrite(LED_White, LED_W_dimer); 134 } 135 asm volatile (" jmp 0"); 136 break; 137 138 139 //////////////////////////////////////////// 140 /// S O S /// 141 //////////////////////////////////////////// 142 case 3: 143 ////Serial.println("SOS"); 144 attachInterrupt(0, SW_ISR, CHANGE); 145 while (1) 146 { 147 dot(); dot(); dot(); // S 148 delay(700); 149 dash(); dash(); dash(); // O 150 delay(700); 151 dot(); dot(); dot(); // S 152 delay(6000); 153 } 154 break; 155 156 157 //////////////////////////////////////////// 158 /// LED RED /// 159 //////////////////////////////////////////// 160 case 4: 161 pinMode(LED_Red2, INPUT); // High Impedence LED Off 162 pinMode (LED_Red1, OUTPUT);//digitalWrite (LED_Red1,LOW); // LED On 163 analogWrite(LED_Red1, 235);// 15%, 255 = OFF & 0 ON 164 delay (2000); 165 if (digitalRead(sw1) == LOW) { 166 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On full power on 167 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 168 while (digitalRead(sw1) == LOW) {}; 169 attachInterrupt(0, SW_ISR, CHANGE); 170 delay (3600000); //60 mins 171 asm volatile (" jmp 0"); 172 } 173 ADCSRA = 135; //enable analog digi 174 attachInterrupt(0, SW_ISR, CHANGE); 175 delay (100); 176 while (1) { 177 vSol = analogRead (SolPanel); 178 //Serial.println(vSol); 179 if (vSol > 600) { //no sun 180 asm volatile (" jmp 0"); 181 } 182 delay (500); 183 } 184 break; 185 186 187 //////////////////////////////////////////// 188 /// White Flashing with night /// 189 //////////////////////////////////////////// 190 case 5: 191 attachInterrupt(0, SW_ISR, CHANGE); 192 while (1) { 193 ADCSRA = 135; //enable analog digi 194 delay (100); 195 vSol = analogRead (SolPanel); 196 if (vSol < 600) { //Volts or no sun 197 digitalWrite(LED_White, HIGH); 198 delay(20);// mSec 199 digitalWrite(LED_White, LOW); 200 } 201 pause (2000);// msec 202 } 203 break; 204 205 206 //////////////////////////////////////////// 207 /// B A T T E R Y level /// 208 //////////////////////////////////////////// 209 case 6: 210 ADCSRA = 135; //enable analog digi 211 delay (100); 212 attachInterrupt(0, SW_ISR, CHANGE); 213 timer = 0; 214 while (digitalRead(sw1) == HIGH && timer < 4) //stop after n times 215 { 216 float p = vcc.Read_Perc(VccMin, VccMax); 217 //Serial.print (p); 218 //Serial.println ("%"); 219 V10 = int(p) / 10; 220 for (int v = 0; v < V10; v++) 221 { 222 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 223 delay (10); 224 pinMode (LED_Green1, INPUT); // High Impedence LED Off 225 delay (650); 226 } 227 for (int v = V10; v < 10; v++) 228 { 229 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 230 delay (10); 231 pinMode (LED_Red1, INPUT); // High Impedence LED Off 232 delay (650); 233 } 234 delay (2000); 235 timer++; 236 } 237 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 238 while (digitalRead(sw1) == LOW) {}; delay(250); 239 asm volatile (" jmp 0"); 240 break; 241 } 242 243} 244 245//////////////////////////////////////////////////////// 246void dot() 247//////////////////////////////////////////////////////// 248{ 249 analogWrite(LED_White, 160); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 250 delay(22); 251 digitalWrite(LED_White, LOW); 252 delay(500); 253} 254 255//////////////////////////////////////////////////////// 256void dash() 257//////////////////////////////////////////////////////// 258{ 259 analogWrite(LED_White, 20); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 260 delay(150); 261 digitalWrite(LED_White, LOW); 262 delay(800); 263} 264 265//////////////////////////////////////////////////////// 266// sleep Now // 267//////////////////////////////////////////////////////// 268void sleepNow() 269{ 270 // disable ADC 271 ADCSRA = 0; 272 set_sleep_mode(SLEEP_MODE_PWR_DOWN);//SLEEP_MODE_PWR_SAVE); 273 interrupts(); 274 attachInterrupt(0, pinInterrupt, HIGH); 275 sleep_enable(); 276 sleep_mode(); 277 sleep_disable(); 278} 279 280//////////////////////////////////////////////////////// 281void pinInterrupt() 282//////////////////////////////////////////////////////// 283{ 284 detachInterrupt(0); 285 attachInterrupt(0, pinInterrupt, HIGH); 286} 287//////////////////////////////////////////////////////// 288// Interrupt Service Routin // 289//////////////////////////////////////////////////////// 290void SW_ISR() { 291 analogWrite(LED_White, 0); 292 MCUCR = bit (BODS) | bit (BODSE); // turn off brown-out enable in software 293 MCUCR = bit (BODS); 294 pinMode (LED_Green1, INPUT); // High Impedence LED Off 295 pinMode (LED_Green2, INPUT); // High Impedence LED Off 296 pinMode (LED_Red1, INPUT); // High Impedence LED Off 297 pinMode (LED_Red2, INPUT); // High Impedence LED Off 298 //pinMode(LED_Green1, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 299 for (long x = 0; x < 20000; x ++) { 300 pinMode (LED_Red1, INPUT); // delay 301 } 302 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 303 304 while (digitalRead(sw1) == LOW) {}; 305 delay(250); 306 asm volatile (" jmp 0"); 307} 308 309 310///////////////////////////////////////////////////////////////////// 311// S L E E P with delay // 312///////////////////////////////////////////////////////////////////// 313void pause(int WDPset) 314{ 315 // disable ADC 316 ADCSRA = 0; 317 // clear various "reset" flags 318 MCUSR = 0; 319 // allow changes, disable reset 320 WDTCSR = bit (WDCE) | bit (WDE); 321 // set interrupt mode and an interval 322 switch (WDPset) { 323 case 125: 324 WDTCSR = bit (WDIE) | bit (WDP1) | bit (WDP0); // set WDIE, and .125 second delay 325 break; 326 case 250: 327 WDTCSR = bit (WDIE) | bit (WDP2); // set WDIE, and .25 second delay 328 break; 329 case 500: 330 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP0); // set WDIE, and .5 second delay 331 break; 332 case 1000: 333 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1); // set WDIE, and 1 second delay 334 break; 335 case 2000: 336 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0); // set WDIE, and 2 second delay 337 break; 338 case 4000: 339 WDTCSR = bit (WDIE) | bit (WDP3) ; // set WDIE, and 4 second delay 340 break; 341 case 8000: 342 WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 second delay 343 break; 344 } 345 wdt_reset(); // pat the dog 346 347 set_sleep_mode (SLEEP_MODE_PWR_DOWN); 348 noInterrupts (); // timed sequence follows 349 sleep_enable(); 350 351 // turn off brown-out enable in software 352 MCUCR = bit (BODS) | bit (BODSE); 353 MCUCR = bit (BODS); 354 interrupts (); // guarantees next instruction executed 355 sleep_cpu (); 356 357 // cancel sleep as a precaution 358 sleep_disable(); 359} 360ISR (WDT_vect) 361{ 362 wdt_disable(); // disable watchdog 363} // end of WDT_vect 364 365 366 367 368 369 370
Norsak_11
arduino
1// By Christian Roy 2// Norsak LED controler 3// rev_09 4//#include <avr/sleep.h> 5 6#include <avr/power.h> 7#include <avr/sleep.h> 8#include <Vcc.h> 9#include <avr/wdt.h> 10 11byte sw1 = 2 ;//pin 12byte LED_White = 11;//pin 13byte LED_Red1 = 6;//pin 14byte LED_Red2 = 7;//pin 15byte LED_Green1 = 5;//pin 16byte LED_Green2 = 4;//pin 17byte Read_Batt = 3;//pin 18float LED_W_Max; 19const float VccMin = 2.7; // Minimum expected Vcc level, in Volts. 20const float VccMax = 4.2; // Maximum expected Vcc level, in Volts. 21const float VccCorrection = 1.0 / 1.0; // Measured Vcc by multimeter divided by reported Vcc 22Vcc vcc(VccCorrection); 23byte LED_W_pwr; 24boolean dimmer = false ; 25int timer; 26int Tsel; 27byte select; 28int batt = 0 ; //pin 29int SolPanel = 1; //pin 30float vSol; // voltage at the solar panel 31float V10; 32boolean RedGreen = false; 33 34/////////////////////////////////////////////////////////////// 35void setup() 36/////////////////////////////////////////////////////////////// 37{ 38 analogReference(INTERNAL);//equal to 1.1 volts on ATmega328P 39 //pinMode(LED_BUILTIN, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 40 pinMode(sw1, INPUT_PULLUP); 41 pinMode(LED_White, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 42 analogWrite(LED_White, 0); //0 = OFF & 255 ON 43 pinMode (LED_Red1, INPUT); // High Impedence LED Off 44 pinMode (LED_Red2, INPUT); // High Impedence LED Off 45 pinMode (LED_Green1, INPUT); // High Impedence LED Off 46 pinMode (LED_Green2, INPUT); // High Impedence LED Off 47 //digitalWrite(LED_BUILTIN, LOW); 48 49 50 Serial.begin(9600); 51 attachInterrupt(0, SW_ISR, CHANGE); 52 /* 53 The 5 different modes are: 54 SLEEP_MODE_IDLE -the least power savings 55 SLEEP_MODE_ADC 56 SLEEP_MODE_PWR_SAVE 57 SLEEP_MODE_STANDBY 58 SLEEP_MODE_PWR_DOWN -the most power savings 59 */ 60} 61 62void loop() { 63 /* 64 pinMode(LED_Red1, INPUT_PULLUP); 65 pinMode(LED_Red2, INPUT); // High Impedence LED Off 66 pinMode (LED_Red1,OUTPUT); // LED On 67 delay (5000); 68 pinMode(LED_Red1, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 69 delay (5000); 70 */ 71 72 /////////////////////////////////////////////////////////////// 73 sleepNow(); 74 /////////////////////////////////////////////////////////////// 75 76 Tsel = 0; 77 select = 0;// SELECT 78 79 while (digitalRead(sw1) == LOW) 80 { 81 select ++; 82 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On LED On 83 delay (12); 84 pinMode (LED_Green1, INPUT); // High Impedence LED Off 85 delay (600); 86 } 87 88 delay (750); 89 switch (select) 90 { 91 //////////////////////////////////////////// 92 /// Green RED /// 93 //////////////////////////////////////////// 94 case 1: 95 attachInterrupt(0, SW_ISR, CHANGE); 96 while (1) 97 { 98 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 99 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 100 delay (50); 101 pinMode (LED_Red1, INPUT); // High Impedence LED Off 102 pinMode (LED_Red2, INPUT); // High Impedence LED Off 103 delay (450); 104 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 105 pinMode (LED_Green2, OUTPUT); digitalWrite (LED_Green2, LOW); // LED On 106 delay (50); 107 pinMode (LED_Green1, INPUT); // High Impedence LED Off 108 pinMode (LED_Green2, INPUT); // High Impedence LED Off 109 delay (450); 110 } 111 break; 112 //////////////////////////////////////////// 113 /// WHITE LED /// 114 //////////////////////////////////////////// 115 case 2: 116 LED_W_pwr = 2;// minimum led white 117 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 118 delay (2510); //waitnig for power LED up 119 while (digitalRead(sw1) == LOW) 120 { 121 LED_W_pwr += 1; 122 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 123 delay(50); 124 if (LED_W_pwr > 240)// max led power 125 { 126 analogWrite(LED_White, 0); 127 delay (500); 128 analogWrite(LED_White, 240); // max led power 129 delay (500); 130 while (digitalRead(sw1) == LOW) {}; 131 break; 132 } 133 } 134 delay (300); 135 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 136 delay (30); 137 pinMode (LED_Green1, INPUT); // High Impedence LED Off 138 delay (500); 139 for (int t = 0; t < 100; t++) { //2 sec 140 if (digitalRead(sw1) == LOW) { 141 // full power on 142 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 143 dimmer = true; 144 while (digitalRead(sw1) == LOW) {}; 145 delay (25); 146 pinMode (LED_Green1, INPUT); // High Impedence LED Off 147 } 148 delay (20); 149 } 150 if (dimmer == true) { 151 float W_LED_Dim ; 152 W_LED_Dim = (float)LED_W_pwr / 360; 153 attachInterrupt(0, SW_ISR, CHANGE); 154 for (float LED_W_pwrf = LED_W_pwr ; LED_W_pwrf > 0; LED_W_pwrf = LED_W_pwrf - W_LED_Dim) {// 360 = 30 min. 155 delay (5000);//5sec 156 analogWrite(LED_White, LED_W_pwrf); 157 } 158 } 159 else 160 { 161 attachInterrupt(0, SW_ISR, CHANGE); 162 delay (3600000); //mSec 163 } 164 asm volatile (" jmp 0"); 165 break; 166 //////////////////////////////////////////// 167 /// S O S /// 168 //////////////////////////////////////////// 169 case 3: 170 ////Serial.println("SOS"); 171 attachInterrupt(0, SW_ISR, CHANGE); 172 while (1) 173 { 174 dot(); dot(); dot(); // S 175 delay(700); 176 dash(); dash(); dash(); // O 177 delay(700); 178 dot(); dot(); dot(); // S 179 delay(6000); 180 } 181 break; 182 //////////////////////////////////////////// 183 /// LED RED /// 184 //////////////////////////////////////////// 185 case 4: 186 pinMode(LED_Red2, INPUT); // High Impedence LED Off 187 pinMode (LED_Red1, OUTPUT);//digitalWrite (LED_Red1,LOW); // LED On 188 analogWrite(LED_Red1, 235);// 15%, 255 = OFF & 0 ON 189 delay (1500); 190 if (digitalRead(sw1) == LOW) { 191 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On full power on 192 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 193 while (digitalRead(sw1) == LOW) {}; 194 attachInterrupt(0, SW_ISR, CHANGE); 195 delay (3600000); //60 mins 196 asm volatile (" jmp 0"); 197 } 198 ADCSRA = 135; //enable analog digi 199 attachInterrupt(0, SW_ISR, CHANGE); 200 delay (100); 201 while (1) { 202 vSol = analogRead (SolPanel); 203 //Serial.println(vSol); 204 if (vSol > 600) { //no sun 205 asm volatile (" jmp 0"); 206 } 207 delay (500); 208 } 209 break; 210 //////////////////////////////////////////// 211 /// White Flaing with Sun /// 212 //////////////////////////////////////////// 213 case 5: 214 attachInterrupt(0, SW_ISR, CHANGE); 215 while (1) { 216 ADCSRA = 135; //enable analog digi 217 delay (100); 218 vSol = analogRead (SolPanel); 219 if (vSol < 600) { //Volts or no sun 220 digitalWrite(LED_White, HIGH); 221 delay(20);// mSec 222 digitalWrite(LED_White, LOW); 223 } 224 pause (2000);// msec 225 } 226 break; 227 //////////////////////////////////////////// 228 /// B A T T E R Y /// 229 //////////////////////////////////////////// 230 case 6: 231 ADCSRA = 135; //enable analog digi 232 delay (100); 233 attachInterrupt(0, SW_ISR, CHANGE); 234 timer = 0; 235 while (digitalRead(sw1) == HIGH && timer < 4) //stop after n times 236 { 237 float p = vcc.Read_Perc(VccMin, VccMax); 238 //Serial.print (p); 239 //Serial.println ("%"); 240 V10 = int(p) / 10; 241 for (int v = 0; v < V10; v++) 242 { 243 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 244 delay (10); 245 pinMode (LED_Green1, INPUT); // High Impedence LED Off 246 delay (650); 247 } 248 for (int v = V10; v < 10; v++) 249 { 250 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 251 delay (10); 252 pinMode (LED_Red1, INPUT); // High Impedence LED Off 253 delay (650); 254 } 255 delay (2000); 256 timer++; 257 } 258 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 259 while (digitalRead(sw1) == LOW) {}; delay(250); 260 asm volatile (" jmp 0"); 261 break; 262 } 263 264} 265 266//////////////////////////////////////////////////////// 267void dot() 268//////////////////////////////////////////////////////// 269{ 270 analogWrite(LED_White, 160); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 271 delay(22); 272 digitalWrite(LED_White, LOW); 273 delay(500); 274} 275 276//////////////////////////////////////////////////////// 277void dash() 278//////////////////////////////////////////////////////// 279{ 280 analogWrite(LED_White, 20); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 281 delay(150); 282 digitalWrite(LED_White, LOW); 283 delay(800); 284} 285 286//////////////////////////////////////////////////////// 287// sleep Now // 288//////////////////////////////////////////////////////// 289void sleepNow() 290{ 291 // disable ADC 292 ADCSRA = 0; 293 set_sleep_mode(SLEEP_MODE_PWR_DOWN);//SLEEP_MODE_PWR_SAVE); 294 interrupts(); 295 attachInterrupt(0, pinInterrupt, HIGH); 296 sleep_enable(); 297 sleep_mode(); 298 sleep_disable(); 299} 300 301//////////////////////////////////////////////////////// 302void pinInterrupt() 303//////////////////////////////////////////////////////// 304{ 305 detachInterrupt(0); 306 attachInterrupt(0, pinInterrupt, HIGH); 307} 308//////////////////////////////////////////////////////// 309// Interrupt Service Routin // 310//////////////////////////////////////////////////////// 311void SW_ISR() { 312 analogWrite(LED_White, 0); 313 MCUCR = bit (BODS) | bit (BODSE); // turn off brown-out enable in software 314 MCUCR = bit (BODS); 315 pinMode (LED_Green1, INPUT); // High Impedence LED Off 316 pinMode (LED_Green2, INPUT); // High Impedence LED Off 317 pinMode (LED_Red1, INPUT); // High Impedence LED Off 318 pinMode (LED_Red2, INPUT); // High Impedence LED Off 319 //pinMode(LED_Green1, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 320 for (long x = 0; x < 20000; x ++) { 321 pinMode (LED_Red1, INPUT); // delay 322 } 323 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 324 325 while (digitalRead(sw1) == LOW) {}; 326 delay(250); 327 asm volatile (" jmp 0"); 328} 329 330 331///////////////////////////////////////////////////////////////////// 332// P A U S E and S L E E P // 333///////////////////////////////////////////////////////////////////// 334void pause(int WDPset) 335{ 336 // disable ADC 337 ADCSRA = 0; 338 // clear various "reset" flags 339 MCUSR = 0; 340 // allow changes, disable reset 341 WDTCSR = bit (WDCE) | bit (WDE); 342 // set interrupt mode and an interval 343 switch (WDPset) { 344 case 125: 345 WDTCSR = bit (WDIE) | bit (WDP1) | bit (WDP0); // set WDIE, and .125 second delay 346 break; 347 case 250: 348 WDTCSR = bit (WDIE) | bit (WDP2); // set WDIE, and .25 second delay 349 break; 350 case 500: 351 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP0); // set WDIE, and .5 second delay 352 break; 353 case 1000: 354 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1); // set WDIE, and 1 second delay 355 break; 356 case 2000: 357 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0); // set WDIE, and 2 second delay 358 break; 359 case 4000: 360 WDTCSR = bit (WDIE) | bit (WDP3) ; // set WDIE, and 4 second delay 361 break; 362 case 8000: 363 WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 second delay 364 break; 365 } 366 wdt_reset(); // pat the dog 367 368 set_sleep_mode (SLEEP_MODE_PWR_DOWN); 369 noInterrupts (); // timed sequence follows 370 sleep_enable(); 371 372 // turn off brown-out enable in software 373 MCUCR = bit (BODS) | bit (BODSE); 374 MCUCR = bit (BODS); 375 interrupts (); // guarantees next instruction executed 376 sleep_cpu (); 377 378 // cancel sleep as a precaution 379 sleep_disable(); 380} 381ISR (WDT_vect) 382{ 383 wdt_disable(); // disable watchdog 384} // end of WDT_vect 385 386 387 388 389 390 391
Norsak_11
arduino
1// By Christian Roy 2// Norsak LED controler 3// rev_09 4//#include <avr/sleep.h> 5 6#include <avr/power.h> 7#include <avr/sleep.h> 8#include <Vcc.h> 9#include <avr/wdt.h> 10 11byte sw1 = 2 ;//pin 12byte LED_White = 11;//pin 13byte LED_Red1 = 6;//pin 14byte LED_Red2 = 7;//pin 15byte LED_Green1 = 5;//pin 16byte LED_Green2 = 4;//pin 17byte Read_Batt = 3;//pin 18float LED_W_Max; 19const float VccMin = 2.7; // Minimum expected Vcc level, in Volts. 20const float VccMax = 4.2; // Maximum expected Vcc level, in Volts. 21const float VccCorrection = 1.0 / 1.0; // Measured Vcc by multimeter divided by reported Vcc 22Vcc vcc(VccCorrection); 23byte LED_W_pwr; 24boolean dimmer = false ; 25int timer; 26int Tsel; 27byte select; 28int batt = 0 ; //pin 29int SolPanel = 1; //pin 30float vSol; // voltage at the solar panel 31float V10; 32boolean RedGreen = false; 33 34/////////////////////////////////////////////////////////////// 35void setup() 36/////////////////////////////////////////////////////////////// 37{ 38 analogReference(INTERNAL);//equal to 1.1 volts on ATmega328P 39 //pinMode(LED_BUILTIN, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 40 pinMode(sw1, INPUT_PULLUP); 41 pinMode(LED_White, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 42 analogWrite(LED_White, 0); //0 = OFF & 255 ON 43 pinMode (LED_Red1, INPUT); // High Impedence LED Off 44 pinMode (LED_Red2, INPUT); // High Impedence LED Off 45 pinMode (LED_Green1, INPUT); // High Impedence LED Off 46 pinMode (LED_Green2, INPUT); // High Impedence LED Off 47 //digitalWrite(LED_BUILTIN, LOW); 48 49 50 Serial.begin(9600); 51 attachInterrupt(0, SW_ISR, CHANGE); 52 /* 53 The 5 different modes are: 54 SLEEP_MODE_IDLE -the least power savings 55 SLEEP_MODE_ADC 56 SLEEP_MODE_PWR_SAVE 57 SLEEP_MODE_STANDBY 58 SLEEP_MODE_PWR_DOWN -the most power savings 59 */ 60} 61 62void loop() { 63 /* 64 pinMode(LED_Red1, INPUT_PULLUP); 65 pinMode(LED_Red2, INPUT); // High Impedence LED Off 66 pinMode (LED_Red1,OUTPUT); // LED On 67 delay (5000); 68 pinMode(LED_Red1, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 69 delay (5000); 70 */ 71 72 /////////////////////////////////////////////////////////////// 73 sleepNow(); 74 /////////////////////////////////////////////////////////////// 75 76 Tsel = 0; 77 select = 0;// SELECT 78 79 while (digitalRead(sw1) == LOW) 80 { 81 select ++; 82 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On LED On 83 delay (12); 84 pinMode (LED_Green1, INPUT); // High Impedence LED Off 85 delay (600); 86 } 87 88 delay (750); 89 switch (select) 90 { 91 //////////////////////////////////////////// 92 /// Green RED /// 93 //////////////////////////////////////////// 94 case 1: 95 attachInterrupt(0, SW_ISR, CHANGE); 96 while (1) 97 { 98 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 99 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 100 delay (50); 101 pinMode (LED_Red1, INPUT); // High Impedence LED Off 102 pinMode (LED_Red2, INPUT); // High Impedence LED Off 103 delay (450); 104 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 105 pinMode (LED_Green2, OUTPUT); digitalWrite (LED_Green2, LOW); // LED On 106 delay (50); 107 pinMode (LED_Green1, INPUT); // High Impedence LED Off 108 pinMode (LED_Green2, INPUT); // High Impedence LED Off 109 delay (450); 110 } 111 break; 112 //////////////////////////////////////////// 113 /// WHITE LED /// 114 //////////////////////////////////////////// 115 case 2: 116 LED_W_pwr = 2;// minimum led white 117 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 118 delay (2510); //waitnig for power LED up 119 while (digitalRead(sw1) == LOW) 120 { 121 LED_W_pwr += 1; 122 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 123 delay(50); 124 if (LED_W_pwr > 240)// max led power 125 { 126 analogWrite(LED_White, 0); 127 delay (500); 128 analogWrite(LED_White, 240); // max led power 129 delay (500); 130 while (digitalRead(sw1) == LOW) {}; 131 break; 132 } 133 } 134 delay (300); 135 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 136 delay (30); 137 pinMode (LED_Green1, INPUT); // High Impedence LED Off 138 delay (500); 139 for (int t = 0; t < 100; t++) { //2 sec 140 if (digitalRead(sw1) == LOW) { 141 // full power on 142 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 143 dimmer = true; 144 while (digitalRead(sw1) == LOW) {}; 145 delay (25); 146 pinMode (LED_Green1, INPUT); // High Impedence LED Off 147 } 148 delay (20); 149 } 150 if (dimmer == true) { 151 float W_LED_Dim ; 152 W_LED_Dim = (float)LED_W_pwr / 360; 153 attachInterrupt(0, SW_ISR, CHANGE); 154 for (float LED_W_pwrf = LED_W_pwr ; LED_W_pwrf > 0; LED_W_pwrf = LED_W_pwrf - W_LED_Dim) {// 360 = 30 min. 155 delay (5000);//5sec 156 analogWrite(LED_White, LED_W_pwrf); 157 } 158 } 159 else 160 { 161 attachInterrupt(0, SW_ISR, CHANGE); 162 delay (3600000); //mSec 163 } 164 asm volatile (" jmp 0"); 165 break; 166 //////////////////////////////////////////// 167 /// S O S /// 168 //////////////////////////////////////////// 169 case 3: 170 ////Serial.println("SOS"); 171 attachInterrupt(0, SW_ISR, CHANGE); 172 while (1) 173 { 174 dot(); dot(); dot(); // S 175 delay(700); 176 dash(); dash(); dash(); // O 177 delay(700); 178 dot(); dot(); dot(); // S 179 delay(6000); 180 } 181 break; 182 //////////////////////////////////////////// 183 /// LED RED /// 184 //////////////////////////////////////////// 185 case 4: 186 pinMode(LED_Red2, INPUT); // High Impedence LED Off 187 pinMode (LED_Red1, OUTPUT);//digitalWrite (LED_Red1,LOW); // LED On 188 analogWrite(LED_Red1, 235);// 15%, 255 = OFF & 0 ON 189 delay (1500); 190 if (digitalRead(sw1) == LOW) { 191 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On full power on 192 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 193 while (digitalRead(sw1) == LOW) {}; 194 attachInterrupt(0, SW_ISR, CHANGE); 195 delay (3600000); //60 mins 196 asm volatile (" jmp 0"); 197 } 198 ADCSRA = 135; //enable analog digi 199 attachInterrupt(0, SW_ISR, CHANGE); 200 delay (100); 201 while (1) { 202 vSol = analogRead (SolPanel); 203 //Serial.println(vSol); 204 if (vSol > 600) { //no sun 205 asm volatile (" jmp 0"); 206 } 207 delay (500); 208 } 209 break; 210 //////////////////////////////////////////// 211 /// White Flaing with Sun /// 212 //////////////////////////////////////////// 213 case 5: 214 attachInterrupt(0, SW_ISR, CHANGE); 215 while (1) { 216 ADCSRA = 135; //enable analog digi 217 delay (100); 218 vSol = analogRead (SolPanel); 219 if (vSol < 600) { //Volts or no sun 220 digitalWrite(LED_White, HIGH); 221 delay(20);// mSec 222 digitalWrite(LED_White, LOW); 223 } 224 pause (2000);// msec 225 } 226 break; 227 //////////////////////////////////////////// 228 /// B A T T E R Y /// 229 //////////////////////////////////////////// 230 case 6: 231 ADCSRA = 135; //enable analog digi 232 delay (100); 233 attachInterrupt(0, SW_ISR, CHANGE); 234 timer = 0; 235 while (digitalRead(sw1) == HIGH && timer < 4) //stop after n times 236 { 237 float p = vcc.Read_Perc(VccMin, VccMax); 238 //Serial.print (p); 239 //Serial.println ("%"); 240 V10 = int(p) / 10; 241 for (int v = 0; v < V10; v++) 242 { 243 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 244 delay (10); 245 pinMode (LED_Green1, INPUT); // High Impedence LED Off 246 delay (650); 247 } 248 for (int v = V10; v < 10; v++) 249 { 250 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 251 delay (10); 252 pinMode (LED_Red1, INPUT); // High Impedence LED Off 253 delay (650); 254 } 255 delay (2000); 256 timer++; 257 } 258 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 259 while (digitalRead(sw1) == LOW) {}; delay(250); 260 asm volatile (" jmp 0"); 261 break; 262 } 263 264} 265 266//////////////////////////////////////////////////////// 267void dot() 268//////////////////////////////////////////////////////// 269{ 270 analogWrite(LED_White, 160); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 271 delay(22); 272 digitalWrite(LED_White, LOW); 273 delay(500); 274} 275 276//////////////////////////////////////////////////////// 277void dash() 278//////////////////////////////////////////////////////// 279{ 280 analogWrite(LED_White, 20); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 281 delay(150); 282 digitalWrite(LED_White, LOW); 283 delay(800); 284} 285 286//////////////////////////////////////////////////////// 287// sleep Now // 288//////////////////////////////////////////////////////// 289void sleepNow() 290{ 291 // disable ADC 292 ADCSRA = 0; 293 set_sleep_mode(SLEEP_MODE_PWR_DOWN);//SLEEP_MODE_PWR_SAVE); 294 interrupts(); 295 attachInterrupt(0, pinInterrupt, HIGH); 296 sleep_enable(); 297 sleep_mode(); 298 sleep_disable(); 299} 300 301//////////////////////////////////////////////////////// 302void pinInterrupt() 303//////////////////////////////////////////////////////// 304{ 305 detachInterrupt(0); 306 attachInterrupt(0, pinInterrupt, HIGH); 307} 308//////////////////////////////////////////////////////// 309// Interrupt Service Routin // 310//////////////////////////////////////////////////////// 311void SW_ISR() { 312 analogWrite(LED_White, 0); 313 MCUCR = bit (BODS) | bit (BODSE); // turn off brown-out enable in software 314 MCUCR = bit (BODS); 315 pinMode (LED_Green1, INPUT); // High Impedence LED Off 316 pinMode (LED_Green2, INPUT); // High Impedence LED Off 317 pinMode (LED_Red1, INPUT); // High Impedence LED Off 318 pinMode (LED_Red2, INPUT); // High Impedence LED Off 319 //pinMode(LED_Green1, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 320 for (long x = 0; x < 20000; x ++) { 321 pinMode (LED_Red1, INPUT); // delay 322 } 323 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 324 325 while (digitalRead(sw1) == LOW) {}; 326 delay(250); 327 asm volatile (" jmp 0"); 328} 329 330 331///////////////////////////////////////////////////////////////////// 332// P A U S E and S L E E P // 333///////////////////////////////////////////////////////////////////// 334void pause(int WDPset) 335{ 336 // disable ADC 337 ADCSRA = 0; 338 // clear various "reset" flags 339 MCUSR = 0; 340 // allow changes, disable reset 341 WDTCSR = bit (WDCE) | bit (WDE); 342 // set interrupt mode and an interval 343 switch (WDPset) { 344 case 125: 345 WDTCSR = bit (WDIE) | bit (WDP1) | bit (WDP0); // set WDIE, and .125 second delay 346 break; 347 case 250: 348 WDTCSR = bit (WDIE) | bit (WDP2); // set WDIE, and .25 second delay 349 break; 350 case 500: 351 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP0); // set WDIE, and .5 second delay 352 break; 353 case 1000: 354 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1); // set WDIE, and 1 second delay 355 break; 356 case 2000: 357 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0); // set WDIE, and 2 second delay 358 break; 359 case 4000: 360 WDTCSR = bit (WDIE) | bit (WDP3) ; // set WDIE, and 4 second delay 361 break; 362 case 8000: 363 WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 second delay 364 break; 365 } 366 wdt_reset(); // pat the dog 367 368 set_sleep_mode (SLEEP_MODE_PWR_DOWN); 369 noInterrupts (); // timed sequence follows 370 sleep_enable(); 371 372 // turn off brown-out enable in software 373 MCUCR = bit (BODS) | bit (BODSE); 374 MCUCR = bit (BODS); 375 interrupts (); // guarantees next instruction executed 376 sleep_cpu (); 377 378 // cancel sleep as a precaution 379 sleep_disable(); 380} 381ISR (WDT_vect) 382{ 383 wdt_disable(); // disable watchdog 384} // end of WDT_vect 385 386 387 388 389 390 391
Flash Light contrecoller Norsak_12
arduino
small change, white led 5 min dimming down after 60 min
1// By Christian Roy 2// Norsak LED controler 3// rev_09 4//#include <avr/sleep.h> 5 6#include <avr/power.h> 7#include <avr/sleep.h> 8#include <Vcc.h> 9#include <avr/wdt.h> 10 11byte sw1 = 2 ;//pin 12byte LED_White = 11;//pin 13byte LED_Red1 = 6;//pin 14byte LED_Red2 = 7;//pin 15byte LED_Green1 = 5;//pin 16byte LED_Green2 = 4;//pin 17byte Read_Batt = 3;//pin 18float LED_W_Max; 19const float VccMin = 2.7; // Minimum expected Vcc level, in Volts. 20const float VccMax = 4.2; // Maximum expected Vcc level, in Volts. 21const float VccCorrection = 1.0 / 1.0; // Measured Vcc by multimeter divided by reported Vcc 22Vcc vcc(VccCorrection); 23byte LED_W_pwr; 24boolean dimmer = false ; 25int timer; 26int Tsel; 27byte select; 28int batt = 0 ; //pin 29int SolPanel = 1; //pin 30float vSol; // voltage at the solar panel 31float V10; 32boolean RedGreen = false; 33 34/////////////////////////////////////////////////////////////// 35void setup() 36/////////////////////////////////////////////////////////////// 37{ 38 analogReference(INTERNAL);//equal to 1.1 volts on ATmega328P 39 //pinMode(LED_BUILTIN, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 40 pinMode(sw1, INPUT_PULLUP); 41 pinMode(LED_White, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 42 analogWrite(LED_White, 0); //0 = OFF & 255 ON 43 pinMode (LED_Red1, INPUT); // High Impedence LED Off 44 pinMode (LED_Red2, INPUT); // High Impedence LED Off 45 pinMode (LED_Green1, INPUT); // High Impedence LED Off 46 pinMode (LED_Green2, INPUT); // High Impedence LED Off 47 //digitalWrite(LED_BUILTIN, LOW); 48 49 50 Serial.begin(9600); 51 attachInterrupt(0, SW_ISR, CHANGE); 52 /* 53 The 5 different modes are: 54 SLEEP_MODE_IDLE -the least power savings 55 SLEEP_MODE_ADC 56 SLEEP_MODE_PWR_SAVE 57 SLEEP_MODE_STANDBY 58 SLEEP_MODE_PWR_DOWN -the most power savings 59 */ 60} 61 62void loop() { 63 64 /////////////////////////////////////////////////////////////// 65 sleepNow(); 66 /////////////////////////////////////////////////////////////// 67 68 Tsel = 0; 69 select = 0;// SELECT 70 71 while (digitalRead(sw1) == LOW) 72 { 73 select ++; 74 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On LED On 75 delay (12); 76 pinMode (LED_Green1, INPUT); // High Impedence LED Off 77 delay (600); 78 } 79 80 delay (750); 81 switch (select) 82 { 83 //////////////////////////////////////////// 84 /// Green RED /// 85 //////////////////////////////////////////// 86 case 1: 87 attachInterrupt(0, SW_ISR, CHANGE); 88 while (1) 89 { 90 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 91 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 92 delay (50); 93 pinMode (LED_Red1, INPUT); // High Impedence LED Off 94 pinMode (LED_Red2, INPUT); // High Impedence LED Off 95 delay (450); 96 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 97 pinMode (LED_Green2, OUTPUT); digitalWrite (LED_Green2, LOW); // LED On 98 delay (50); 99 pinMode (LED_Green1, INPUT); // High Impedence LED Off 100 pinMode (LED_Green2, INPUT); // High Impedence LED Off 101 delay (450); 102 } 103 break; 104 105 106 //////////////////////////////////////////// 107 /// WHITE LED /// 108 //////////////////////////////////////////// 109 case 2: 110 LED_W_pwr = 2;// minimum led white 111 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 112 delay (2510); //waitnig for power LED up 113 while (digitalRead(sw1) == LOW) // adjust white LED power 114 { 115 LED_W_pwr += 1; 116 analogWrite(LED_White, LED_W_pwr); //0 = OFF & 255 ON 117 delay(50); 118 if (LED_W_pwr > 240)// max led power 119 { 120 analogWrite(LED_White, 0); 121 delay (500); 122 analogWrite(LED_White, 240); // max led power 123 delay (500); 124 while (digitalRead(sw1) == LOW) {}; 125 break; 126 } 127 } 128 attachInterrupt(0, SW_ISR, CHANGE); 129 delay (3600000); // 60 min 130 //diming off// 131 for (int LED_W_dimer = LED_W_pwr ; LED_W_dimer > 0; LED_W_dimer--) { // 360 = 30 min. 132 delay (300000/LED_W_pwr); 133 analogWrite(LED_White, LED_W_dimer); 134 } 135 asm volatile (" jmp 0"); 136 break; 137 138 139 //////////////////////////////////////////// 140 /// S O S /// 141 //////////////////////////////////////////// 142 case 3: 143 ////Serial.println("SOS"); 144 attachInterrupt(0, SW_ISR, CHANGE); 145 while (1) 146 { 147 dot(); dot(); dot(); // S 148 delay(700); 149 dash(); dash(); dash(); // O 150 delay(700); 151 dot(); dot(); dot(); // S 152 delay(6000); 153 } 154 break; 155 156 157 //////////////////////////////////////////// 158 /// LED RED /// 159 //////////////////////////////////////////// 160 case 4: 161 pinMode(LED_Red2, INPUT); // High Impedence LED Off 162 pinMode (LED_Red1, OUTPUT);//digitalWrite (LED_Red1,LOW); // LED On 163 analogWrite(LED_Red1, 235);// 15%, 255 = OFF & 0 ON 164 delay (2000); 165 if (digitalRead(sw1) == LOW) { 166 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On full power on 167 pinMode (LED_Red2, OUTPUT); digitalWrite (LED_Red2, LOW); // LED On 168 while (digitalRead(sw1) == LOW) {}; 169 attachInterrupt(0, SW_ISR, CHANGE); 170 delay (3600000); //60 mins 171 asm volatile (" jmp 0"); 172 } 173 ADCSRA = 135; //enable analog digi 174 attachInterrupt(0, SW_ISR, CHANGE); 175 delay (100); 176 while (1) { 177 vSol = analogRead (SolPanel); 178 //Serial.println(vSol); 179 if (vSol > 600) { //no sun 180 asm volatile (" jmp 0"); 181 } 182 delay (500); 183 } 184 break; 185 186 187 //////////////////////////////////////////// 188 /// White Flashing with night /// 189 //////////////////////////////////////////// 190 case 5: 191 attachInterrupt(0, SW_ISR, CHANGE); 192 while (1) { 193 ADCSRA = 135; //enable analog digi 194 delay (100); 195 vSol = analogRead (SolPanel); 196 if (vSol < 600) { //Volts or no sun 197 digitalWrite(LED_White, HIGH); 198 delay(20);// mSec 199 digitalWrite(LED_White, LOW); 200 } 201 pause (2000);// msec 202 } 203 break; 204 205 206 //////////////////////////////////////////// 207 /// B A T T E R Y level /// 208 //////////////////////////////////////////// 209 case 6: 210 ADCSRA = 135; //enable analog digi 211 delay (100); 212 attachInterrupt(0, SW_ISR, CHANGE); 213 timer = 0; 214 while (digitalRead(sw1) == HIGH && timer < 4) //stop after n times 215 { 216 float p = vcc.Read_Perc(VccMin, VccMax); 217 //Serial.print (p); 218 //Serial.println ("%"); 219 V10 = int(p) / 10; 220 for (int v = 0; v < V10; v++) 221 { 222 pinMode (LED_Green1, OUTPUT); digitalWrite (LED_Green1, LOW); // LED On 223 delay (10); 224 pinMode (LED_Green1, INPUT); // High Impedence LED Off 225 delay (650); 226 } 227 for (int v = V10; v < 10; v++) 228 { 229 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 230 delay (10); 231 pinMode (LED_Red1, INPUT); // High Impedence LED Off 232 delay (650); 233 } 234 delay (2000); 235 timer++; 236 } 237 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 238 while (digitalRead(sw1) == LOW) {}; delay(250); 239 asm volatile (" jmp 0"); 240 break; 241 } 242 243} 244 245//////////////////////////////////////////////////////// 246void dot() 247//////////////////////////////////////////////////////// 248{ 249 analogWrite(LED_White, 160); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 250 delay(22); 251 digitalWrite(LED_White, LOW); 252 delay(500); 253} 254 255//////////////////////////////////////////////////////// 256void dash() 257//////////////////////////////////////////////////////// 258{ 259 analogWrite(LED_White, 20); //0 = OFF & 255 ONdigitalWrite(LED_White, HIGH); 260 delay(150); 261 digitalWrite(LED_White, LOW); 262 delay(800); 263} 264 265//////////////////////////////////////////////////////// 266// sleep Now // 267//////////////////////////////////////////////////////// 268void sleepNow() 269{ 270 // disable ADC 271 ADCSRA = 0; 272 set_sleep_mode(SLEEP_MODE_PWR_DOWN);//SLEEP_MODE_PWR_SAVE); 273 interrupts(); 274 attachInterrupt(0, pinInterrupt, HIGH); 275 sleep_enable(); 276 sleep_mode(); 277 sleep_disable(); 278} 279 280//////////////////////////////////////////////////////// 281void pinInterrupt() 282//////////////////////////////////////////////////////// 283{ 284 detachInterrupt(0); 285 attachInterrupt(0, pinInterrupt, HIGH); 286} 287//////////////////////////////////////////////////////// 288// Interrupt Service Routin // 289//////////////////////////////////////////////////////// 290void SW_ISR() { 291 analogWrite(LED_White, 0); 292 MCUCR = bit (BODS) | bit (BODSE); // turn off brown-out enable in software 293 MCUCR = bit (BODS); 294 pinMode (LED_Green1, INPUT); // High Impedence LED Off 295 pinMode (LED_Green2, INPUT); // High Impedence LED Off 296 pinMode (LED_Red1, INPUT); // High Impedence LED Off 297 pinMode (LED_Red2, INPUT); // High Impedence LED Off 298 //pinMode(LED_Green1, OUTPUT); digitalWrite (LED_Red1,LOW); // LED On 299 for (long x = 0; x < 20000; x ++) { 300 pinMode (LED_Red1, INPUT); // delay 301 } 302 pinMode (LED_Red1, OUTPUT); digitalWrite (LED_Red1, LOW); // LED On 303 304 while (digitalRead(sw1) == LOW) {}; 305 delay(250); 306 asm volatile (" jmp 0"); 307} 308 309 310///////////////////////////////////////////////////////////////////// 311// S L E E P with delay // 312///////////////////////////////////////////////////////////////////// 313void pause(int WDPset) 314{ 315 // disable ADC 316 ADCSRA = 0; 317 // clear various "reset" flags 318 MCUSR = 0; 319 // allow changes, disable reset 320 WDTCSR = bit (WDCE) | bit (WDE); 321 // set interrupt mode and an interval 322 switch (WDPset) { 323 case 125: 324 WDTCSR = bit (WDIE) | bit (WDP1) | bit (WDP0); // set WDIE, and .125 second delay 325 break; 326 case 250: 327 WDTCSR = bit (WDIE) | bit (WDP2); // set WDIE, and .25 second delay 328 break; 329 case 500: 330 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP0); // set WDIE, and .5 second delay 331 break; 332 case 1000: 333 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1); // set WDIE, and 1 second delay 334 break; 335 case 2000: 336 WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0); // set WDIE, and 2 second delay 337 break; 338 case 4000: 339 WDTCSR = bit (WDIE) | bit (WDP3) ; // set WDIE, and 4 second delay 340 break; 341 case 8000: 342 WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 second delay 343 break; 344 } 345 wdt_reset(); // pat the dog 346 347 set_sleep_mode (SLEEP_MODE_PWR_DOWN); 348 noInterrupts (); // timed sequence follows 349 sleep_enable(); 350 351 // turn off brown-out enable in software 352 MCUCR = bit (BODS) | bit (BODSE); 353 MCUCR = bit (BODS); 354 interrupts (); // guarantees next instruction executed 355 sleep_cpu (); 356 357 // cancel sleep as a precaution 358 sleep_disable(); 359} 360ISR (WDT_vect) 361{ 362 wdt_disable(); // disable watchdog 363} // end of WDT_vect 364 365 366 367 368 369 370
Downloadable files
Norsak 1 LED Controller
Norsak 1 LED Controller
Comments
Only logged in users can leave comments
Anonymous user
2 years ago
Where is the code for this? Unable to download.
Anonymous user
2 years ago
My dad was interested in this, but the code is missing.
Chrisroy
2 years ago
If you want be contribute let me know. New mode idea is welcome
Chrisroy
2 years ago
I will check
Anonymous user
3 years ago
Interesting project! Can´t see the Fritzing schematics due to missing parts. Dio you have the schematics in some other program? Why flashing lantern? At least in Sweden and northern waters we don´t allow red,green or white lanterns to flash.
Anonymous user
7 years ago
My dad was interested in this, but the code is missing.
Chrisroy
2 years ago
If you want be contribute let me know. New mode idea is welcome
Chrisroy
2 years ago
I will check
Anonymous user
7 years ago
Where is the code for this? Unable to download.
Chrisroy
7 years ago
The ultimate Flash Light Controller!
Chrisroy
2 years ago
The ultimate Flash Light Controller!