Vous êtes sur la page 1sur 3

5/28/2015

PrintedcodeversionproducedbyWordPressWPSynHighlightplugin

/*
Designatrafficlightforanintersectionofa
highwayandacountryroad.Normall,thelight
shouldbegreenforthehighwayandredforthe
countryroad,butwhentrafficapproacheson
thecountryroad,thehighwaygetsaredlight
andthecountryroadgetsagreenlight.

Whenalightturnsredittransitionsthrough
yellow,butaredlighttransitionsdirectly
togreen.

Apushbuttonrepresentsacarapproachingon
thecountryroad.

ImplementthesolutionwithaFiniteState
MachineorFSM.

Followingtheexcellentdescriptionat
http://hacking.majenko.co.uk/finitestatemachine
firstbreakdowntheproblemintostates.

IdentifywhichstatesareTransitional(T)and
whichareStatic(S).AStaticstateisonein
whichtheFSMiswaitingforstimulus,andis
takingnoactions.ATransitionalStateisa
statewhichcausesactions,butdoesn'tlook
forstimulus.

ATransitionalStaterunsonceandimmediately
movesontoaStaticState.

State0:highway=green,country=red;(T)
State1:waitforcaroncountryroad(S)
State2:highway=yellow,makenoteofcurrenttime(T)
State3:waitforyellowtimetopass(S)
State4:highway=red,country=green,makenoteofcurrenttime(T)
State5:waitforhighwayredtimetopass(S)
State6:country=yellow,makenoteofcurrenttime(T)
state7:waitforyellowtimetopass(S)thengoto0
*/

//Usenamesforstatessoit'seasierto
//understandwhatthecodeisdoing
constintS_HIGHWAYGREEN=0;
constintS_WAITCARCOUNTRY=1;
constintS_HIGHWAYYELLOW=2;
constintS_WAITHIGHWAYYELLOW=3;
constintS_HIGHWAYRED=4;
constintS_WAITHIGHWAYRED=5;
constintS_COUNTRYYELLOW=6;
constintS_WAITCOUNTRYYELLOW=7;

//Pinnumbers
constintcountrySensorPin=2;

constinthighwayGreenLEDPin=3;
constinthighwayYellowLEDPin=4;
constinthighwayRedLEDPin=5;

constintcountryGreenLEDPin=6;
constintcountryYellowLEDPin=7;
constintcountryRedLEDPin=8;

http://teachmetomake.com/wordpress/arduinotutorialstatemachine#codesyntax_1

1/3

5/28/2015

PrintedcodeversionproducedbyWordPressWPSynHighlightplugin

voidsetup()
{
pinMode(highwayGreenLEDPin,OUTPUT);
pinMode(highwayYellowLEDPin,OUTPUT);
pinMode(highwayRedLEDPin,OUTPUT);
pinMode(countryGreenLEDPin,OUTPUT);
pinMode(countryYellowLEDPin,OUTPUT);
pinMode(countryRedLEDPin,OUTPUT);
}

voidloop()
{

//startoffwiththehighwaygettinggreen
//Thekeyword"static"makessurethevariable
//isn'tdestroyedaftereachloop
staticintstate=S_HIGHWAYGREEN;

//Tostorethecurrenttimefordelays
staticunsignedlongts;

switch(state)
{
caseS_HIGHWAYGREEN:
//Highwaygetsgreen,countryroadred
digitalWrite(highwayGreenLEDPin,HIGH);
digitalWrite(highwayYellowLEDPin,LOW);
digitalWrite(highwayRedLEDPin,LOW);

digitalWrite(countryGreenLEDPin,LOW);
digitalWrite(countryYellowLEDPin,LOW);
digitalWrite(countryRedLEDPin,HIGH);

state=S_WAITCARCOUNTRY;

break;

caseS_WAITCARCOUNTRY:

if(digitalRead(countrySensorPin)==HIGH){
state=S_HIGHWAYYELLOW;
}

break;

caseS_HIGHWAYYELLOW:
digitalWrite(highwayGreenLEDPin,LOW);
digitalWrite(highwayYellowLEDPin,HIGH);
digitalWrite(highwayRedLEDPin,LOW);

digitalWrite(countryGreenLEDPin,LOW);
digitalWrite(countryYellowLEDPin,LOW);
digitalWrite(countryRedLEDPin,HIGH);

ts=millis();//Rememberthecurrenttime

state=S_WAITHIGHWAYYELLOW;//Movetothenextstate

break;

caseS_WAITHIGHWAYYELLOW:
//Iftwosecondshavepassed,thenmoveontothenextstate.
if(millis()>ts+2000)
{
state=S_HIGHWAYRED;
http://teachmetomake.com/wordpress/arduinotutorialstatemachine#codesyntax_1

2/3

5/28/2015

PrintedcodeversionproducedbyWordPressWPSynHighlightplugin

break;

caseS_HIGHWAYRED:
//Highwayred,countryroadgreen
digitalWrite(highwayGreenLEDPin,LOW);
digitalWrite(highwayYellowLEDPin,LOW);
digitalWrite(highwayRedLEDPin,HIGH);

digitalWrite(countryGreenLEDPin,HIGH);
digitalWrite(countryYellowLEDPin,LOW);
digitalWrite(countryRedLEDPin,LOW);

ts=millis();//Rememberthecurrenttime

state=S_WAITHIGHWAYRED;

break;

caseS_WAITHIGHWAYRED:

//Iffivesecondshavepassed,thenstart
//transitiontoaredlightforthecountry
//road
if(millis()>ts+5000)
{
state=S_COUNTRYYELLOW;
}

break;

caseS_COUNTRYYELLOW:
digitalWrite(highwayGreenLEDPin,LOW);
digitalWrite(highwayYellowLEDPin,LOW);
digitalWrite(highwayRedLEDPin,HIGH);

digitalWrite(countryGreenLEDPin,LOW);
digitalWrite(countryYellowLEDPin,HIGH);
digitalWrite(countryRedLEDPin,LOW);

ts=millis();//Rememberthecurrenttime

state=S_WAITCOUNTRYYELLOW;

break;

caseS_WAITCOUNTRYYELLOW:

//Iftwosecondshavepassed,thengo
//backtothebeginningwiththehighway
//gettingthegreenlight
if(millis()>ts+2000)
{
state=S_HIGHWAYGREEN;
}

break;

}//endofswitch
}//endofloop

http://teachmetomake.com/wordpress/arduinotutorialstatemachine#codesyntax_1

3/3

Vous aimerez peut-être aussi