simplify wiring, keep right and left separated
[challenge-bot] / phase0 / phase0.ino
index 8a802693be9b51b733e96d7f400608e7449bd7c9..feb0caac8f4cc186184f8b2081fbae289f180f9c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- This program is free software: you can redistribute it and/or modify
   This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU Affero General Public License as
     published by the Free Software Foundation, either version 3 of
     the License, or (at your option) any later version.
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-/*
-  Blink
-  Turns on an LED on for one second, then off for one second, repeatedly.
 
-  This example code is in the public domain.
+/*
+ * traffic lights
  */
+
 int red0 = 12;
 int yellow0 = 11;
 int green0 = 10;
@@ -27,9 +26,6 @@ int yellow1 = 6;
 int green1 = 5;
 
 void setup() {
-  // initialize the digital pin as an output.
-  // Pin 13 has an LED connected on most Arduino boards:
-
   pinMode(red0, OUTPUT);
   pinMode(red1, OUTPUT);
   pinMode(yellow0, OUTPUT);
@@ -38,53 +34,64 @@ void setup() {
   pinMode(green1, OUTPUT);
 }
 
-void on(int led) {
-  digitalWrite(led, HIGH);
+/*
+  on(pinNumber);
+  turn on pin 'pinNumber'.
+  digitalWrite(pinNumber, HIGH); turns the voltage on for that pin.
+ */
+void on(int pinToTurnOn) {
+  digitalWrite(pinToTurnOn, HIGH);
 }
 
-void off(int led) {
-  digitalWrite(led, LOW);
+/*
+  off(pinNumber);
+  turn off pin 'pinNumber'.
+  digitalWrite(pinNumber, LOW); turns the voltage off for that pin.
+*/
+void off(int pinToTurnOff) {
+  digitalWrite(pinToTurnOff, LOW);
 }
 
-void ons_offs(int* ons, int onsc,
-              int* offs, int offsc,
-              int delay_time) {
-  int ii;
-  for(ii = 0; ii < offsc; ii++) {
-    off(offs[ii]);
+void onsOffs(int* pinsToTurnOn, int numberOfPinsToTurnOn,
+             int* pinsToTurnOff, int numberOfPinsToTurnOff){
+  // generic index used for arrays of pins to turn on or off
+  int index;
+  // turn off pins in pinsToTurnOff
+  for(index = 0; index < numberOfPinsToTurnOff; index++) {
+    off(pinsToTurnOff[index]);
   }
-  for(ii = 0; ii < onsc; ii++) {
-    on(ons[ii]);
+  // turn on pins in pinsToTurnOn
+  for(index = 0; index < numberOfPinsToTurnOn; index++) {
+    on(pinsToTurnOn[index]);
   }
-  delay(delay_time);
 }
 
 void loop() {
-  int red_wait = 300;
-  int yellow_wait = 800;
-  int green_wait = 1600;
+  int redWait = 300;
+  int yellowWait = 800;
+  int greenWait = 1600;
 
-  ons_offs((int[]){red0, green1}, 2,
-           (int[]){yellow0, green0, red1, yellow1}, 4,
-           green_wait);
+  onsOffs((int[]){red0, green1}, 2,
+           (int[]){yellow0, green0, red1, yellow1}, 4);
+  delay(greenWait);
 
-  ons_offs((int[]){red0, yellow1}, 2,
-           (int[]){yellow0, green0, red1, green1}, 4,
-           yellow_wait);
+  onsOffs((int[]){red0, yellow1}, 2,
+           (int[]){yellow0, green0, red1, green1}, 4);
+  delay(yellowWait);
 
-  ons_offs((int[]){red0, red1}, 2,
-           (int[]){yellow0, green0, yellow1, green1}, 4,
-           red_wait);
+  onsOffs((int[]){red0, red1}, 2,
+           (int[]){yellow0, green0, yellow1, green1}, 4);
+  delay(redWait);
 
-  ons_offs((int[]){green0, red1}, 2,
-           (int[]){red0, yellow0, yellow1, green1}, 4,
-           green_wait);
+  onsOffs((int[]){green0, red1}, 2,
+           (int[]){red0, yellow0, yellow1, green1}, 4);
+  delay(greenWait);
 
-  ons_offs((int[]){yellow0, red1}, 2,
-           (int[]){red0, green0, yellow1, green1}, 4,
-           yellow_wait);
+  onsOffs((int[]){yellow0, red1}, 2,
+           (int[]){red0, green0, yellow1, green1}, 4);
+  delay(yellowWait);
 
-  ons_offs((int[]){red0, red1}, 2,
-           (int[]){yellow0, green0, yellow1, green1}, 4,
-           red_wait);
+  onsOffs((int[]){red0, red1}, 2,
+           (int[]){yellow0, green0, yellow1, green1}, 4);
+  delay(redWait);
 }