Created
July 15, 2015 09:27
-
-
Save vittorioromeo/6fc0020d751192847bc4 to your computer and use it in GitHub Desktop.
semaphore tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseBtn | |
{ | |
private: | |
int pin; | |
public: | |
BaseBtn(int mPin) : pin(mPin) | |
{ | |
pinMode(pin, INPUT); | |
} | |
bool getInput() | |
{ | |
return digitalRead(pin); | |
} | |
}; | |
class ToggleBtn : public BaseBtn | |
{ | |
private: | |
bool prevState; | |
bool state; | |
bool wasPressed; | |
public: | |
ToggleBtn(int mPin) : BaseBtn(mPin), state(false), prevState(false), wasPressed(false) { } | |
void update() | |
{ | |
prevState = state; | |
if(getInput() == HIGH) | |
{ | |
if(!wasPressed) state = !state; | |
wasPressed = true; | |
} | |
else wasPressed = false; | |
} | |
bool on() { return state; } | |
bool changed() { return prevState != state; } | |
}; | |
class LedSemaphore | |
{ | |
private: | |
// Leds for vehicles | |
int vPins[3]; | |
// Leds for people | |
int pPins[2]; | |
// State | |
bool peopleWaiting; | |
int vLightTime; | |
int pLightTime; | |
const int maxVLightTime; | |
const int maxPLightTime; | |
int currVLight; | |
int currPLight; | |
int pProgress; | |
public: | |
LedSemaphore(int mPinV_G, int mPinV_Y, int mPinV_R, int mPinP_G, int mPinP_R) | |
: peopleWaiting(false), vLightTime(0), maxVLightTime(12000), maxPLightTime(24000), currVLight(0), currPLight(1), pProgress(0) | |
{ | |
vPins[0] = mPinV_G; | |
vPins[1] = mPinV_Y; | |
vPins[2] = mPinV_R; | |
for(int i = 0; i < 3; ++i) pinMode(vPins[i], OUTPUT); | |
pPins[0] = mPinP_G; | |
pPins[1] = mPinP_R; | |
for(int i = 0; i < 2; ++i) pinMode(pPins[i], OUTPUT); | |
} | |
void updateV() | |
{ | |
++vLightTime; | |
if(vLightTime >= maxVLightTime) | |
{ | |
vLightTime = 0; | |
++currVLight; | |
Serial.println("cycle v"); | |
} | |
} | |
void updateP() | |
{ | |
++pLightTime; | |
if(pLightTime >= maxPLightTime) | |
{ | |
pLightTime = 0; | |
++currPLight; | |
Serial.println("cycle p"); | |
} | |
} | |
void update() | |
{ | |
if(!peopleWaiting) | |
{ | |
currVLight = 0; | |
} | |
else | |
{ | |
if((currVLight % 3) != 2) | |
{ | |
updateV(); | |
} | |
else | |
{ | |
if(currPLight == 3) | |
{ | |
++pLightTime; | |
if(pLightTime >= maxPLightTime) | |
peopleWaiting = false; | |
} | |
else updateP(); | |
} | |
} | |
digitalWrite(vPins[currVLight % 3], HIGH); | |
digitalWrite(vPins[(currVLight + 1) % 3], LOW); | |
digitalWrite(vPins[(currVLight + 2) % 3], LOW); | |
digitalWrite(pPins[currPLight % 2], HIGH); | |
digitalWrite(pPins[(currPLight + 1) % 2], LOW); | |
} | |
void onBtnPressed() | |
{ | |
Serial.println("button pressed"); | |
if(!peopleWaiting) | |
{ | |
peopleWaiting = true; | |
currPLight = 1; | |
} | |
} | |
}; | |
ToggleBtn btn(8); | |
LedSemaphore lsem(3, 4, 5, 6, 7); | |
void setup() | |
{ | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
btn.update(); | |
if(btn.changed()) | |
{ | |
// Serial.println(btn.on() ? "on" : "off"); | |
lsem.onBtnPressed(); | |
} | |
lsem.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment