From 3bebd54178be8e896200eafe49933c9c0914517e Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 17 Jun 2015 15:47:44 +0200 Subject: Init turret sequence docs + logical --- Venus_Skeleton/Venus_Skeleton.ino | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Venus_Skeleton/Venus_Skeleton.ino b/Venus_Skeleton/Venus_Skeleton.ino index 87b8449..f061f0c 100644 --- a/Venus_Skeleton/Venus_Skeleton.ino +++ b/Venus_Skeleton/Venus_Skeleton.ino @@ -177,22 +177,23 @@ long microsecondsToCentimeters(long microseconds) return microseconds / 29 / 2; } +/** + * Given the number of positions $n$, fills the lower half of the array with 0, + * 2, 4, ..., $n-1$, then continues filling the upper half with $n-2$, $n-4$, + * ..., 5, 3, 1. + */ void initTurretSequence(int array[], int numOfPositions) { - int sign = 1; - int x = 0; - - for (int i = 0; i < numOfPositions; i++) { + // for n=7, fill lower half of array with 0, 2, 4, 6 + // for n=8, fill lower half of array with 0, 2, 4, 6 + for (int i = 0; i < numOfPositions / 2; i++) { + array[i] = i * 2; + } + // for n=7, fill upper half of array with 5, 3, 1 + // for n=8, fill upper half of array with 7, 5, 3, 1 + int x = numOfPositions / 2 * 2 - 1; + for (int i = numOfPositions / 2 + 1; i < numOfPositions; i++) { array[i] = x; - x += sign * 2; - - if(x > numOfPositions) { - sign = -1; - x -= 3; - } - if(x == numOfPositions) { - sign = -1; - x -= 1; - } + x -= 2; } } -- cgit v1.2.1