| 1 | /* |
| 2 | This program is free software: you can redistribute it and/or modify |
| 3 | it under the terms of the GNU Affero General Public License as |
| 4 | published by the Free Software Foundation, either version 3 of |
| 5 | the License, or (at your option) any later version. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU Affero General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Affero General Public License |
| 13 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * traffic lights |
| 18 | */ |
| 19 | |
| 20 | int red0 = 12; |
| 21 | int yellow0 = 11; |
| 22 | int green0 = 10; |
| 23 | |
| 24 | int red1 = 7; |
| 25 | int yellow1 = 6; |
| 26 | int green1 = 5; |
| 27 | |
| 28 | void setup() { |
| 29 | pinMode(red0, OUTPUT); |
| 30 | pinMode(red1, OUTPUT); |
| 31 | pinMode(yellow0, OUTPUT); |
| 32 | pinMode(yellow1, OUTPUT); |
| 33 | pinMode(green0, OUTPUT); |
| 34 | pinMode(green1, OUTPUT); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | on(pinNumber); |
| 39 | turn on pin 'pinNumber'. |
| 40 | digitalWrite(pinNumber, HIGH); turns the voltage on for that pin. |
| 41 | */ |
| 42 | void on(int pinToTurnOn) { |
| 43 | digitalWrite(pinToTurnOn, HIGH); |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | off(pinNumber); |
| 48 | turn off pin 'pinNumber'. |
| 49 | digitalWrite(pinNumber, LOW); turns the voltage off for that pin. |
| 50 | */ |
| 51 | void off(int pinToTurnOff) { |
| 52 | digitalWrite(pinToTurnOff, LOW); |
| 53 | } |
| 54 | |
| 55 | void onsOffs(int* pinsToTurnOn, int numberOfPinsToTurnOn, |
| 56 | int* pinsToTurnOff, int numberOfPinsToTurnOff){ |
| 57 | // generic index used for arrays of pins to turn on or off |
| 58 | int index; |
| 59 | // turn off pins in pinsToTurnOff |
| 60 | for(index = 0; index < numberOfPinsToTurnOff; index++) { |
| 61 | off(pinsToTurnOff[index]); |
| 62 | } |
| 63 | // turn on pins in pinsToTurnOn |
| 64 | for(index = 0; index < numberOfPinsToTurnOn; index++) { |
| 65 | on(pinsToTurnOn[index]); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void loop() { |
| 70 | int redWait = 300; |
| 71 | int yellowWait = 800; |
| 72 | int greenWait = 1600; |
| 73 | |
| 74 | onsOffs((int[]){red0, green1}, 2, |
| 75 | (int[]){yellow0, green0, red1, yellow1}, 4); |
| 76 | delay(greenWait); |
| 77 | |
| 78 | onsOffs((int[]){red0, yellow1}, 2, |
| 79 | (int[]){yellow0, green0, red1, green1}, 4); |
| 80 | delay(yellowWait); |
| 81 | |
| 82 | onsOffs((int[]){red0, red1}, 2, |
| 83 | (int[]){yellow0, green0, yellow1, green1}, 4); |
| 84 | delay(redWait); |
| 85 | |
| 86 | onsOffs((int[]){green0, red1}, 2, |
| 87 | (int[]){red0, yellow0, yellow1, green1}, 4); |
| 88 | delay(greenWait); |
| 89 | |
| 90 | onsOffs((int[]){yellow0, red1}, 2, |
| 91 | (int[]){red0, green0, yellow1, green1}, 4); |
| 92 | delay(yellowWait); |
| 93 | |
| 94 | onsOffs((int[]){red0, red1}, 2, |
| 95 | (int[]){yellow0, green0, yellow1, green1}, 4); |
| 96 | delay(redWait); |
| 97 | } |