Panel Cookies
Noise Generator - Code
12/01/2024 | Views: 1732 | Arduino | by: ELECTRONOOBS      


And the circuit is complete and ready for a test . Now get the SD card and on your computer format it to FAT32. Inside, create a folder named mp3. Here copy all your noise files with names like this, 0001, 0002 and so on. I have these 3 files with different noise of a hair dryer, ventilator and hiss sound. (Create your own mp3 files if you want) Now remove the SD card and plug it into the DF player. Before we fix everything inside, let’s check the code. We import all the needed libraries which you could download from below. Defined the used pins for the buttons, LEDs and potentiometer. Each time a button is pressed, we change to the next or previous soundtrack. But we also store the track to the EEPROM. In that way, if I turn off the generator, it will later start up with the same song, which is supposed to be the favorite of your baby, right? With these lines we adjust the brightness of the LEDs. And with the third button, we change the color of the LEDs from white, red, blue and so on and also store that on the EEPROM as well. Upload the code and give it a test.





Noise Generator Code 12/01/2024

#include <SoftwareSerial.h>
#include <DFMiniMp3.h>                  //https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <EEPROM.h>
#include <IRremote.h>


//Inputs/Outputs
int brightness_pin = A0;
int next_button = 3;
int prev_button = 4;
int color_button = 5;
#define PIN        6 
int RECV_PIN = 7;


IRrecv irrecv(RECV_PIN);
decode_results results;

//Variables
uint16_t count = 0;
#define NUMPIXELS 25 
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 500
int saved_track = 2;
bool next_buttonState = true;
bool prev_buttonState = true;
bool color_buttonState = true;
int brightness = 0;
int prev_brightness = 0;
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
int actualColor = 1;
int colorAmount = 7;
int prevColor = 1;
int light_toggle = 0;
bool toggle_sound = false;
bool play_status = true;
unsigned int Actual_Millis, Previous_Millis;
int volume = 20;

class Mp3Notify{
public:
  static void OnError(uint16_t errorCode)
  {
    // see DfMp3_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }

  static void OnPlayFinished(uint16_t globalTrack)
  {
    Serial.println();
    Serial.print("Play finished for #");
    Serial.println(globalTrack);   
  }

  static void OnCardOnline(uint16_t code)
  {
    Serial.println();
    Serial.print("Card online ");
    Serial.println(code);     
  }

  static void OnCardInserted(uint16_t code)
  {
    Serial.println();
    Serial.print("Card inserted ");
    Serial.println(code); 
  }

  static void OnCardRemoved(uint16_t code)
  {
    Serial.println();
    Serial.print("Card removed ");
    Serial.println(code);  
  }
};

SoftwareSerial secondarySerial(10, 11); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);



void setup() 
{ 
  pinMode(next_button, INPUT_PULLUP);
  pinMode(prev_button, INPUT_PULLUP);
  pinMode(color_button,INPUT_PULLUP);

  Serial.begin(9600);  
  mp3.begin();
  uint16_t volume = mp3.getVolume();  
  mp3.setVolume(20);   //Set volume value (0~30).
  count = mp3.getTotalTrackCount();  
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels.clear(); // Set all pixel colors to 'off'

  saved_track = EEPROM.read(1);
  actualColor = EEPROM.read(2);
  prevColor = actualColor;
  delay(50);
  mp3.playMp3FolderTrack(1); 
  mp3.loop();
  
  irrecv.enableIRIn(); // Start the receiver

  delay(200);

  Actual_Millis = millis();               //Save time for refresh loop
  Previous_Millis = Actual_Millis; 

}

void waitMilliseconds(uint16_t msWait)
{
  uint32_t start = millis();  
  while ((millis() - start) < msWait)
  {
    // calling mp3.loop() periodically allows for notifications 
    // to be handled without interrupts
    mp3.loop(); 
    delay(1);
  }
}


void loop() 
{
  while(Serial.available()>0){
    char mystr = Serial.read();

    if (mystr == '1') {
      Serial.println("Button A");
      light_toggle++;
      if(light_toggle>6){
        light_toggle=0;
      }      
    }
    else if(mystr == '2') {
      Serial.println("Button B");
      light_toggle = 0;     
      set_led_brightness(0);
    }

    else if(mystr == '3') {
      Serial.println("Button C");
      volume--;
      if(volume < 0){
        volume = 0;
      }
      mp3.setVolume(volume);    
    }

    else if(mystr == '4') {
      Serial.println("Button D");
      volume++;
      if(volume > 24){
        volume = 24;
      }
      mp3.setVolume(volume);    
    }

    else if(mystr == '5') {
      Serial.println("Button E");
      toggle_sound = true;    
    }   
  }


   

  if(toggle_sound){
    if(play_status){
      play_status = false;
      mp3.pause(); 
    }
    else if(!play_status){
      play_status = true
      ;
      mp3.start(); 
    }
    toggle_sound = false;
  }

  //Play next track
  if(!digitalRead(next_button) && next_buttonState){
    next_buttonState = false;
    saved_track = EEPROM.read(1);
    saved_track++;
    if(saved_track > count){
      saved_track = 1;
    }
    EEPROM.update(1,saved_track);
    mp3.playMp3FolderTrack(1); 
    delay(100);
  }

  else if (digitalRead(next_button) && !next_buttonState){
    next_buttonState = true;
  }


  //Play previous track
  if(!digitalRead(prev_button) && prev_buttonState){
    prev_buttonState = false;
    saved_track = EEPROM.read(1);
    saved_track--;
    if(saved_track < 1){
      saved_track = count;
    }
    EEPROM.update(1,saved_track);
    mp3.playMp3FolderTrack(1);  
    delay(100);
  }

  else if (digitalRead(prev_button) && !prev_buttonState){
    prev_buttonState = true;
  }

  //Change color
  if(!digitalRead(color_button) && color_buttonState){
    color_buttonState = false;    
    light_toggle = 0;
    actualColor++;
    if(actualColor > colorAmount){
      actualColor = 1;
    }
    EEPROM.update(2,actualColor);
    set_led_brightness(brightness);
    delay(100);    
  }

  else if (digitalRead(color_button) && !color_buttonState){
    color_buttonState = true;
  }


  //Read brightness value
  brightness = analogRead(brightness_pin);

  if(light_toggle==0){
    if (brightness != prev_brightness){
      prev_brightness = brightness;
      set_led_brightness(brightness);
    }
  }

  else if(light_toggle==1){
    set_led_brightness(0);
    turn_one_LED();
  }
  else if(light_toggle==2){
    set_led_brightness(15);
  }
  else if(light_toggle==3){
    set_led_brightness(25);
  }
  else if(light_toggle==4){
    set_led_brightness(50);
  }
  else if(light_toggle==5){
    set_led_brightness(100);
  }
  else if(light_toggle==6){
    set_led_brightness(254);
  }
  
  

  //Change RGB values
  if(actualColor != prevColor){
    prevColor = actualColor;
    if(actualColor == 1){
      redValue = 0;
      greenValue = 0;
      blueValue = 0;
    }
    if(actualColor == 2){
      redValue = 255;
      greenValue = 0;
      blueValue = 0;
    }
    if(actualColor == 3){
      redValue = 0;
      greenValue = 255;
      blueValue = 0;
    }
    if(actualColor == 4){
      redValue = 0;
      greenValue = 0;
      blueValue = 255;
    }
    if(actualColor == 5){
      redValue = 255;
      greenValue = 255;
      blueValue = 0;
    }
    if(actualColor == 6){
      redValue = 255;
      greenValue = 0;
      blueValue = 255;
    }
    if(actualColor == 7){
      redValue = 0;
      greenValue = 255;
      blueValue = 255;
    }
  }
  
}///End of void loop






void set_led_brightness(int value){
  int brightness_value = map(value,10,1024,0,255);
  int R = brightness_value-redValue;
  int G = brightness_value-greenValue;
  int B = brightness_value-blueValue;
  if(R<0){
    R = 0;
  }
  if(G<0){
    G = 0;
  }
  if(B<0){
    B = 0;
  }
  for(int i=0; i<NUMPIXELS; i++) { // For each pixel...

    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a moderately bright green color:
    pixels.setPixelColor(i, pixels.Color(R, G, B));
  }
  pixels.show();   // Send the updated pixel colors to the hardware.
}

void turn_one_LED(){
  pixels.setPixelColor(1, pixels.Color(40, 40, 40));
  pixels.show();
}







Last tutorials

Homemade Baby White Noise Generator
RLC Transistor Tester PCB with Arduino
Ferrofluid 3D printed Bluetooth Speaker
10A Bharger/Protection Module
TMC silent driver comparison A4988 stepper driver

ADVERTISERS



>

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel bajo