Click here to Skip to main content
15,887,676 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Let's REALLY make sure "4" isn't "5" Pin
MikeTheFid18-Oct-18 4:03
MikeTheFid18-Oct-18 4:03 
GeneralRe: Let's REALLY make sure "4" isn't "5" Pin
W Balboos, GHB18-Oct-18 8:15
W Balboos, GHB18-Oct-18 8:15 
GeneralRe: Let's REALLY make sure "4" isn't "5" Pin
raddevus19-Oct-18 8:57
mvaraddevus19-Oct-18 8:57 
GeneralRe: Let's REALLY make sure "4" isn't "5" Pin
Ryan Peden25-Oct-18 5:09
professionalRyan Peden25-Oct-18 5:09 
GeneralRe: Let's REALLY make sure "4" isn't "5" Pin
Nelek29-Oct-18 1:16
protectorNelek29-Oct-18 1:16 
GeneralRe: Let's REALLY make sure "4" isn't "5" Pin
charlieg30-Oct-18 11:17
charlieg30-Oct-18 11:17 
GeneralRe: Let's REALLY make sure "4" isn't "5" Pin
TheGreatAndPowerfulOz6-Nov-18 9:09
TheGreatAndPowerfulOz6-Nov-18 9:09 
Generalfriend: frequency to MIDI should be easy right? Pin
raddevus15-Oct-18 8:57
mvaraddevus15-Oct-18 8:57 
It's a long convoluted story about how I finally wound up on the following code (below).

It all started with a request from a friend:
naive friend
"Can't you make a device for me that analyzes tones (frequencies) and turns them into MIDI and outputs them to my computer so I can play the guitar and have it turned into MIDI? Should be easy right?"


Me:
Everything should be easy.


Well, there was a official arduino example using a MKR 1000 that has an incorrect schematic[^]*. And from there, I was down the rabbit-hole.

Okay, so now I was just trying to create a quick sound generator that I can use to input into the Analog 0 pin (A0) of my MKR Zero so I can test how the code works. I have a Grove Seeed Sound Sensor laying around so I find some sample code.
However, there is no explanation of this odd piece of code and I have to look at it closely.

Yes, it's a simple bitshift, but I had to think about what it does.
Do you see what it does?**

C++
const int pinAdc = A0;

void setup()
{
    Serial.begin(115200);
    //Serial.println("Grove - Sound Sensor Test...");
}

void loop()
{
    long sum = 0;
    for(int i=0; i<32; i++)
    {
        sum += analogRead(pinAdc);
    }

    sum >>= 5;

    Serial.println(sum);
    delay(10);
}


So it looks like it takes 32 samples each time through the loop and then it does that bit shift.
Interesting.
The code is from Grove - Sound Sensor[^]

*Bonus Material
The error in the schematic that is not in the breadboard example is that the schematic uses Amp2 OUT to go into the A0 pin on the Arduino even though there is no input on Amp2. The output should be from Amp1 to A0 on the schematic. See schematic at : https://www.arduino.cc/en/uploads/Tutorial/ArduinoMKR1000AudioInput_schem.png[^]


** spoiler Yep, it just clips off the 5 least significant bits, right? Well, it seems weird to do something like that without even mentioning why. This would've been a nice place in the code (or at the article) for a comment. But no.

modified 16-Oct-18 8:25am.

GeneralRe: friend: frequency to MIDI should be easy right? Pin
Rick York15-Oct-18 9:35
mveRick York15-Oct-18 9:35 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
raddevus15-Oct-18 9:42
mvaraddevus15-Oct-18 9:42 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
Rick York15-Oct-18 10:17
mveRick York15-Oct-18 10:17 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
msk471115-Oct-18 22:50
msk471115-Oct-18 22:50 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
raddevus16-Oct-18 2:08
mvaraddevus16-Oct-18 2:08 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
Bernhard Hiller16-Oct-18 22:52
Bernhard Hiller16-Oct-18 22:52 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
Marc Clifton16-Oct-18 1:24
mvaMarc Clifton16-Oct-18 1:24 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
raddevus16-Oct-18 2:12
mvaraddevus16-Oct-18 2:12 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
Storm-blade16-Oct-18 1:30
professionalStorm-blade16-Oct-18 1:30 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
raddevus16-Oct-18 2:13
mvaraddevus16-Oct-18 2:13 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
Rick York16-Oct-18 5:32
mveRick York16-Oct-18 5:32 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
raddevus16-Oct-18 8:35
mvaraddevus16-Oct-18 8:35 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
Rick York16-Oct-18 9:38
mveRick York16-Oct-18 9:38 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
Jan Holst Jensen218-Oct-18 1:14
Jan Holst Jensen218-Oct-18 1:14 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
StatementTerminator18-Oct-18 4:19
StatementTerminator18-Oct-18 4:19 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
raddevus18-Oct-18 12:47
mvaraddevus18-Oct-18 12:47 
GeneralRe: friend: frequency to MIDI should be easy right? Pin
StatementTerminator19-Oct-18 5:07
StatementTerminator19-Oct-18 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.