Turkey is a divided, unhappy country. It is not clear exactly what motivated the men who plotted the coup. But they tried to seize control at a time when Turkey is deeply divided over President Recep Tayyip Erdogan's project to transform the country, and hurting badly by the contagion of violence from the war in Syria.
The Turkish government has indirectly accused the exiled Islamic cleric Fethullah Gulen of being behind the coup.
Mr Erdogan has in the past blamed Mr Gulen for a variety of Turkey's ills.
Prime Minister Binali Yildirim threatened any country that would "stand by" Mr Gulen "won't be a friend of Turkey and will be considered at war with Turkey".
Mr Gulen lives in self-imposed exile in the United States. He was an ally of Mr Erdogan's, and was sometimes known as Turkey's second most-powerful man, before they fell out.
Turkey is important in the Middle East because of its geographic position, straddling Europe and Asia; because it is a leading member of Nato; and because the Islamist President Erdogan and his AK party have taken a strong interest in the Sunni Muslim-led governments in the region.
In a speech a Turkish newscaster said she was forced to read at gunpoint, the plotters claimed that they were aiming to restore Turkey's secular democracy.
The ruling AK party has become expert at winning elections, but there have always been doubts about Mr Erdogan's long-term commitment to democracy.
Once, he compared democracy to a bus you take to your destination, and then get off.
President Erdogan is a political Islamist who has rejected modern Turkey's secular heritage.
He has been increasingly authoritarian, locking up troublesome journalists and others.
Now that he has crushed an attempted coup he might try to impose an even tighter regime on the country.
Mr Erdogan served as prime minister for many years. Now he is trying to change the constitution to turn himself into a strong executive president.
Mr Erdogan and his governments have been deeply involved in the war in Syria since it started in 2011, backing mainly Islamist militias fighting the regime of President Bashar al-Assad.
But violence has spread across the border, helping to reignite the fight with the Kurdish PKK, and turning Turkey into a target for the jihadists who call themselves Islamic State.
The West sees Turkey as part of the solution in the Middle East. That requires stability, and without it a simple equation applies.
Turmoil in the Middle East plus turmoil in Turkey equals trouble for everyone.
But it can be argued that Turkey has made a lot of trouble on its own in the region and is deeply tied up in the conflicts of its neighbours.
Recep Tayyip Erdogan's governments have shown a much keener appetite about getting involved in the Middle East than the Turkish people.
He has been a key backer of the mainly Islamist militias that have been fighting Syria's president since 2011.
Recently he has been patching up relations with Israel - but President Erdogan also has a natural affinity with Israel's enemy Hamas - which shares roots in the Muslim Brotherhood.
And Turkey is seen by the EU as a vital part of schemes to control the flow of migrants from the Middle East.
Turkey is facing increasing turmoil, and the attempt to over thrown President Erdogan will not be the last of it.