Hi Ann, if that is the case then I would seriously consider dropping Madrid and Southern Spain all together, especially with how suffocatingly hot it can be during the summer time.
I would recommend you fly in to Barcelona, rent a car and do a little roadtrip through northern Spain, which will be much more pleasant climate-wise during the summer months. From Barcelona you head north along the Costa Brava, then head west across the Pyrenees, and end up exploring the Basque Country, flying out of Bilbao. Here would be some potential stops including overnights, that I would recommend for a 2-3 week trip.
Start Barcelona
Girona - overnight
Costa Brava
Empúries Ancient Greek Ruins
Besalu
Ribes de Freser - overnight
Vall de Nuria
Puigcerdà
La Seu d'Urgell - overnight
Espot
Vielha - multi-day base
Aigüestortes National Park
Vall de Boi
Vall de Aran
Jaca - overnight
Loarre Castle
Sos del Rey Católico
Pamplona - overnight
Olite Castle
San Sebastian - overnight
Bayonne
Basque coastal drive
Bilbao - overnight
End Bilbao
As you can see, with a car, you can visit many interesting and unique places in northern Spain, in a relatively focused area, without the need to zigzag across the entire country of Spain. Travel less, see more ;-)