brziIgor Putnik
Published © GPL3+

Useless box

Simple useless box with Arduino and LEDs for mood effect.

BeginnerShowcase (no instructions)15 hours18,558
Useless box

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Servos (Tower Pro MG996R)
×1
24-bit rgb led driver board
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

Electronics shcematic

sheme_IIgUVOWynA.fzz

Code

Arduino code V2

C/C++
Sequential animations, resets after 10 seconds of inactivity. Made mainly for faires. Light flicker fixed.
#include <VarSpeedServo.h>
#include <FastLED.h>

#define DATA_PIN 3
#define switchpin 2
#define servopin 11

#define NUM_LEDS 24

uint8_t i;
 
VarSpeedServo lid; 
 
CRGB leds[NUM_LEDS];

void setup() {
 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); 
 pinMode(switchpin, INPUT_PULLUP); 
 setPwmFrequency(DATA_PIN, 1); //Reduces LED flicker
 for(i = 0; i < 24; i++) leds[i] = CRGB::Green;
 FastLED.setBrightness(255);
 FastLED.show();
 lid.attach(servopin);
 closelid(255);
}

void(* resetFunc) (void) = 0;
 
void loop() { 
  while(digitalRead(switchpin));
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(255);
  anim(255, 65, 5);
  resetck();
  while(digitalRead(switchpin));
  openlid(40);
  delay(800);
  closelid(255);
  anim(80, 45, 15);
  resetck();
  while(digitalRead(switchpin));
  FastLED.setBrightness(45);
  FastLED.show();
  delay(450);
  FastLED.setBrightness(0);
  FastLED.show();
  delay(1000);
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(255);
  delay(500);
  for(i = 0; i < 24; i++) leds[i] = CRGB::Yellow;
  FastLED.setBrightness(255);
  FastLED.show();
  resetck();
  while(digitalRead(switchpin));
  lid.write(95,25,true);
  delay(950);
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(255);
  resetck();
  anim(130, 80, 0);
  while(digitalRead(switchpin));
  delay(1500);
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(150);
  for(i = 0; i < 24; i++) leds[i] = CRGB::Orange;
  FastLED.setBrightness(80);
  FastLED.show();
  resetck();
  while(digitalRead(switchpin));
  lid.write(110,255,true);
  delay(800);
  openlid(20);
  delay(800);
  lid.write(110,25,true);
  delay(800);
  FastLED.setBrightness(40);
  FastLED.show();
  delay(800);
  closelid(255);
  resetck();
  while(digitalRead(switchpin));
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(255);
  anim(40, 0, 15);
  for(i = 0; i < 24; i++) leds[i] = CRGB::Red;
  animp(0, 40, 15);
  resetck();
  while(digitalRead(switchpin));
  delay(1500);
  FastLED.setBrightness(0);
  FastLED.show();
  delay(1250);
  FastLED.setBrightness(100);
  FastLED.show();
  openlid(255);
  delay(1500);
  closelid(255);
  resetck();
  while(digitalRead(switchpin));
  delay(1500);
  FastLED.setBrightness(255);
  FastLED.show();
  delay(850);
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  lid.write(90, 10, true);
  delay(800);
  openlid(255);
  delay(250);
  lid.write(90, 10, true);
  delay(800);
  openlid(255);
  delay(1000);
  closelid(255);
  resetck();
  while(digitalRead(switchpin));
  delay(850);
  for(i = 0; i < 24; i++) leds[i] = CRGB::Green;
  FastLED.show();
  delay(1500);
  for(i = 0; i < 24; i++) leds[i] = CRGB::Red;
  openlid(255);
  FastLED.show();
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(10);
  resetck();
  while(digitalRead(switchpin));
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(255);
  delay(800);
  resetck();
  while(digitalRead(switchpin));
  delay(250);
  FastLED.setBrightness(100);
  FastLED.show();
  delay(850);
  openlid(255);
  while(!digitalRead(switchpin));
  delay(300);
  closelid(255);
  resetck();
  while(digitalRead(switchpin));
  delay(250);
  anim(100, 23, 20);
  openlid(45);
  delay(1250);
  closelid(45);
  delay(1250);
  resetck();
  while(digitalRead(switchpin));
  openlid(10);
  delay(1500);
  anim(23, 0, 20);
  delay(1500);
  closelid(10);
  delay(1500);
  for(i = 0; i < 24; i++) leds[i] = CRGB::Green;
  FastLED.setBrightness(25);
  FastLED.show();
  delay(650);
  FastLED.setBrightness(80);
  FastLED.show();
  delay(650);
  FastLED.setBrightness(140);
  FastLED.show();
  delay(650);
  FastLED.setBrightness(170);
  FastLED.show();
  delay(650);
  FastLED.setBrightness(210);
  FastLED.show();
  delay(650);
  FastLED.setBrightness(255);
  FastLED.show();
  delay(650);
  openlid(255);
  delay(1000);
  closelid(255);
 }


void closelid(byte recv){ 
  lid.write(170,recv,true);
}
 
void openlid(byte recv){ 
  lid.write(65,recv,true);
}

void anim(byte st, byte en, byte d){
  for(i = st; i != en; i--){
    FastLED.setBrightness(i);
    FastLED.show();
    delay(d); 
  }
}

void animp(byte st, byte en, byte d){
  for(i = st; i != en; i++){
    FastLED.setBrightness(i);
    FastLED.show();
    delay(d); 
  }
}

void resetck(){
  unsigned long lastmillis = millis();
  while(digitalRead(switchpin)) if (millis() - lastmillis > 10000) resetFunc();
}

void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x07; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}

Arduino code

C/C++
"The brain of the box". The control() function is somewhat bugy, that's something i'm not interested in fixing any time soon. If you have any ideas for this, i'm happy to hear them.
#include <VarSpeedServo.h>
#include <FastLED.h>
 
#define NUM_LEDS 24 //there are 24 leds on the driver board
#define DATA_PIN 3 //which is connected to the pin 3

uint8_t mood=0; //variable for mood control
unsigned rnd; //used for randomly choosing animations
 
VarSpeedServo lid; //i named the servo lid
 
CRGB leds[NUM_LEDS];

void setup() {
 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  //applies to this driver board
 pinMode(12,INPUT_PULLUP); //switch is connected to the pin 12/GND
 lid.attach(9); //servo is connected to the pin 9
 closelid(15); //close the lid with speed 15 (see the function below)
 for(byte i=0;i<24;i++)leds[i] = CRGB::Blue; //set all leds to blue (in the buffer not yet shown)
 FastLED.show(); //switch to the new led buffer (displays previously set blue leds)
 while(digitalRead(12)); //do nothing until switch is flicked
 openlid(255); //open the lid with speed 255 (see the function below)
 delay(500); //essential delay until servo finishes its move
 closelid(255);
 for(byte i=255;i>0;i--){ //lower the brightness incrementally
  FastLED.setBrightness(i);
  FastLED.show();
  delay(5);
 }
 control(); //This function is used to control the "mood" of the box
}
 
 
 
 
void loop() {  
  if(!digitalRead(12)){
  rnd=(random(800)); //generates a random number and falls into one of the animations 
     if(rnd>=0 && rnd<50){
       openlid(150);
       delay(500);
       closelid(255);
       mood+=10;
     }
     if(rnd>=50 && rnd<75){
       lid.write(100,175,true);
       delay(500);
       lid.write(55,175,true);
       delay(500);
       openlid(255);
       delay(500);
       closelid(255);
       mood+=10;
     }
     if(rnd>=75 && rnd<100){
       lid.write(130,175,true);
       delay(500);
       lid.write(100,130,true);
       delay(500);
       lid.write(55,55,true);
       delay(500);
       closelid(75);
       mood+=10;
     }
     if(rnd>=100 && rnd<125){
       openlid(255);
       delay(750);
       closelid(50);
       mood+=15;
     }
     if(rnd>=125 && rnd<150){
       openlid(255);
       delay(500);
       lid.write(100,55,true);
       delay(1000);
       closelid(255);
       mood+=15;
     }
     if(rnd>=150 && rnd<175){
       delay(2000);
       openlid(255);
       delay(500);
       closelid(255);
       mood+=10;
     }
     if(rnd>=175 && rnd<200){
       openlid(255);
       delay(500);
       closelid(255);
       mood+=15;
     }
     if(rnd>=200 && rnd<225){
       lid.write(65,175,true);
       delay(500);
       openlid(255);
       delay(500);
       closelid(255);
       mood+=15;
     }
     if(rnd>=250 && rnd<275){
      lid.write(100,200,true);
      delay(750);
      lid.write(168,255,true);
      delay(500);
      closelid(200);
      mood+=15;
    }
      if(rnd>=225 && rnd<250){
    lid.write(168,200,true);
    delay(750);
    lid.write(0,50,true);
    delay(500);
    closelid(250);
    mood+=15;
    }
      if(rnd>=225 && rnd<250){  
    lid.write(100,255,true);
    delay(700);
    lid.write(130,235,true);
    delay(700);
    lid.write(150,120,true);
    delay(700);
    lid.write(168,100,true);
    delay(400);
    closelid(255);
    mood+=25;
  }
     if(rnd>=275 && rnd<300){
    lid.write(168,180,true);
    delay(2000);
    closelid(255);
    mood+=25;
    }
     if(rnd>=300 && rnd<325){
    lid.write(168,180,true);
    delay(400);
    lid.write(130,180,true);
    closelid(255);
    mood+=25;
    }
     if(rnd>=325 && rnd<350){
   openlid(150);
   delay(400);
   lid.write(140,255,true);
   delay(400);
   openlid(150);
   delay(400);
   mood+=25;
   closelid(255);
    }
   if(rnd>=350 && rnd<375){
    openlid(150);
    lid.write(140,255,true);
    closelid(255);
    mood+=25;
    }
   if(rnd>=375 && rnd<400){
    openlid(150);
    delay(1000);
    lid.write(50,255,true);
    delay(500);
    openlid(150);
    delay(1000);
    closelid(255);
    mood+=25;
    }
   if(rnd>=400 && rnd<450){
   openlid(250);
   lid.write(140,255);
   closelid(40);
   mood+=25;
 
    }
   if(rnd>=450 && rnd<475){
   lid.write(150,150,true);
   delay(500);
   openlid(255);
   delay(500);
   closelid(255);
   mood+=25;
 
    }
    if (rnd>=475 && rnd<500){
   openlid(250);
   delay(500);
   lid.write(140,255);
   delay(500);
   closelid(40);
   delay(500);
   mood+=25;
    }
   
  if(rnd>=500){
  openlid(255);
  delay(500);
  closelid(255);
  }
  control(); //Change the state of the LEDs
  }
 }

 uint8_t control(){ //Changes the LED color and gives it an animation depening on mood value
if(mood>=0 && mood<=15){
     for(byte i=0;i<24;i++)leds[i] = CRGB::Green;
      for(byte i=0;i<255;i++){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
      }
}
if (mood>=15 && mood<30){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Green;
      for(byte i=255;i>200;i--){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=30 && mood<60){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Green;
      for(byte i=200;i>130;i--){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=60 && mood<100){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Orange;
      for(byte i=0;i<130;i++){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=100 && mood<130){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Orange;
      for(byte i=130;i<200;i++){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=130 && mood<155){
   for(byte i=0;i<24;i++) leds[i] = CRGB::Orange;
      for(byte i=200;i<255;i++){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=155 && mood<180){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Orange;
      for(byte i=255;i>75;i--){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=180 && mood<200){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Orange;
      for(byte i=255;i>75;i--){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=200 && mood<225){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Red;
      for(byte i=75;i<180;i++){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }
if (mood>=225){
   for(byte i=0;i<24;i++)leds[i] = CRGB::Red;
      for(byte i=180;i<255;i++){
      FastLED.setBrightness(i);
      FastLED.show();
      delay(5);
    }
   }  
} //After mood hits overflow it goes back to zero, i never really was
 //specially interested in fixing this function

void closelid(byte recv){ //function for closing the lid
  lid.write(168,recv,true);
}
 
void openlid(byte recv){ //function for opening the lid
  lid.write(42,recv,true);
}

Credits

brzi

brzi

5 projects • 40 followers
Igor Putnik

Igor Putnik

2 projects • 9 followers

Comments