 const int CLOCKWISE = 1;
 const int COUNTER_CLOCKWISE = 0;
 
+#if defined(__AVR_ATmega328P__)
+
+// Arduino Pins
 const int speedInPin = A0;
 const int rateOfSpeedChangeInPin = A1;
 const int speedOutPin = 9;

  
  if (powerEnabled)
  {
    speedTarget = ReadPotentiometer(speedInPin);
    speedTarget = map(speedTarget, 0, potentiometerCeiling, minimumSpeed, 255);
  
    delayBetweenAdjustments = ReadPotentiometer(rateOfSpeedChangeInPin);
    delayBetweenAdjustments = map(delayBetweenAdjustments, 0, potentiometerCeiling, minimumDelay, maximumDelay);
  }
  else
  {
    speedTarget = 0;
  }

  directionSwitch.update();
  directionTarget = directionSwitch.read();

  if (directionTarget == CLOCKWISE)
  {
    speedTarget = abs(speedTarget);
  }
  else
  {
    speedTarget = -abs(speedTarget);
  }
  
  if (currentSpeed == 0 && currentDirection != directionTarget)
  {
    // We're at zero speed and want to switch directions.
    // The motor spec sheet says we have to wait .5 seconds before
    // switching.

    // TODO: can we just write direction on every loop based on the sign of the speed?
    
    delay(500);
    digitalWrite(directionOutputPin, directionTarget);
    currentDirection = directionTarget;
  }
  
  currentSpeed += SlewToward(currentSpeed, speedTarget);
  analogWrite(speedOutPin, abs(currentSpeed));           

//  if (debugOutputCounter++ == 20)
//  {
//    debugOutputCounter = 0;
//    Serial.print("currentSpeed = " );                       
//    Serial.print(currentSpeed);      
//    Serial.print("\t target = ");      
//    Serial.print(speedTarget);   
//    Serial.print("\t delayBetweenAdjustments = ");      
//    Serial.println(delayBetweenAdjustments);   
//  }
  
  delay(delayBetweenAdjustments);                     
}

boolean ReadSwitch(int pin)
{
  return !digitalRead(pin);
}

int ReadPotentiometer(int pin)
{
  int value;
  
  value = analogRead(pin);
  value = constrain(value, 0, potentiometerCeiling);
  
  return value;

const int directionSwitchInPin = 6;
const int directionOutputPin = 5;

#elif defined( __AVR_ATtinyX4__ )

// ATMEL ATTINY84 / arduino-tiny mapping
//
//                           +-\/-+
//                     VCC  1|    |14  GND
//             (D  0)  PB0  2|    |13  AREF (D 10) (A 0)
//             (D  1)  PB1  3|    |12  PA1  (D  9) (A 1)
//       RESET         PB3  4|    |11  PA2  (D  8) (A 2)
//  PWM  INT0  (D  2)  PB2  5|    |10  PA3  (D  7) (A 3)
//  PWM  (A 7) (D  3)  PA7  6|    |9   PA4  (D  6) (A 4)
//  PWM  (A 6) (D  4)  PA6  7|    |8   PA5  (D  5) (A 5)   PWM
//                           +----+
//
const int speedInPin = A0;
const int rateOfSpeedChangeInPin = A1;
const int speedOutPin = 5;
const int onOffSwitchInPin = 7;
const int directionSwitchInPin = 8;
const int directionOutputPin = 6;

#elif defined( __AVR_ATtinyX5__ )

// ATMEL ATTINY85 / arduino-tiny mapping
//                           +-\/-+
//  Ain0       (D  5)  PB5  1|    |8   VCC
//  Ain3       (D  3)  PB3  2|    |7   PB2  (D  2)  INT0  Ain1
//  Ain2       (D  4)  PB4  3|    |6   PB1  (D  1)        pwm1
//                     GND  4|    |5   PB0  (D  0)        pwm0
//                           +----+
//
// NOTE!!!! This requires use of the RESET pin so either RSTDISBL fuse must be set
// which prevents normal reprogramming or the rateOfChange pot has to be used as a
// rheostat and as R1 of a voltage divider so that the voltage on A0 never goes
// below ~1.5V and resets the MCU.
//
const int speedInPin = A3;
const int rateOfSpeedChangeInPin = A0;
const int speedOutPin = 0;
const int onOffSwitchInPin = 2;
const int directionSwitchInPin = 1;
const int directionOutputPin = 4;

#endif

// Settings and limits
const int switchDebounceTime = 30;
const int minimumDelay = 5;
const int maximumDelay = 40;
const int minimumSpeed = 10;
// 10K ohm potentiometers probably won't get all the way to 1024
// and we want to make sure that max feasible input = max motor speed
const int potentiometerCeiling = 1000;
// For prototyping we want to avoid disabling RESET so we need to arrange
// the circuit to keep a minimum voltage so we don't RESET the MCU.
// For production we'll change the fuses and set this to 0.
const int rateOfSpeedChangePotentiometerFloor = 800;

// Switches using the Bounce library for debouncing
Bounce onOffSwitch = Bounce(onOffSwitchInPin, switchDebounceTime);
Bounce directionSwitch = Bounce(directionSwitchInPin, switchDebounceTime);

// State variables
int targetSpeed = 0;
int currentSpeed = 0;
int delayBetweenAdjustments = 15;
boolean powerEnabled = false;
int targetDirection = CLOCKWISE;
int currentDirection = CLOCKWISE;

void setup()
{
  pinMode(onOffSwitchInPin, INPUT_PULLUP);
  pinMode(directionSwitchInPin, INPUT_PULLUP);
  pinMode(directionOutputPin, OUTPUT);
  // We don't have to set pin mode for analog inputs
  
  // TODO: check startup logic to make sure this is right
  digitalWrite(directionOutputPin, targetDirection);
}

void loop()
