Madrid is an amazing city, and definitely should be visited. However, if for some reason you don't want to go that far out of your way, about 3-1/2 hours south of Barcelona is Valencia, which I consider to be a "perfect" city (which is saying a lot). It has a lot of history but also some interesting more modern architecture, a beautiful center city and historic district and is entirely walkable. There are several very scenic and quaint towns nearby such as Denia, and while you didn't say what time of the year you will be there, if it is the summer, there are many nice beaches there as well. Plus, it is the home of paella so, really, what more could you want?