News

moodlite - Coming soon


Rhythm function and PCB update

Coolstuff55 · 162

Coolstuff55

  • Newbie
  • *
    • Posts: 6
    • Likes: +0/-0
    • View Profile
on: October 03, 2019, 11:27:54 AM
Hi all,
I have received my PCBs back and are ready to assemble. I have attached some photos below.
I have also added a microphone circuit with a small amplifier connected to the analog pin of the ESP8266,
with the hope of adding a rhythm function later.
I have found a piece of code online called soundbracelet that looks great for this task.
A demo of the code can be seen here :
&feature=youtu.be

I have tried adding the code to a moodlite function, but my arduino coding is relatively limited.
Heres what I've got so far. Any help would be much appreciated. MIC_PIN etc is defined on the main page
Cheers,
Matt

Code: [Select]
void three_sin()  //just overwriting an exsisting function as a test
{
  #define DC_OFFSET  0                                         // DC offset in mic signal - if unusure, leave 0
                                                                // I calculated this value by serialprintln lots of mic values
  #define NOISE     30                                         // Noise/hum/interference in mic signal and increased value until it went quiet
  #define SAMPLES   60                                          // Length of buffer for dynamic level adjustment
  #define TOP (iNrOfLeds + 2)                                    // Allow dot to go slightly off scale
  #define PEAK_FALL 10                                          // Rate of peak falling dot
 
  byte
    peak      = 0,                                              // Used for falling dot
    dotCount  = 0,                                              // Frame counter for delaying dot-falling speed
    volCount  = 0;                                              // Frame counter for storing past volume data
  int
    vol[SAMPLES],                                               // Collection of prior volume samples
    lvl       = 10,                                             // Current "dampened" audio level
    minLvlAvg = 0,                                              // For dynamic adjustment of graph low & high
    maxLvlAvg = 512;


 
  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int      n, height;
   
  n = analogRead(MIC_PIN);                                    // Raw reading from mic
  n = abs(n - 512 - DC_OFFSET);                               // Center on zero

  Serial.println(n);
 
  n = (n <= NOISE) ? 0 : (n - NOISE);                         // Remove noise/hum
  lvl = ((lvl * 7) + n) >> 3;                                 // "Dampened" reading (else looks twitchy)
 
  // Calculate bar height based on dynamic min/max levels (fixed point):
  height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
 
  if (height < 0L)       height = 0;                          // Clip output
  else if (height > TOP) height = TOP;
  if (height > peak)     peak   = height;                     // Keep 'peak' dot at top
 
 
  // Color pixels based on rainbow gradient
  for (int k=0; k<iNrOfLeds; k++) {
    if (k >= height)   crgbLeds[k].setRGB( 0, 0,0);
    else crgbLeds[k] = ColorFromPalette(crgbCurrentPalette, 255, 255);
  }
 
  // Draw peak dot 
  if (peak > 0 && peak <= iNrOfLeds-1) crgbLeds[peak] = CHSV(map(peak,0,iNrOfLeds-1,30,150), 255, 255);
 
  addEffect();

// Every few frames, make the peak pixel drop by 1:
 
    if (++dotCount >= PEAK_FALL) {                            // fall rate
      if(peak > 0) peak--;
      dotCount = 0;
    }
 
  vol[volCount] = n;                                          // Save sample for dynamic leveling
  if (++volCount >= SAMPLES) volCount = 0;                    // Advance/rollover sample counter
 
  // Get volume range of prior frames
  minLvl = maxLvl = vol[0];
  for (i=1; i<SAMPLES; i++) {
    if (vol[i] < minLvl)      minLvl = vol[i];
    else if (vol[i] > maxLvl) maxLvl = vol[i];
  }
  // minLvl and maxLvl indicate the volume range over prior frames, used
  // for vertically scaling the output graph (so it looks interesting
  // regardless of volume level).  If they're too close together though
  // (e.g. at very low volume levels) the graph becomes super coarse
  // and 'jumpy'...so keep some minimum distance between them (this
  // also lets the graph go to zero when no sound is playing):
  if((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6;                 // Dampen min/max levels
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6;                 // (fake rolling average)

}




Coolstuff55

  • Newbie
  • *
    • Posts: 6
    • Likes: +0/-0
    • View Profile
Reply #1 on: October 03, 2019, 10:31:34 PM
Forgot to add PCB images... attached.



shiryou

  • moodlite Developer
  • Newbie
  • *****
    • Posts: 48
    • Likes: +5/-0
    • View Profile
Reply #2 on: October 04, 2019, 10:48:19 AM
it looks awesome!

can you upload the pcb schematics? im really interested on try it out and implement it on the moodlite code.

I tried diferent solutions with different mics and had no success...

good job!



Coolstuff55

  • Newbie
  • *
    • Posts: 6
    • Likes: +0/-0
    • View Profile
Reply #3 on: October 04, 2019, 11:12:53 AM
Cheers! I have no idea if it will work either, its just a simple mic and amplifier circuit. I'll post the schematic tomorrow, whats the best format for you? I designed in Altuim. I would really appreciate it if you could help with the code side, I think it would make an awesome feature.

Matt



shiryou

  • moodlite Developer
  • Newbie
  • *****
    • Posts: 48
    • Likes: +5/-0
    • View Profile
Reply #4 on: October 04, 2019, 11:55:35 AM
choose the format you prefer, i just designed one pcb before so... i'll adapt to what you have.

No problem with the code. I added it to moodlite before, but the signal was so low that the leds almost didnt light up... but if i get it to work i will add them to the effects, no problems with that

Thanks!



Coolstuff55

  • Newbie
  • *
    • Posts: 6
    • Likes: +0/-0
    • View Profile
Reply #5 on: October 05, 2019, 09:09:28 AM
Sweet, just attached as an image for now. Let me know if you need anything more.
On the right side of the schematic is the reset switch, PIR sensor and LED output (what exactly does the rst switch do in the software?).
On the left side is the microphone and amplifier circuit. The opamp used is the common LM386 audio amplifier. R5 is a pot that is used to adjust the volume if necessary. It should be adjusted once at setup and then left. C3 sets the gain of the amplifier. The 10uF that I've put in there sets it to its max of 46dB, or x200. This should be fine for our application. If necessary, this can be adjusted to x50 or x20 with different values.
Hopefully the components all arrive this week, so I can put it together and test some code.
I think that's all... If there are any details I've missed or any questions feel free to ask.
Cheers,
Matt.



shiryou

  • moodlite Developer
  • Newbie
  • *****
    • Posts: 48
    • Likes: +5/-0
    • View Profile
Reply #6 on: October 08, 2019, 07:10:18 AM
hi! its almost the same circuit i tried before summer but i dindt get it to work.

Ill try this one as soon as i can and tell you something

thanks!



Coolstuff55

  • Newbie
  • *
    • Posts: 6
    • Likes: +0/-0
    • View Profile
Reply #7 on: October 10, 2019, 03:42:44 AM
Sweet as. My components arrived this week for the PCB, so I should get it built up in the next week and then I can have a play too.
I also discovered mDNS which would be cool to add to the code too. It would mean instead of typing an IP address, you could just type "moodlite.local" and it will come up. Looks quite simple too. More info here -> https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html

Cheers
Matt



 


SMF spam blocked by CleanTalk