//------------------------------------------------------------------- // Useless Light Box // 2024.10.03 RSP // Target: Pro Mini 5V/16MHz // // Deploys and illuminates a 5V USB bulb with random misbehaving //------------------------------------------------------------------- #include Servo myservo; #define BTN1_PIN 2 #define LAMP_PIN 5 // PWM #define SERVO_PIN 9 #define SERVO_RETRACT 96 #define SERVO_DEPLOY 0 #define DEBOUNCE_MSEC 50 byte fade[100] = { 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, 11, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 29, 30, 32, 34, 36, 38, 40, 43, 45, 48, 50, 53, 56, 60, 63, 67, 70, 75, 79, 83, 88, 93, 99, 104, 110, 117, 123, 130, 138, 146, 154, 163, 173, 183, 193, 204, 216, 228, 242, 255 }; //------------------------------------------------------------------- void setup() { pinMode( BTN1_PIN, INPUT_PULLUP ); pinMode( 3, OUTPUT ); digitalWrite( 3, LOW ); // provide ground for pin 2 pinMode( LAMP_PIN, OUTPUT ); digitalWrite( LAMP_PIN, LOW ); // lamp off myservo.attach(SERVO_PIN); myservo.write(SERVO_RETRACT); } void loop() { // wait for button while (digitalRead(BTN1_PIN)) random(); // pick a startup switch (random(0,3)) { case 1: // ED myservo.write(SERVO_DEPLOY); delay(1000); myservo.write(SERVO_RETRACT); delay(250); myservo.write(SERVO_DEPLOY); delay(1000); myservo.write(SERVO_RETRACT); delay(250); case 2: // fast myservo.write(SERVO_DEPLOY); delay(250); digitalWrite(LAMP_PIN,HIGH); break; default: // turn on normally for (int i=SERVO_RETRACT; i >= SERVO_DEPLOY; i--) { myservo.write(i); delay(10); } for (int i=0; i < 100; i++) { analogWrite(LAMP_PIN,fade[i]); delay(10); } } for (uint32_t tm=millis(); millis()-tm < DEBOUNCE_MSEC; ) if (!digitalRead(BTN1_PIN)) tm = millis(); // wait for button while (digitalRead(BTN1_PIN)) random(); // pick a shutdown switch (random(0,3)) { case 1: // shake it off myservo.write(SERVO_RETRACT); delay(1000); myservo.write(SERVO_DEPLOY); delay(250); myservo.write(SERVO_RETRACT); delay(1000); myservo.write(SERVO_DEPLOY); delay(250); case 2: // fast off myservo.write(SERVO_RETRACT); digitalWrite(LAMP_PIN,LOW); break; default: // turn off normally for (int i=99; i >= 0; i--) { analogWrite(LAMP_PIN,fade[i]); delay(10); } for (int i=SERVO_DEPLOY; i <= SERVO_RETRACT; i++) { myservo.write(i); delay(10); } } for (uint32_t tm=millis(); millis()-tm < DEBOUNCE_MSEC; ) if (!digitalRead(BTN1_PIN)) tm = millis(); }