If this condition becomes chronic, and persists after several days, a diagnosis of pleurisy must be ruled out. Viral infections are the most common cause, but there are other possible causes, including mtb and other bacterial infections.
This can be a very painful condition.
Since the most common cause is viral, there is little to do except help the immune system (by exposure to sunlight, vitamin c, and perhaps a vitamin supplement), assist breathing (if necessary) and wait for developments. Sometimes the pain is in the form of a "stitch". Other times it may be extreme and there may be a fast heart rate.
I don't want to ever truly discourage going to an ER (it may sem I was doing so, on reflection) , but a single episode or two usually resolves of its own accord.
If the cause is bacterial antibiotics are indicated.
If it continues a physican usually orders a CBC, full panel bloodwork, mtb test, and perhaps a thorocentesis, an ultrasound and an x-ray.
Now Ibuprufin can be a very dangerous drug, and is contraindicated during pregnancy.
Originally this was a prescription drug.
When it was released for sale in drug stores without a prescription the dose was lowered to 200 mg a pill. The prescription pills are 400 mg.
You should use as little of this drug as possible and review the internet for side effects and contraindications.
Goodness gracious. It sounds like a pleuritic problem. There are sliding surfaces...I guess skin-like would be the appropriate word...surrounding the lungs, When they become inflamed, this sort of thing happens. They don't slide properly against one another.
Usually it goes away. That is, the condition is self-limiting.
Ibuprufin, twice a day, 400 my each time, with food, for four days is a common prescription.
You bring to mind the fact I had these episodes decade ago as a teenager, exactly as you describe, and then...they went away, to return a half dozen other times now and again.
One school of thought suggests a viral infection. Of course there is a million-to-one shot you have a tumor or something really bad. It certainly isn't heart-related.
I am sticking my neck out, but I would not be overly concerned (and I am the first one to hit the panic button) unless they persist, in which case a visit to a pulmonary specialist would be in order.