lanciare loop in automatico

Buongiorno a tutti,
sono nuovo, ho iniziato da poco con Arduino e avrei bisogno di un aiuto.

Ho scaricato un codice da internet che serve per far partire una sequenza di led assieme a delle immagini su un diplay oled.
In teoria dovrei avere un telecomando IR per far partire la sequenza, ma io vorrei che la sequenza partisse in automatico appena accendo la scheda e una volta finito ricominciasse.

Caricando il codice non succedeva nulla, allora ho eliminato tutti i riferimenti ai pulsanti software. Così facendo i led iniziano a lampeggiare e appaiono alcune immagini sul display, ma non parte la sequenza completa.

Sapete aiutarmi?
Allego il codice qui sotto e lascio anche il link al video di come dovrebbe essere il risultato.

allego il codice in un altro messaggio

Grazie!

Buongiorno,
essendo il tuo primo post, nel rispetto del regolamento della sezione Italiana del forum (… punto 13, primo capoverso), ti chiedo cortesemente di presentarti IN QUESTO THREAD (spiegando bene quali conoscenze hai di elettronica e di programmazione ... possibilmente evitando di scrivere solo una riga di saluto) e di leggere con molta attenzione tutto il su citato REGOLAMENTO ... Grazie. :slight_smile:

Guglielmo

P.S.: Ti ricordo che, purtroppo, fino a quando non sarà fatta la presentazione nell’apposito thread, nessuno ti potrà rispondere, quindi ti consiglio di farla al più presto. :wink:

/*
  Build Back To The Future DELOREAN (DeAGOSTINI/Eaglemoss) 
  Flux Capacitor Unit with OLED V1.091 By LsKaz

  Ver1.91 2019/01/25 when FluxUnit = 1 empty blink speed bug fix
*/

int FluxUnit = 0; // Fred Wendland Flux Capacitor board use change 0 to 1
#define MCP4725_ADDR 0x60   

#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI 
#include <Wire.h>

int j=2;
int old_j=9;
int jj=0;
int bar1[10]={};
int bar2[10]={};
int MatrixCntmx = 2000;
int MatrixCnt = MatrixCntmx;
int LedSw  = 1;
int LedCnt = 0;
int ON_PIN = 11;
int TR_PIN = 12;
int SW_KEY[3]={0,0,0};
int SW_ON[4]={0,0,0,0};
int SW_OFF[3]={0,0,0};
int SW_CNT[4]={0,0,0,0};
int SW_PIN[3]={14,15,16}; // A0:LEDオン A1:アップ A2:ダウン 
int LCD_X[11]={};
int LCD_Lv=0;
int LCD_Cnt=0;
int LCD_Sw=0;
int OLED_Cnt = 0;
int EMPTY_Cnt = 0;
int EMPTY_CntOff = 30;
int EMPTY_CntMAX = 60;
int BR=0;

//                     0    1    2    3    4    5    6    7    8  
int LedCntmx[9] =  {  20,  15,   8,   5,   3,   2,   1,   1,   0};
int LedBright[9] = {   0,   5,  10,  10,  20,  50, 100, 230, 255};
int LedBright2[9] = {   0,  960,  1166,  1372, 1578, 1784, 1990, 2100, 3000};
int LedBright3[9] = {   0,   5,  20,  40,  60,  90, 120, 255, 255};
int LcdDef[9]    = {   0,   2,   4,   6,   8,  12,  15,  20,  20};

void setup() {
  Serial.begin(9600);
  Serial.println("OLED Start");
  
  randomSeed(analogRead(6));      // 乱数リセット
 
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(SW_PIN[0],INPUT);
  pinMode(SW_PIN[1],INPUT);
  pinMode(SW_PIN[2],INPUT);
  
}

void loop() {
int Lv;
int LvMax;

  LCD_Lv = LcdDef[j];
  Lv=LCD_Lv;
  LvMax=10;
  OLED_Cnt++;
  
  if (OLED_Cnt > 3){
    OLED_Cnt=0;
    for (int x=1 ; x < 11; x++ ) {
      switch(j){
       case 0: LCD_X[x] = 0;break;
       case 1: LCD_X[x] = random(0,2);break;
       case 2: LCD_X[x] = random(1,4);break;
       case 3: LCD_X[x] = random(4,10);break;
       case 8: LCD_X[x] = random(33,45);break;
       default: LCD_X[x] = random(Lv,Lv+5);break;
      }
      if (x == 2 ) {LCD_X[x] = LCD_X[x] / 1.8 ;}
      if (x == 3 ) {LCD_X[x] = LCD_X[x] / 2.3 ;}
      if (x == 4 ) {LCD_X[x] = LCD_X[x] / 1.6 ;}
      if (x == 5 ) {LCD_X[x] = LCD_X[x] / 2.1 ;}
      if (x == 6 ) {LCD_X[x] = LCD_X[x] / 1.6 ;}
      if (x == 7 ) {LCD_X[x] = LCD_X[x] / 1.2 ;}
      if (x == 8 ) {LCD_X[x] = LCD_X[x] / 3.0 ;}
      if (x == 10) {LCD_X[x] = LCD_X[x] / 2.0 ;}

      if (SW_ON[3]==1){LCD_X[x] = 0;}
      if (LCD_X[x]<0){LCD_X[x]=0;}
      if (LCD_X[x]>20){LCD_X[x]=20;}
      if (LCD_X[x]!=20){LvMax=1;}
    }
  }
  // picture loop  
  u8g.firstPage();  
  do {
    loop2();
    draw();
  } while( u8g.nextPage() );
  
  if (LCD_Lv < 0) {LCD_Lv=0;}
  if (LCD_Lv > 20) {LCD_Lv=20;}
//Serial.print("Lv=");
//Serial.println(j);
}

void loop2() {

  //----- main arduino control --------------------------
  SW_KEY[0]=digitalRead(SW_PIN[0]);
//  Serial.print("SW_KEY[0]=");
//  Serial.println(SW_KEY[0]);
  SW_KEY[1]=digitalRead(SW_PIN[1]);
//  Serial.print("SW_KEY[1]=");
//  Serial.println(SW_KEY[1]);
  SW_KEY[2]=digitalRead(SW_PIN[2]);
//  Serial.print("SW_KEY[2]=");
//  Serial.println(SW_KEY[2]);
  
  if (SW_KEY[0] == HIGH) {
    if (SW_CNT[0]++ > 10) {SW_CNT[0]=10;} 
  } else {SW_CNT[0]=0;SW_CNT[1]=0;SW_CNT[2]=0;j=0;}
  
  if (SW_KEY[1] == HIGH && SW_KEY[0] == HIGH) {
    if (SW_CNT[1]++ > 10) {SW_CNT[1]=10;
                           if (SW_ON[1]==0){SW_ON[1]=1;
                                            j++;
                                            Serial.println(j);} 
    } 
  } else {SW_CNT[1]=0;SW_ON[1]=0;}
  
  if (SW_KEY[2] == HIGH && SW_KEY[0] == HIGH) {
    if (SW_CNT[2]++ > 10) {SW_CNT[2]=10;
                           if (SW_ON[2]==0){SW_ON[2]=1;
                                            j--;
                                            Serial.println(j);} 
    } 
  } else {SW_CNT[2]=0;SW_ON[2]=0;}

  if (j < 0){j=0;}
  if (j > 8){j=8;}
  //---------EMPTY BRINK LED -------------------------------  
  if (SW_KEY[0] == LOW && SW_KEY[1] == HIGH && SW_KEY[2] == HIGH) {
    if (SW_CNT[3]++ > 10) {
      SW_CNT[3]=30;j=1;SW_ON[3]=1;
      EMPTY_Cnt++;
      if (EMPTY_Cnt > EMPTY_CntMAX) {EMPTY_Cnt=0;}
      if (EMPTY_Cnt < EMPTY_CntOff) {digitalWrite(8, HIGH);} else {digitalWrite(8, LOW);}
    }
  } else {EMPTY_Cnt=0;SW_CNT[3]=0;SW_ON[3]=0;digitalWrite(8, LOW);}
  //----- main arduino control end------------------------

  MatrixCnt++;
  if (MatrixCnt > MatrixCntmx) {
//                                matrixloop();
                                MatrixCnt = 0;
  }  
  LedCnt++;
  if (LedCnt > (LedCntmx[j])) 
    {LedCnt=0;LedSw++;
    if (LedSw > 4) 
       {LedSw = 1;
        if (j > 4 & random(0,j*2)>8)
           {BR=BR+random((j-5)*40,(j-4)*40);
        }
    }
  }
  if (BR > 255){BR=255;}  
  if (SW_KEY[0] == LOW){analogWrite(10, BR);}  
  if (j > 4 & j < 8)  {analogWrite(10, BR);} 
  if (LedSw == 4 & j == 8) {analogWrite(10, random(240,255));BR=255;}
  if (LedSw == 4 & j == 8 & random(0,3)==1) {analogWrite(10, 150);}
  if (FluxUnit == 0) {
     if (LedSw == 4) {analogWrite(3, LedBright[j]);} else {digitalWrite(3,LOW);}
     if (LedSw == 3) {analogWrite(5, LedBright[j]);} else {digitalWrite(5,LOW);}
     if (LedSw == 2) {analogWrite(6, LedBright[j]);} else {digitalWrite(6,LOW);}
     if (LedSw == 1) {analogWrite(9, LedBright[j]);} else {digitalWrite(9,LOW);}
  } else {
      if (j != old_j) {
         old_j=j;
         switch(j){
           case 0:  digitalWrite(5, LOW);Flux_draw();break;
           default: analogWrite(5, LedBright3[j]);Flux_draw();break;
         }
         
      } else {
        /*  DUMMY logic START */
         if (LedSw == 4) {analogWrite(3, 0);} else {digitalWrite(3,LOW);}
         if (LedSw == 3) {analogWrite(3, 0);} else {digitalWrite(3,LOW);}
         if (LedSw == 2) {analogWrite(3, 0);} else {digitalWrite(3,LOW);}
         if (LedSw == 1) {analogWrite(3, 0);} else {digitalWrite(3,LOW);}
        /*  DUMMY logic END */
      }
  }
  if (j > 6) {digitalWrite(2, HIGH);} else {digitalWrite(2,LOW);}
  if (j == 0) {digitalWrite(4, HIGH);digitalWrite(7, LOW);} else {digitalWrite(4,LOW);digitalWrite(7, HIGH);}
  BR=BR-5;if (BR < 0){BR=0;}  
//  Serial.println(BR);
}
//================OLED SECTION=====================================
void u8g_prepare(void) {
  u8g.setFont(u8g_font_6x10);
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setContrast(255);
  u8g.setFontPosTop();
}

void u8g_box_frame(uint8_t a) {

//  u8g.drawFrame(0,0,128,64);
  for (int y=1 ; y < 11; y++ )
  {
    for (int x=1 ; x <= LCD_X[y];x++)
    {
//      u8g.drawBox(0,6*y-6,6*LCD_X[y],5);
      u8g.drawBox(4+6*x-6,2+6*y-6,5,5);
  
    }
  }
}

void u8g_disc_circle(uint8_t a) {
  u8g.drawStr( 0, 0, "drawDisc");
  u8g.drawDisc(10,18,9);
  u8g.drawDisc(24+a,16,7);
  u8g.drawStr( 0, 30, "drawCircle");
  u8g.drawCircle(10,18+30,9);
  u8g.drawCircle(24+a,16+30,7);
}

void u8g_r_frame(uint8_t a) {
  u8g.drawStr( 0, 0, "drawRFrame/Box");
  u8g.drawRFrame(5, 10,40,30, a+1);
  u8g.drawRBox(50, 10,25,40, a+1);
}

void u8g_string(uint8_t a) {
  u8g.drawStr(30+a,31, " 0");
  u8g.drawStr90(30,31+a, " 90");
  u8g.drawStr180(30-a,31, " 180");
  u8g.drawStr270(30,31-a, " 270");
}

void u8g_line(uint8_t a) {
  u8g.drawStr( 0, 0, "drawLine");
  u8g.drawLine(7+a, 10, 40, 55);
  u8g.drawLine(7+a*2, 10, 60, 55);
  u8g.drawLine(7+a*3, 10, 80, 55);
  u8g.drawLine(7+a*4, 10, 100, 55);
}

void u8g_triangle(uint8_t a) {
  uint16_t offset = a;
  u8g.drawStr( 0, 0, "drawTriangle");
  u8g.drawTriangle(14,7, 45,30, 10,40);
  u8g.drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset);
  u8g.drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53);
  u8g.drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset);
}

void u8g_ascii_1() {
  char s[2] = " ";
  uint8_t x, y;
  u8g.drawStr( 0, 0, "ASCII page 1");
  for( y = 0; y < 6; y++ ) {
    for( x = 0; x < 16; x++ ) {
      s[0] = y*16 + x + 32;
      u8g.drawStr(x*7, y*10+10, s);
    }
  }
}

void u8g_ascii_2() {
  char s[2] = " ";
  uint8_t x, y;
  u8g.drawStr( 0, 0, "ASCII page 2");
  for( y = 0; y < 6; y++ ) {
    for( x = 0; x < 16; x++ ) {
      s[0] = y*16 + x + 160;
      u8g.drawStr(x*7, y*10+10, s);
    }
  }
}

void u8g_extra_page(uint8_t a)
{
  if ( u8g.getMode() == U8G_MODE_HICOLOR || u8g.getMode() == U8G_MODE_R3G3B2) {
    /* draw background (area is 128x128) */
    u8g_uint_t r, g, b;
    b = a << 5;
    for( g = 0; g < 64; g++ )
    {
      for( r = 0; r < 64; r++ )
      {
  u8g.setRGB(r<<2, g<<2, b );
  u8g.drawPixel(g, r);
      }
    }
    u8g.setRGB(255,255,255);
    u8g.drawStr( 66, 0, "Color Page");
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
  {
    u8g.drawStr( 66, 0, "Gray Level");
    u8g.setColorIndex(1);
    u8g.drawBox(0, 4, 64, 32);    
    u8g.drawBox(70, 20, 4, 12);
    u8g.setColorIndex(2);
    u8g.drawBox(0+1*a, 4+1*a, 64-2*a, 32-2*a);
    u8g.drawBox(74, 20, 4, 12);
    u8g.setColorIndex(3);
    u8g.drawBox(0+2*a, 4+2*a, 64-4*a, 32-4*a);
    u8g.drawBox(78, 20, 4, 12);
  }
  else
  {
    u8g.drawStr( 0, 12, "setScale2x2");
    u8g.setScale2x2();
    u8g.drawStr( 0, 6+a, "setScale2x2");
    u8g.undoScale();
  }
}


uint8_t draw_state = 0;

void draw(void) {
  u8g_prepare();
  u8g_box_frame(draw_state&7);
}

void Flux_draw(void) {
  Wire.beginTransmission(MCP4725_ADDR);
  Wire.write(64);                     // cmd to update the DAC
  Wire.write(LedBright2[j] >> 4);        // the 8 most significant bits...
  Wire.write((LedBright2[j] & 15) << 4); // the 4 least significant bits...
  Wire.endTransmission();
}

Mi sono presentato nell'apposita sezione e ho letto tutti il regolamento.

ho dovuto ripostare il codice perchè mi sono accorto che quello che avevo copiato prima non era completo.
L'ho diviso in 2 parti altrimenti era troppo lungo.

Grazie