Dangling else problem

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

#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 it's not 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 the 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

Easy to make evaluation board for DYI robots

Intro

I will start from saying that the board could be replaced by any Arduino plus some(s) its motor driver shield(s). So why I made it you may ask? Well, while I made this tiny tank-robot model presented on below pictures, I wanted to make at least some things by myself, and decide what I need and how I need it instead of only buying prefabricated stuff.

This is the result, hope you will enjoy it and that maybe you will find inspirations for your own stuff.

Her's what it has and what it can..

  • Handle two separate motors, each can run either "forward", "backward", or stop.
  • Motors can be powered from a separate power source (there's a jumper for that, if removed, power is taken from a source connected to a separate goldpin)
  • ATtiny24 manages the board, it's relatively small microprocessor, but I think that it will be fine for such project. All its pins are available via goldpins, I also added a couple goldpins connected to Vcc and GND to make easier connection to other boards, shields and stuff like that.
  • To make programming easier, 6 PINs ISP socket for the ATtiny24 is also included.
  • ATtiny24 can be removed and the motor driver can be managed from external source, e.g. an Arduino. This could be useful in future if the ATtiny24 would not be sufficient.
  • There isn't any voltage regulator, or step/up/down/ converter because it would make the board less flexible.

Circuit

The circuit is simple, consists only of a small microprocessor, motor driver, some filtering capacitors and a lot off connectors.

The hardware was designed in Eagle, the board is available on GitHub.

Software

PB0, PB1, PB2 and PB3 ports of the ATtiny24 are used to drive the motor, others are available for other purposes, e.g to communicate with the sensors. There's not a lot of them left, because the microprocessor is quite small, but I think that for such project, it will be enough.

DKP-50 dosimeter (radioactivity detector) tear-down

DKP-50 was a dosimeter produced for the Polish army during the cold war era. It indicates amount of radioactivity absorbed by the body, a soldier was equipped in this device in case of nuclear conflict. A scale has range of 0-50R (R = Roentgen, a legacy unit of exposure of X-rays and gamma rays). 500R in 5h is usually lethal.

It's based on ionizing chamber that changes radioactivity radiation to an electric current, current discharges a capacitor that is also used as a power source. The device measures discharge of a capacitor over time. This is done by using a wire that accordingly changes its position and optic unit to make those results available for the operator.

If you're interested in radioactivity, you may also want to read about a Geiger–Müller Counter that I've made.

Sample result is visible on the picture. Note that it's inaccurate because the device was discharged and wasn't calibrated.

The deice, visible a plastic cap (with seal) to protect charging electrodes and a metal clip to attach the device to an uniform. On the cap, there're two names: "DKP-50" and "12".

Kirlian photography - electronic engineering meets parapsychology

Kirlian photography is an interesting photographic technique of capturing corona discharge of objects. The images basically contain only edges in a form of blue glow. Note: one of those photos is visible in "The X files" intro - read the whole article to know why!

In this post I will present my minimalist approach with common materials and without complicated construction. The results aren't that good as with more complex setups, but I think that they are still really interesting.

Materials

  • A camera with modifiable ISO and exposure time.
  • 10-30kV high voltage supply.
  • Tin foil. I used a foil in a form of adhesive strips because it's easier to stick it to surfaces, but a regular one can be also used.
  • Small piece of glass.
  • Slats or other things to put everything in place.