Knight Rider 2008

Hola a todos, que tal, espero que todos perfectamente bien, les escribo desde chile, estoy hace ya un tiempo, de hacer en un arduino mega, las luces del auto fantástico, pero las de la versión 2008, para esto tengo el siguiente código:

int ledpin[] ={3, 5, 6, 9, 10, 11}; //organize all PWM pins neatly.
int n = 0; //speed counter.
int i =0; // repeater.
int value[] ={0,0,0,0,0,0};

void setup() {
  // put your setup code here, to run once:

}

void RPT() {
  // put your main code here, to run repeatedly:
    {
 //time codes for light sequence.
 //I designed this to very flexible you could put anything here
 //this particular sequence presents a nice nightrider effect.
 // 100 = 39% power. I was too lazy to find enough
 //resistors to run my 20ma LEDs at full power.
 //change these to 255 if you have properly limited resistors.
 if(n == 100){value[0]=100;}
 if(n == 200){value[0]=100;}
 if(n == 300){value[1]=100;}
 if(n == 400){value[2]=100;}
 if(n == 500){value[3]=100;}
 if(n == 600){value[4]=100;}
 if(n == 700){value[5]=100;}
 if(n == 800){value[5]=100;}
 if(n == 900){value[4]=100;}
 if(n == 1000){value[3]=100;}
 if(n == 1100){value[2]=100;}
 if(n == 1200){value[1]=100;}
 if(n >= 1200){n =0;}
 
 //fade all pins
 for (i  = 0; i <=5; i++)
 {
   if(value[i] >= 5){ value[i]-=1;}
   else if (value[i] < 5) { value[i]=0;}
 }
 
//write to all pins
for (i  = 0; i <=5; i++)
  {
    analogWrite(ledpin[i], value [i]);
  }
  n+=5; // time stepper. Tweak at your own risk.
  delay(5); //speed modifier. Tweak at your own risk!
}
}

void loop(){
  RPT();
}

Este código está pensado para un arduino nano, pero, yo quiero hacerlo con un arduino mega que tengo, la cuestión es que no entiendo como hacerlo para que ande en más pines, por más que intente modificar la cantidad de pines no funciona,

Por otro lado tengo este otro código, pero quiero que al moverse de lado a lado se vaya desvaneciendo, no solo apagándose, como es este código pensado en un nano, pero que van encendiendo y apagando uno a uno, no desvaneciéndose, quizá este es un tema recontra requete hablado, pero, la verdad no he encontrado, y lo que hay o había, ya no está, les muestro el código, desde ya les estoy infinitamente agradecido

int cycleTime = 2500;
int pins[] = {  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int numPins = 11;
int numCycles = 58;
int cycle = 0;
int pinDigital[] = {0, 0, 0, 0, 0, 0, 0, 0,0,0,0};
int pinStatus[] = {0, 0, 0, 0, 0, 0, 0, 0,0,0,0};
int pinIntensity[] = {0, 0, 0, 0, 0, 0, 0, 0,0,0,0};
int intensityCurve[] = {254, 200, 150, 100, 50, 0};
int intensityMark = 50;
int timer = 0;
int cycleDefs[58][11] = 
  {
//    {5,0,5,0,5,0,5,0,5}, 
//    {0,0,5,5,5,5,0,0,0},
    /*{0,0,0,0,0,0,0,0,0,0,0},*/
    {1,0,0,0,0,0,0,0,0,0,0}, 
    {2,1,0,0,0,0,0,0,0,0,0}, 
    {3,2,1,0,0,0,0,0,0,0,0}, 
    {4,3,2,1,0,0,0,0,0,0,0}, 
    {5,4,3,2,1,0,0,0,0,0,0}, 
    {5,5,4,3,2,1,0,0,0,0,0}, 
    {5,5,5,4,3,2,1,0,0,0,0}, 
    {5,5,5,5,4,3,2,1,0,0,0}, 
    {5,5,5,5,5,4,3,2,1,0,0}, 
    {5,5,5,5,5,5,4,3,2,1,0}, 
    {5,5,5,5,5,5,5,4,3,2,1}, 
    {5,5,5,5,5,5,5,5,4,3,2}, 
    {5,5,5,5,5,5,5,5,5,4,3},
    {5,5,5,5,5,5,5,5,5,5,4},
    {5,5,5,5,5,5,5,5,5,5,5},
    {4,5,5,5,5,5,5,5,5,5,5}, 
    {3,4,5,5,5,5,5,5,5,5,5}, 
    {2,3,4,5,5,5,5,5,5,5,5}, 
    {1,2,3,4,5,5,5,5,5,5,5}, 
    {0,1,2,3,4,5,5,5,5,5,5}, 
    {0,0,1,2,3,4,5,5,5,5,5}, 
    {0,0,0,1,2,3,4,5,5,5,5}, 
    {0,0,0,0,1,2,3,4,5,5,5}, 
    {0,0,0,0,0,1,2,3,4,5,5}, 
    {0,0,0,0,0,0,1,2,3,4,5}, 
    {0,0,0,0,0,0,0,1,2,3,4},
    {0,0,0,0,0,0,0,0,1,2,3},
    {0,0,0,0,0,0,0,0,0,1,2},
    {0,0,0,0,0,0,0,0,0,0,1},
    /*{0,0,0,0,0,0,0,0,0,0,0},*/
//    {5,0,5,0,5,0,5,0,5}, 

    /*{0,0,0,0,0,0,0,0,0,0,0},  */
    {0,0,0,0,0,0,0,0,0,0,1}, 
    {0,0,0,0,0,0,0,0,0,1,2}, 
    {0,0,0,0,0,0,0,0,1,2,3}, 
    {0,0,0,0,0,0,0,1,2,3,4}, 
    {0,0,0,0,0,0,1,2,3,4,5}, 
    {0,0,0,0,0,1,2,3,4,5,5}, 
    {0,0,0,0,1,2,3,4,5,5,5}, 
    {0,0,0,1,2,3,4,5,5,5,5}, 
    {0,0,1,2,3,4,5,5,5,5,5}, 
    {0,1,2,3,4,5,5,5,5,5,5}, 
    {1,2,3,4,5,5,5,5,5,5,5},
    {2,3,4,5,5,5,5,5,5,5,5},
    {3,4,5,5,5,5,5,5,5,5,5},
    {4,5,5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5,5,4}, 
    {5,5,5,5,5,5,5,5,5,4,3}, 
    {5,5,5,5,5,5,5,5,4,3,2}, 
    {5,5,5,5,5,5,5,4,3,2,1}, 
    {5,5,5,5,5,5,4,3,2,1,0}, 
    {5,5,5,5,5,4,3,2,1,0,0}, 
    {5,5,5,5,4,3,2,1,0,0,0}, 
    {5,5,5,4,3,2,1,0,0,0,0}, 
    {5,5,4,3,2,1,0,0,0,0,0}, 
    {5,4,3,2,1,0,0,0,0,0,0}, 
    {4,3,2,1,0,0,0,0,0,0,0}, 
    {3,2,1,0,0,0,0,0,0,0,0},
    {2,1,0,0,0,0,0,0,0,0,0},
    {1,0,0,0,0,0,0,0,0,0,0},
    /*{0,0,0,0,0,0,0,0,0,0,0}*/
  
    };


  
void setup()
{
  int i;
//  Serial.begin(9600);
//  Serial.println("Program Loaded");  
  for (i = 0; i < numPins; i++)
  {   
    pinMode(pins[i], OUTPUT);    
  } 
  pinMode(13,OUTPUT);
}


void loop()
{
  
  int i;
  int tmp;

  
  timer++;
  if((timer % intensityMark)==0)
  {
    for(i = 0; i<numPins; i++)
    {
      int targetIntensity = intensityCurve[pinStatus[i]];
      if(targetIntensity > pinIntensity[i])
      {
        pinIntensity[i]++;
      }
      else if(targetIntensity < pinIntensity[i])  
      {
        pinIntensity[i]--;
      }
    }
  }

  for (i = 0; i < numPins; i++) 
  { 
    if((timer % intensityMark) >= pinIntensity[i])
    {
      if(pinDigital[i] == 0)
      {
         pinDigital[i] = 1;
         digitalWrite(pins[i],HIGH);
      }
     
    }
    else
    {
      if(pinDigital[i] == 1)
      {
         pinDigital[i] = 0;
         digitalWrite(pins[i],LOW);
             //for(int b=255; b>=256; b--){
             //  analogWrite(pins[i],b);
               //delay(2);
             //}
      }
    }
  }
  if(timer == cycleTime)
  {
    timer = 0; 
    cycle++;
  }
  else
  {
    return;
  }
//  Serial.read();
  if(cycle >= (numCycles))
  {
    cycle = 0; 
    //digitalWrite(13,HIGH);
  }
  if(cycle == 20)
  {
    //digitalWrite(13,LOW);   
  }

  for(i=0; i<numPins; i++)
  {
    pinStatus[i] = cycleDefs[cycle][i];  
  }

}

como dije anteriormente desde ya les agradezco cualquier ayuda, soy nobato aún en arduino, y la gran mayoría las he sacado al ojo, saludos a todos desde chile!

Moderador:
Bienvenido al Foro Arduino en Español.
Por favor, lee las Normas del foro y edita tu código usando etiquetas de código.
Ve a edición, luego selecciona todo el error que has publicado, lo cortas y click en </>


echo, gracias, saludos!

No lo tomes a mal pero si no lees y te informas del MEGA será dificil entender las cosas, o encontrar algo en internet que haga lo que quieres.

Pasar de un UNO/NANO a un MEGA no es gran cosa.
El Mega tiene 54 pines I/O entrada/salida. O sea el UNO/NANO esta nadando en el MEGA, si se entiende la anolgía.
Entoces buscas MEGA y como se llaman los pines y los agregas a esto

int pins[] = {  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

Empiezas en el 22 y terminas en el 53 segun este esquema de los pines. Ver conector parte inferior

Vamos al código.
Desvanecer en arduino solo se puede lograr con analogWrite(pin, valor) donde pin es el que estas usando y valor va de 0 a 255 con 0 para apagado y 255 para encendido o máx brillo. Si colocas el LED con Anódo a VCC y cátodo mirando al pin Arduino via una Resistencia.

Entonces, a un código 1/0 como el que tienes debes agregarle contadores que lleven el valor de desvanecimiento o de crecimiento de la iluminación.

Estoy viendo el código para hacerte alguna sugerencia.

Me temo que no vas a poder añadir mas led segun las paginas 367 y 368 de la hoja de características del ATmega 1280 o 2560. Como 'máximo' puede dar 200mA entre todos sus pines y ya con los que tienes estas teoricamente en 220 mA. Te acondejo que si quieres mas led uses una tira tipo Neopixel, tendras los led RGB que quieras y manejados por un solo pin. Podria quedar algo asi:Raider - YouTube
24/08/2021 Edito y añado su codigo:


#include <Adafruit_NeoPixel.h>
#define PIN  12 //pin Datos
#define DELAYVAL 30 //Velocidad tira
#define NUMPIXELS 24 //Numero diodos

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int Red, Green, Blue, Pixel_1, Pixel_2;

void setup() {
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}
void loop() {
  aleatorio();
  //*********************** I D A **************************************
  for (int x = 0; x < NUMPIXELS; x++) {
    for (int y = 0; y < (NUMPIXELS / 2); y++) {
      Pixel_1 = x - y;
      if (Pixel_1 < (NUMPIXELS / 2) && Pixel_1 >= 0) {
        pixels.setPixelColor(Pixel_1, pixels.Color(Red, Green, Blue));
      }
      Pixel_2 = NUMPIXELS - 1 - Pixel_1;
      if (Pixel_2 < NUMPIXELS && Pixel_2 >= (NUMPIXELS / 2)) {
        pixels.setPixelColor(Pixel_2, pixels.Color(Red, Green, Blue));
      }
    }
    ilumina();
  }
  delay(DELAYVAL);
  //*********************************************************************
  //*********************** V U E L T A *********************************
  for (int x = (NUMPIXELS / 2); x >= -(NUMPIXELS / 2); x--) {
    for (int y = 0; y < (NUMPIXELS / 2); y++) {
      Pixel_1 = x + y;
      if (Pixel_1 < (NUMPIXELS / 2) && Pixel_1 >= 0) {
        pixels.setPixelColor(Pixel_1, pixels.Color(Red, Green, Blue));
      }
      Pixel_2 = NUMPIXELS - 1 - Pixel_1;
      if (Pixel_2 >= (NUMPIXELS / 2) && Pixel_2 <= NUMPIXELS) {
        pixels.setPixelColor(Pixel_2, pixels.Color(Red, Green, Blue));
      }
    }
    ilumina();
  }
  delay(DELAYVAL);
  //*********************************************************************
}
void ilumina () {
  pixels.show();
  delay(DELAYVAL);
  pixels.clear();
}

void aleatorio () {
  Red = 5;
  Green = 0;
  Blue = 0;
//  Red = random(0, 256) / 20;
//  Green = random(0, 256 / 20);
//  Blue = random(0, 256) / 40;
}

Aunque a mi me gustaba mas el antiguo que tambien era igual a la de los Cylon,(serie Battlestar Galactica):Cylon - YouTube
24/08/2021 Edito y añado su codigo:


#include <Adafruit_NeoPixel.h>
#define PIN  12 //pin PWM
#define NUMPIXELS 8 //Numero diodos
#define DELAYVAL 70
int potencia = 10;// Es un divisor potencia maxima=1

int expo [9] { 1, 2, 4, 8, 16, 32, 64, 128, 254};
int  Red, Green, Blue, a, b, c, duration, distance;

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


void setup() {
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
 }

void loop() {
  a = random(1, 4);
  b = random(0, 80);
  c = random(0, 80);
  switch (a) {
    case 1: {
        Red = 255;
        Green = b;
        Blue = c;
        break;
      }
    case 2: {
        Red = b;
        Green = 255;
        Blue = c;
        break;
      }
    case 3: {
        Red = b;
        Green = c;
        Blue = 255;
        break;
      }
  }
  Red = Red / potencia;
  Green = Green / potencia;
  Blue = Blue / potencia;

    //IDA
    for (int x = 0; x <= 14; x++) {
      for (int y = 0; y <= 8; y++) {
        pixels.setPixelColor(x - y, pixels.Color(Red / expo [y] , Green / expo [y] , Blue / expo [y]));
      }
      pixels.show();
      delay(DELAYVAL);
      pixels.clear();
    }
    //VUELTA
    for (int x = 7; x >= -7; x--) {
      for (int y = 0; y <= 8; y++) {
        pixels.setPixelColor(x + y, pixels.Color(Red / expo [y] , Green / expo [y] , Blue / expo [y]));
      }
      pixels.show();
      delay(DELAYVAL);
      pixels.clear();
    }
  }

Saludos.

gracias compa, pero, es posible entonces hacer lo del nano en el mega, pero con 8 leds?, alguien que sepa como modificar el código para 6 leds, pero con 8 leds y en el mega, lo que quiero hacer es la del auto fantastico, con un mega y 8 leds, muchas gracias

Hazlo directamente, los pines del NANO en el MEGA.

lo hice, pero el código corto, no se como modificarlo, pq al intentar agregar más de los pines que dice, o, no funciona o no hace nada

int ledpin[] ={3, 5, 6, 9, 10, 11}; //organize all PWM pins neatly.
int n = 0; //speed counter.
int i =0; // repeater.
int value[] ={0,0,0,0,0,0};

void setup() {
  // put your setup code here, to run once:

}

void RPT() {
  // put your main code here, to run repeatedly:
    {
 //time codes for light sequence.
 //I designed this to very flexible you could put anything here
 //this particular sequence presents a nice nightrider effect.
 // 100 = 39% power. I was too lazy to find enough
 //resistors to run my 20ma LEDs at full power.
 //change these to 255 if you have properly limited resistors.
 if(n == 100){value[0]=100;}
 if(n == 200){value[0]=100;}
 if(n == 300){value[1]=100;}
 if(n == 400){value[2]=100;}
 if(n == 500){value[3]=100;}
 if(n == 600){value[4]=100;}
 if(n == 700){value[5]=100;}
 if(n == 800){value[5]=100;}
 if(n == 900){value[4]=100;}
 if(n == 1000){value[3]=100;}
 if(n == 1100){value[2]=100;}
 if(n == 1200){value[1]=100;}
 if(n >= 1200){n =0;}
 
 //fade all pins
 for (i  = 0; i <=5; i++)
 {
   if(value[i] >= 5){ value[i]-=1;}
   else if (value[i] < 5) { value[i]=0;}
 }
 
//write to all pins
for (i  = 0; i <=5; i++)
  {
    analogWrite(ledpin[i], value [i]);
  }
  n+=5; // time stepper. Tweak at your own risk.
  delay(5); //speed modifier. Tweak at your own risk!
}
}

void loop(){
  RPT();
}

entonces, esa es la razón de mi padecer..., gracias de todas maneras por el tiempo...

Toma aqui lo tienes, es mas facil que veas tu los cambios que yo explicarlos, lo mismo si quieres poner mas led, pero ojo con el limite de los 200mA totales.

int ledpin[] ={3, 4, 5, 6, 7, 8, 9, 10}; //organize all PWM pins neatly.
int n = 0; //speed counter.
int i =0; // repeater.
int value[] ={0,0,0,0,0,0,0,0};

void setup() {
  // put your setup code here, to run once:

}

void RPT() {
  // put your main code here, to run repeatedly:
    {
 //time codes for light sequence.
 //I designed this to very flexible you could put anything here
 //this particular sequence presents a nice nightrider effect.
 // 100 = 39% power. I was too lazy to find enough
 //resistors to run my 20ma LEDs at full power.
 //change these to 255 if you have properly limited resistors.
 if(n == 100){value[0]=100;}
 if(n == 200){value[0]=100;}
 if(n == 300){value[1]=100;}
 if(n == 400){value[2]=100;}
 if(n == 500){value[3]=100;}
 if(n == 600){value[4]=100;}
 if(n == 700){value[5]=100;}
 if(n == 700){value[6]=100;}
 if(n == 700){value[7]=100;}
 if(n == 700){value[7]=100;}
 if(n == 700){value[6]=100;}
 if(n == 800){value[5]=100;}
 if(n == 900){value[4]=100;}
 if(n == 1000){value[3]=100;}
 if(n == 1100){value[2]=100;}
 if(n == 1200){value[1]=100;}
 if(n >= 1200){n =0;}
 
 //fade all pins
 for (i  = 0; i <=7; i++)
 {
   if(value[i] >= 7){ value[i]-=1;}
   else if (value[i] < 7) { value[i]=0;}
 }
 
//write to all pins
for (i  = 0; i <=7; i++)
  {
    analogWrite(ledpin[i],value [i]);
  }
  n+=5; // time stepper. Tweak at your own risk.
  delay(5); //speed modifier. Tweak at your own risk!
}
}

void loop(){
  RPT();
}

saludos.

El segundo codigo funciona en el mega sin tocar nada, eso si el Knight Rider esta un poco tuerto.
Saludos.

Si agrega drivers tipo ULN2003 puede poner todos los LED que quiera sin preocuparse por la corriente de cada pin ni la total. :wink:

Saludos

Efectivamente.

gracias maestrísimo, me funcionó impecable, gracias!

hola maestro, le sería mucha molestia explicarme gráficamente como debería hacerlo?, me gustó la idea, y desde ya muchas gracias!

Busca Arduino ULN2003 LED y ahi encontrarás muchos esquemas.
Esto funciona para autoresponderse muchas preguntas.

Como te dice @Surbyte, todo (o casi) se soluciona con una simple búsqueda en Google.
20 LED Knight Rider-2 Using arduino mega

Saludos

Interesante proyecto, si señor, pero eso no es ni el Knigh Rider, ni el coche fantastico.Es un Cylon y de los antiguos bah.. dos diodos meneandose de un lado a otro sin desvaneciminientos ni nada.
Knigh Rider (antiguo Coche fantastico, en España 1982) LINK
Cilon moderno: LINK
Knigh Rider moderno: LINK
y el que van a sacar para el 2025 LINK
Saludos

Se lo puse para que vea como conectar los ULN, lo del efecto no me interesa particularmente así que ni lo miré.

Saludos

mira, en la noche subiré un video de como quedó, que, gracias a tu modificación pude hacer funcionar en el mega, igual me gustaría hacer el cylon, pero, como el efecto del auto fantastico 2008 (así se llamó para latinoamerica) es decir, que se vayan encendiendo todos los led y apagando en desvanecimiento, eso, aún no he encontrado nada con PWM, que quede medianamente similar, gracias maestro

XD, me lo imaginé, joajoajoajoa, gracias de todas maneras