Created
October 27, 2021 09:06
-
-
Save ElWazy/699064280bedadf5af5536bc79e28068 to your computer and use it in GitHub Desktop.
Semáforo Doble Arduino
This file contains 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
// 0 = GREEN | |
// 1 = YELLOW | |
// 2 = RED | |
int semaphore1[3] = {5, 6, 7}; | |
int semaphore2[3] = {8, 9, 10}; | |
bool pass = true; | |
void setup() { | |
for (int i = 0; i < 3; i++) { | |
pinMode(semaphore1[i], OUTPUT); | |
pinMode(semaphore2[i], OUTPUT); | |
} | |
} | |
// States | |
void go(int semaphore[3]) { | |
digitalWrite(semaphore[0], HIGH); | |
digitalWrite(semaphore[1], LOW); | |
digitalWrite(semaphore[2], LOW); | |
} | |
void change(int semaphore[3]) { | |
digitalWrite(semaphore[0], LOW); | |
digitalWrite(semaphore[1], HIGH); | |
digitalWrite(semaphore[2], LOW); | |
} | |
void wait(int semaphore[3]) { | |
digitalWrite(semaphore[0], LOW); | |
digitalWrite(semaphore[1], LOW); | |
digitalWrite(semaphore[2], HIGH); | |
} | |
// Blinks | |
void blinking(int led) { | |
const int AMOUNT_BLINKINGS = 5; | |
for(int i = 0; i < AMOUNT_BLINKINGS; i++) { | |
digitalWrite(led, HIGH); | |
delay(100); | |
digitalWrite(led, LOW); | |
delay(100); | |
} | |
} | |
void double_blinking(int led1, int led2) { | |
const int AMOUNT_BLINKINGS = 5; | |
for(int i = 0; i < AMOUNT_BLINKINGS; i++) { | |
digitalWrite(led1, HIGH); | |
digitalWrite(led2, HIGH); | |
delay(100); | |
digitalWrite(led1, LOW); | |
digitalWrite(led2, LOW); | |
delay(100); | |
} | |
} | |
// Controllers | |
void translate() { | |
change(semaphore1); | |
change(semaphore2); | |
delay(3000); | |
double_blinking(semaphore1[1], semaphore2[1]); | |
} | |
bool stand(bool other) { | |
int led; | |
if (other) { | |
go(semaphore1); | |
wait(semaphore2); | |
led = semaphore1[0]; | |
} else { | |
go(semaphore2); | |
wait(semaphore1); | |
led = semaphore2[0]; | |
} | |
delay(3000); | |
blinking(led); | |
translate(); | |
return !other; | |
} | |
void loop() { | |
pass = stand(pass); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment