Knight Rider w/o wait

22 Nov 2011

Many Knight Riders code out there. I'm trying to implement one using Timers and Interrupts instead of the wait function. This way, can run it within a program and not slow program down. Not sure which timer function would work easiest. donde

23 Nov 2011

Do both. Then you'll know which is more appropriate :)

24 Nov 2011

This one worked well for me.... Although there might be a cleaner way to do it..... =)

#include "mbed.h"

BusOut scanner(p21, p22, p23, p24, p25, p26, p27, p28);
int activeLed = 0;
int scanDir = 1;
float scanDelay = 0.05;

Ticker scanTimer;

void scanTime() {
    if (scanDir == 1) {
        activeLed ++;
        scanner = 0 << activeLed - 1;
        scanner = 1 << activeLed;
        if (activeLed == 7) {
            scanDir = 0;
        }
    } else {
        activeLed --;
        scanner = 0 << activeLed + 1;
        scanner = 1 << activeLed;
        if (activeLed == 0) {
            scanDir = 1;
        }
    }
}

int main() {
    scanTimer.attach(&scanTime, scanDelay);
    while(1){
//     do stuff
    }
}

Now, having said all of that... If you want to use less pins..... I'd suggest something like this http://www.sparkfun.com/products/9056 ....... or even better, something like this.... http://www.sparkfun.com/products/10136

Good luck!

03 Dec 2011
03 Dec 2011

Looks like I need to learn how to present code.

04 Dec 2011

Forgot about "<<code>>" Thanks Mark x,for good looking version of a Knight Rider. I'll try it out next. Follows is my version, probably the longest on record! donde

// Knight Rider using modulo and 2 Timers
// t1 is for "on" time of LEDS, t2 is for "off" time. To speed the train up, half all values of the modulo.


#include "mbed.h"
#include "m3pi.h"
 
m3pi m3pi;

DigitalOut red1(p13);
DigitalOut red2(p14);
DigitalOut red3(p15);
DigitalOut red4(p16);
DigitalOut red5(p17);
DigitalOut red6(p18);
DigitalOut red7(p19);
DigitalOut red8(p20);
 
Timer t1;
Timer t2;

int main() {
  t1.start();
  t2.start();
     while(1) { 
         if (t1.read_ms() % 3200 == 200)
         red1 = 1;
         if (t1.read_ms() % 3200 == 400)
         red2 = 1;
         if (t2.read_ms() % 800 == 50)
         red1 = 0;
         if (t1.read_ms() % 3200 == 600)
         red3 = 1;
         if (t2.read_ms() % 800 == 100)
         red2 = 0;
         if (t1.read_ms() % 3200 == 800)
         red4 = 1;
         if (t2.read_ms() % 800 == 150)
         red3 = 0;
         if (t1.read_ms() % 3200 == 1000)
         red5 = 1;
         if (t2.read_ms() % 800 == 200)
         red4 = 0;
         if (t1.read_ms() % 3200 == 1200)
         red6 = 1;
         if (t2.read_ms() % 800 == 250)
         red5 = 0;
         if (t1.read_ms() % 3200 == 1400)
         red7 = 1;
         if (t2.read_ms() % 800 == 300)
         red6 = 0;
         if (t2.read_ms() % 3200 == 1600)
         red8 = 1;   
          if (t2.read_ms() % 800 == 350)
         red7 = 0;
         if (t1.read_ms() % 3200 == 1600)
         red7 = 1;
         if (t2.read_ms() % 800 == 400)
         red8 = 0;
         if (t1.read_ms() % 3200 == 2000)
         red6 = 1;
         if (t2.read_ms() % 800 == 450)
         red7 = 0;
         if (t1.read_ms() % 3200 == 2200)
         red5 = 1;
         if (t2.read_ms() % 800 == 500)
         red6 = 0;
         if (t1.read_ms() % 3200 == 2400)
         red4 = 1;
         if (t2.read_ms() % 800 == 550)
         red5 = 0;
         if (t1.read_ms() % 3200 == 2600)
         red3 = 1;
         if (t2.read_ms() % 800 == 600)
         red4 = 0;
         if (t1.read_ms() % 3200 == 2800)
         red2 = 1;   
         if (t2.read_ms() % 800 == 650)
         red3 = 0;
         if (t1.read_ms() % 3200 == 3000)
         red1 = 1;
         if (t2.read_ms() % 800 == 700)
         red2 = 0;
         if (t2.read_ms() % 800 == 750)
         red1 = 0;
         }
}  
05 Dec 2011

Hi Don,

I played around with a simple KITT scanner to test PWM output. You can find it at: http://mbed.org/users/wim/programs/mbed_kitt/m14hz7

It uses a ticker to call a statemachine that updates the LEDs at each call. I use pulsewidth modulated outputs to show a trailing tail of the scanning LED. You can expand this to more LEDs by using all PWM outputs on mbed.

Code looks like something this:

// Variables for Heartbeat and Status
Ticker heartbeat;
bool heartbeatflag=false;

PwmOut K1(LED1);
PwmOut K2(LED2);
PwmOut K3(LED3);
PwmOut K4(LED4);

// Heartbeat monitor
void pulse() {
  static int kitt_state=0;
  
    switch (kitt_state) {
      case 0: 
               //  1000
               K1.pulsewidth(KITT_MAX); // K1 On
               K2.pulsewidth(KITT_20);  // K2 Decay
               K3.pulsewidth(KITT_OFF); // K3 Off
               K4.pulsewidth(KITT_OFF); // K4 Off         
               
               kitt_state++;
               break;
      case 1:
               //  0100     
               K1.pulsewidth(KITT_50);  // K1 Decay
               K2.pulsewidth(KITT_MAX); // K2 On
               K3.pulsewidth(KITT_OFF); // K3 Off
               K4.pulsewidth(KITT_OFF); // K4 Off         
                           
               kitt_state++;
               break;            

//snip code

      case 7:             
               //  1000             
               K1.pulsewidth(KITT_MAX); // K1 On
               K2.pulsewidth(KITT_50);  // K2 Decay
               K3.pulsewidth(KITT_20);  // K3 Decay
               K4.pulsewidth(KITT_OFF); // K4 Off         
                           
               kitt_state = 0;
               break;            

    } //switch
                                         
}


void heartbeat_start() {
  heartbeat.attach(&pulse, 0.1);
  
  K1.period(KITT_PERIOD);          // KITT LEDs period

  heartbeatflag = true;
}

void heartbeat_stop() {
  heartbeat.detach();
  heartbeatflag = false;
}


int main() {

  bool running=true;
  char command;
    pc.printf("Hello World!\n\r");

    heartbeat_start();
             
    while(running) {};
}