KARR - Arduino - Nephasto


The front lights of KITT's evil brother from the mythical series 'Knight Rider', recreated in Arduino.





The first prototype of KITT was called KARR (Knight Automated Roving Robot). Something went wrong with its programming and it turned evil. With this simple project we are going to recreate the famous lights he had on his nose on an Arduino board.

For this to be a really evil project the LEDs we use must be the color that KARR had, yellow, not red (which KITT had).

Arduino.

Basically we are going to use the Arduino’s digital outputs and simulate using pulse modulation (or PWM) an analog signal to create the “fade” effect on the LEDs. Here is the schematic:

 

As you can see we need:

  • An Arduino board.
  • A test board.
  • 10 resistors of 220Ω.
  • 10 yellow leds.
  • Wires.

And this is the code:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// \brief   KARR, KITT's evil brother.
//          Cycling of LEDS simulating fades using PWM.
// \license MIT open-source license
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
// Constants.
////////////////////////////////////////////////////////////////////

const unsigned int LED_COUNT = 10;                // Number of LEDs.
const unsigned int START_PIN = 4;                 // First pin used.
const unsigned long LED_DELAY_TIME = 1;           // Time to lower the brightness of an LED.
const unsigned long POINTER_DELAY_TIME = 75;      // Time for pointer to move.

////////////////////////////////////////////////////////////////////
// It sets its maximum brightness and decreases until it reaches 0.
////////////////////////////////////////////////////////////////////
class Led
{
public:
  Led()
  {
    pin = brightness = 0;
    delayTime = 0;
  }

  void Setup(unsigned int thePin)
  {
    pin = thePin;
    pinMode(pin, OUTPUT);  // Inininializes the pin as output.
  }

  void Update()
  {
    if (brightness > 0 && (millis() - delayTime >= LED_DELAY_TIME))
    {
      delayTime = millis();
      analogWrite(pin, brightness--);
    }
  }
  
  void SetHigh()
  {
    brightness = 255;
    delayTime = millis();
    analogWrite(pin, brightness);
  }

private:
  unsigned int pin;
  unsigned int brightness;
  unsigned long delayTime;
};


////////////////////////////////////////////////////////////////////
// Moves between the first LED and the last LED, setting the
// maximum brightness as it passes.
////////////////////////////////////////////////////////////////////
class Pointer
{
public:
  Pointer()
  {
    currentPosition = 0;
    delaySpeed = time = 0;
    dir = 0;
    leds = NULL;
    ledsCount = 0;
  }

  void Setup(unsigned long theDelay, Led *theLeds, unsigned int theLedsCount)
  {
    currentPosition = 0;
    dir = 1;
    delaySpeed = theDelay;
    leds = theLeds;
    ledsCount = theLedsCount;
    time = 0;
  }
  
  void Update()
  {
    if (millis() - time >= delaySpeed)
    {
      leds[currentPosition].SetHigh();

      currentPosition += dir;
      
      if (currentPosition <= 0 || currentPosition >= (ledsCount - 1))
        dir *= -1;

      time = millis();
    }
  }

private:
  int currentPosition;
  unsigned long delaySpeed;
  unsigned long time;
  int dir;

  Led *leds;
  unsigned int ledsCount;
};


Led led[LED_COUNT];
Pointer pointer;

void setup()
{
  pointer.Setup(POINTER_DELAY_TIME, led, LED_COUNT);

  for (int i = 0; i < LED_COUNT; ++i)
    led[i].Setup(i + START_PIN);
}


void loop()
{
  pointer.Update();

  for (int i = 0; i < LED_COUNT; ++i)
    led[i].Update();
}

And it looks like this once assembled: