/*
 * Board:   Teensy 3.6
 * Target:  SMAD Displays (Max's "Poor Man's" Servo-Driven Robot Head)
 * Author:  Clive "Max" Maxfield (max@clivemaxfield.com) 
 * License: The MIT License (See full license at the bottom of this file)
 *
 *          V01 Read pots (1X) and display their values in serial monitor
 *          V02 Read pots (5X and average) and display their values in serial monitor
 */

#define NUM_POTS              6
#define NUM_POT_SAMPLES       5


// Assign any input/output (I/O) pins
const int PinsPots[NUM_POTS] = {A18, A14, A19, A17, A15, A16};


// Define any global variables
int PotVals[NUM_POTS];

char *PotNames[] = {"LLR", "LFB", "LRT", "RLR", "RFB", "RRT"}; 



void setup ()
{
    Serial.begin(9600);
    delay(2000);
    Serial.println("====== Start ======");    
}


void loop ()
{
    ReadPots();
    DisplayPotVals();
    delay(1000);
}



//=============================================================================
// Utility Functions
//=============================================================================

void ReadPots ()
{
    for (int iPot = 0; iPot < NUM_POTS; iPot++)
    {
        PotVals[iPot] = 0;

        for (int iSmp = 0; iSmp < NUM_POT_SAMPLES; iSmp++)
        {
            PotVals[iPot] += analogRead(PinsPots[iPot]);
        }

        PotVals[iPot] /= NUM_POT_SAMPLES;
    }
}


void DisplayPotVals ()
{
    for (int iPot = 0; iPot < NUM_POTS; iPot++)
    {
        Serial.print(PotNames[iPot]);
        Serial.print(" =");
        if (PotVals[iPot] < 1000) Serial.print(" ");
        if (PotVals[iPot] <  100) Serial.print(" ");
        if (PotVals[iPot] <   10) Serial.print(" ");
        Serial.print(PotVals[iPot]);        
        Serial.print("   ");
    }
    Serial.println();
}


/*
 * Copyright (c) 2022 Clive "Max" Maxfield
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and any associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHOR(S) OR COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */