Coordinating carpet and wall colors is more about picking complementary tones, rather than exactly matching your colors. Wall and carpet surfaces have very different textures and light reflecting properties. Carpet swatches and paint samples that look the same in the store may look very different in your room, in different lighting conditions. The best approach is to compare color samples in the room at different times of day, in natural daylight and with lights on during both day and night.
If your walls are painted, and you're picking carpet, start by comparing sample pieces of carpet in 3 - 5 different tones of that color. Move the pieces around the room to see how the colors look in different light. The carpet colors will look different next to windows or lamps, or in darker corners. If you have 1-2 likely choices, get larger sample pieces and compare those in different light before deciding.
If you're picking paint to go with existing carpet, select 4-5 likely colors at the paint store and take home several color squares of each. Stick them to the wall in different places, and look at them under different light conditions and times of day. Laying some paint samples on the carpet can help you decide which one best complements the carpet color. Once you have a couple of choices, buy some samples and paint test areas on the wall, putting some close to the floor so you can easily compare to the carpet. Again, evaluate your choices in different light conditions. If you can't find an exact match, choose a color that complements and coordinates with the carpet. Paint is less expensive than carpet, so even if you decide later that your wall color is wrong, you can easily re-paint the room.
Having a well-coordinated space is sure to increase your sense of happiness in your home.