Dangling else problem

What is a dangling else problem? It's presented below - what will print this program?

#include <iostream>

int main()
{
    bool a = true;
    bool b = false;
    
    if(a)
        if (b)
            std::cout << "foo" << std::endl;
    else
        std::cout << "bar" << std::endl;

   return 0;
}

It may seem that it won't print anything, but isn't true - on the screen we will see "bar". That's because the "else" is bound to the last "if" statement that wasn't already bound to other "else".

This may lead to bugs and confusion. One easy way is to always embrace body of the "if" statement in brackets. If we want to bind the "else" to the first "if" then correct parenthesis should be added as below:

    if(a)
    {
        if (b)
        {
            std::cout << "foo" << std::endl;
        }
    }
    else
    {
        std::cout << "bar" << std::endl;
    }

If we want to bound it to the second "if", it should be:

    if(a)
    {
        if (b)
        {
            std::cout << "foo" << std::endl;
        }
        else
        {
            std::cout << "bar" << std::endl;
        }
    }

What is good is that compilers will usually produce a warning in places where this error may exist:

 In function 'int main()':
8:7: warning: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]

Tiny and cheap lab generator

Function generator is a rely useful tool in the lab, a simple version can be build at home, there's a lot of examples on the Internet, some are much more complex and powerful, but this one is intended to be really simple and cheap. It can be also built-in in some bigger projects that also requires a generator.

The generator can output (only) square signal with adjusted frequency and pulse width. The amplitude may be also modified by soldering different values on the voltage divider on the output, it's a bit laborious, but if it will be used in digital or microprocessor projects, then there isn't really need for amplitude adjustment.

simple laboratory generator, front view

The generator is built from two NAND gates (IC1A, IC1B), that's a classic configuration used in many other circuits. S1 and C1-C8 selects the frequency, there is an equitation for it, but the general rule is that the more capacity thy have, the lower frequency is generated. By adjusting witch one switch are set to ON state, we can select plugged capacitors and the output frequency. R2-R4, D1, D2 changes pulse width.

IC1C, IC1D make a buffer. Old trick.

A voltage divider R5+R6 can be used to reduce an amplitude of the signal.

simple laboratory generator, circuit

Single layer PCB was used, some of the elements are SMD, but majority is THT.

simple laboratory generator, bottom view

On the o'scope it's visible, that the signal looks good, below images show the lowest and highest frequency signal that can be generated, but it could be expanded by using smaller/bigger C1-C8 and R2-R4.

simple laboratory generator, oscilloscope view, lowest frequency simple laboratory generator, oscilloscope view, highest frequency

Project was created in Eagle, you can download all of its files (including PCB images for home made etching) from my GitHub. Image of top side needs to be mirrored before printing.

More readable way of casting primitive types in C++

How to cast a variable of one primitive type to another? Usually people use static_cast operator, but it can be done in other, more readable way.

Below example demonstrate the problem, and two solutions:

char var1 = 3;
char var2 = 5;

// first way
int result1 = static_cast<int>(var1 + var2);

// second way
int result2 = int(var1 + var2);

The same trick may be used e.g. when passing to a function an argument of different type that expected.

There is also C-style casting available in C++, but I didn't mention it above because it's discouraged.

Yes, I know, casting from one type to another is a common source of bugs, and can signify that overall design could be improved. That's true, but sometimes it's inevitable.

A trip with Geiger Counter on the Śnieżka mountain

During the Second World War Nazis established a uranium mine in Krkonoše mountains. Extracted ore was used in a research facility in Oranienburg. After the war, the mine was in polish borders, but Poland was then a puppy country of the USSR, so the ore was still extracted, but now was transported to the USSR. Later the mine was used in civil research.

I've visited those mountains with my Geiger counter, not the mine, but still close - Śnieżka mountain.

My construction uses three GM tubes instead of one - that's why the amount of pulses per period of time is also three times bigger. Don't panic! :) In addition, even if radioactivity is a bit bigger that in (for example) my home in Wroclaw, it's still not hazardous for health.

As a reference, at home my Geiger Counter shows around 60 pulses per minute.

As you will see below results were bigger than those that I observe at home, is it due to the uranium ore that is near? Maybe, I'm not sure, I wasn't on any other mountains with my counter, so I can't verify it.

Preparations

32F429IDISCOVERY board is used to count pulses and show results, it requires 5V power supply, the same as my Geiger Counter. Both were supplied from DC/DC converter (the cheapest I could buy), and 3xAA NiCd batteries. In total 250-300mA was drained from the batteries.

Final adjustments of the latest HW version - as usual, I made mistakes during the design of the PCB.

Geiger Counter - last modification before a trip