This is my Arduino “Hello World” project, inspired by my TV set’s please wait indicator (that reminds me of Knight Rider’s K.I.T.T car).
The two potentiometers on the video were used to adjust speed and maximum brightness of the LEDs (variables maxBrightness and stepDelay). In the sketch below they are still fixed.
Thanks to the Crayon WordPress plugin, posted Arduino sketches will now look (almost) like they do in the Arduino IDE. The following sketch will produce the above variant of the ‘Larson scanner’, better known as the KITT scanner from the TV series Knight Rider.
Pin numbers are for the Arduino Uno (must be PWM pins). Note that by using a two-dimensional ledPins array and toggling its first index, there’s no need to have separate loops for both directions (outer for loop). The code for fading can be kept simple by introducing virtual LEDs (inner for loop).
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 |
// Preferences (optionally read - within loop() - from potentiometers/sensors attached to analog pins) const int maxBrightness = 200; // brightness of the 'leader' LED ( range: [0,255], best: [50,200] ) const float dimFactor = 0.35; // a LED's brightness reduction factor relative to its 'predecessor' (sort of sets the visible 'tail length', best value depends on maxBrightness) const int stepDelay = 40; // LED shift delay ( optically best on Arduino Uno: [25,100] ) const int bounceDelay = 200; // Delay between direction switch (a 'natural' return into visibility of the 'leader' over virtual positions would take appr. stepDelay*(pinCount-1) msec.) // Setup dependent declarations: const int pinCount = 6; // number of LEDs (connected to PWM pins) byte ledPins[2][pinCount] = {{3, 5, 6, 9, 10, 11},{11, 10, 9, 6, 5, 3}}; // used PWM pins in forward and backward order int direction = 0; // toggle for ledPins array's 1st index (changing direction) void setup() { for (int thisPin = 0; thisPin < pinCount; thisPin++) { pinMode(ledPins[0][thisPin], OUTPUT); } } void loop() { for (int p = 0; p < 2*pinCount-1; p++) { // p values from pinCount to (2*pinCount-1) refer to 'virtual' positions (invisible LEDs) int intensity = maxBrightness; for (int j = p; j >= 0; j--) { if (j < pinCount) { // don't write to 'virtual' LEDs analogWrite(ledPins[direction][j],intensity); } intensity = intensity*dimFactor; } delay(stepDelay); } delay(bounceDelay); direction ^= 1; // bitwise toggle (change direction) } |