I used the www.travelinescotland.com website for public-transportation schedules. I observed a few cases when the train schedules differed from those on the nationalrail.co.uk website, so I would verify train times on the latter. Two other websites for intercity bus schedules are www.citylink.co.uk and www.stagecoachbus.com
I spent a few nights in Dundee in 2019, during which time I day-tripped to St. Andrews by bus. It would be easy to do that in reverse; I'd guess this would make more sense than taking the car and looking for parking, but you should check on the bus schedule (I think buses are frequent) and parking cost. Dundee doesn't get a lot of foreign tourists, so it makes a nice real-world destination when you're spending so much time in St. Andrews. Major tourist sights in Dundee include:
Discovery Point, Riverside Dr, across from train station: Historical museum documenting the Shackleton and Antarctic exploration. Includes restored ship Discovery.
Verdant Works, W Henderson’s Wynd: Restored jute factory. Guidebooks say this industrial heritage site is extremely well done.
McManus Art Galleries & Museum, Albert Sq, Fine art and decorative art. This is a traditional museum.
www.mcmanus.co.uk/content/galleries-displays/galleries
V & A Dundee Design Museum, 1 River Esplanade. New branch of London's V&A. Has what looked like a destination sort of restaurant, but I didn't eat there. Free/donation requested except for special exhibitions. www.vam.ac.uk/dundee
Dundee Botanic Garden, Riverside Dr.
St. Andrews itself has a picturesque center (medieval street plan but mostly Victorian-era buildings) where you can enjoy some wandering-around time during one or more of your free half-days:
There may be guided walking tours of the university.
Ruins of St. Andrews Castle
St. Andrews Preservation Trust Museum and Garden, 12 North St.: 17C fishing family’s house.
Museum of the University of St. Andrews, 7A the Scores: Art and decorative art, I believe; it was closed at the time of my visit.
St. Salvatore’s Chapel: Beautiful medieval church (1450).
St. Andrews Cathedral: Ruins are best part and free.
There's a coastal walking path; not sure exactly where it is.
The East Neuk villages on the coast between St. Andrews and Edinburgh are picturesque. There are buses taking that route, though you might have to transfer. I think Crail, Pittenweem and Anstruther ("Enster") are considered among the best to visit. This might be an option for one or two of your half-days.
I didn't have time to go to Perth, but it is supposed to be a nice city with some worthwhile sights:
Scone Palace, E side of river: About 3 miles N of Perth RR Station.
Fergusson Gallery, Marshall Place: Small gallery showing works of a Scottish Colourist. www.culturepk.org.uk/museums-and-galleries/the-fergusson-gallery/
River Tay Public Art Trail (sculptures, gardens). Taks about one hour.
Balhousie Castle, 99 Hay St: Houses the Black Watch Museum. About 0.8 miles from town center.
Branklyn Gardens, 116 Dundee Rd, E side of river, about 1 mile from town center. Has a tearoom.
With a car you'll have a lot more options for easy side trips than I did. I trust others will make suggestions, because I didn't cover a great deal of the Dundee/St. Andrews area.
I'll put some Edinburgh suggestions in a separate response since I'm about maxxed out here.