vendredi 5 juin 2020

Arduino LED Cube 4x4x4 Any Ideas how to Fix

// Connections
int mreset = 10;
int shift = 11;
int store = 12;
int data = 13;
int taster = 9;
int a, tasterstatus = 0;

// Delays
int tasterdelay = 250;


int fast = 5;
int stage1 = 1000;
int stage2 = 1000;


unsigned long time_now = 0;

// Pattern
int firstlevel[24]  = {1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int secondlevel[24] = {0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int thirdlevel[24]  = {0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int fourthlevel[24] = {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};

int leftside[24]    = {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int ndleftside[24]  = {1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int ndrightside[24] = {1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
int rightside[24]   = {1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0};

int backside[24]    = {1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
int ndbackside[24]  = {1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0};
int ndfrontside[24] = {1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0};
int frontside[24]   = {1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0};

int cube1[24]       = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int cube2_1[24]     = {1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int cube2_2[24]     = {0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int bigcube1_1[24]  = {1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int bigcube1_2[24]  = {0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0};

int cube3_1[24]     = {0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0};
int cube3_2[24]     = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0};
int cube4[24]       = {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0};

int cube5_1[24]     = {1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
int cube5_2[24]     = {0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
int cube6[24]       = {1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int cube7_1[24]     = {0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0};
int cube7_2[24]     = {0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0};
int cube8[24]       = {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0};

int custom1[24]     = {1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0};
int custom2[24]     = {0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0};

void setup() {

//Set Outputs
  pinMode(shift , OUTPUT);
  pinMode(store , OUTPUT);
  pinMode(data , OUTPUT);
  pinMode(mreset , OUTPUT);
  pinMode(taster , INPUT);

// Set Shift Register Master Reset On/Off
  digitalWrite(mreset, LOW);
  digitalWrite(store, HIGH);
  digitalWrite(mreset, HIGH);
  digitalWrite(store, LOW);
}
void loop() {
//Waiting for Button PRess
  tasterstatus = digitalRead(taster);
  if (tasterstatus == HIGH) {
    a++;
    delay(tasterdelay);
  }
  //Choose Pattern
  if (a == 1) {
    pattern1();
  }
  if (a == 2) {
    pattern2();
  }
}

void pattern1() {
//Equate Millis with time_now
  time_now = millis();
  // First Sample for LED Cube
  for (int i = 0; i < 24; i++) {
    digitalWrite(shift, LOW);
    digitalWrite(data, firstlevel[i]);
    digitalWrite(shift, HIGH);

  }
  digitalWrite(store, HIGH);
  digitalWrite(store, LOW);
// "Delay"
  while (millis() < time_now + stage1) {
    tasterstatus = digitalRead(taster);
    // Checking Button State
    if (tasterstatus == HIGH) {
      delay(tasterdelay);
      a++;
      pattern2();
    }
  }
  // Clearing Shift Registers with Master Reset
  digitalWrite(mreset, LOW);
  digitalWrite(store, HIGH);
  digitalWrite(mreset, HIGH);
  digitalWrite(store, LOW);

  time_now = millis();
  for (int i = 0; i < 24; i++) {
    digitalWrite(shift, LOW);
    digitalWrite(data, secondlevel[i]);
    digitalWrite(shift, HIGH);

  }
  digitalWrite(store, HIGH);
  digitalWrite(store, LOW);

  while (millis() < time_now + stage1) {
    tasterstatus = digitalRead(taster);
    if (tasterstatus == HIGH) {
      delay(tasterdelay);
      a++;
      pattern2();
    }
  }
  digitalWrite(mreset, LOW);
  digitalWrite(store, HIGH);
  digitalWrite(mreset, HIGH);
  digitalWrite(store, LOW);

  time_now = millis();
  for (int i = 0; i < 24; i++) {
    digitalWrite(shift, LOW);
    digitalWrite(data, thirdlevel[i]);
    digitalWrite(shift, HIGH);

  }
  digitalWrite(store, HIGH);
  digitalWrite(store, LOW);

  while (millis() < time_now + stage1) {
    tasterstatus = digitalRead(taster);
    if (tasterstatus == HIGH) {
      delay(tasterdelay);
      a++;
      pattern2();
    }
  }
  digitalWrite(mreset, LOW);
  digitalWrite(store, HIGH);
  digitalWrite(mreset, HIGH);
  digitalWrite(store, LOW);
  time_now = millis();
  for (int i = 0; i < 24; i++) {
    digitalWrite(shift, LOW);
    digitalWrite(data, fourthlevel[i]);
    digitalWrite(shift, HIGH);

  }
  digitalWrite(store, HIGH);
  digitalWrite(store, LOW);

  while (millis() < time_now + stage1) {
    tasterstatus = digitalRead(taster);
    if (tasterstatus == HIGH) {
      delay(tasterdelay);
      a++;
      pattern2();
    }
  }
  digitalWrite(mreset, LOW);
  digitalWrite(store, HIGH);
  digitalWrite(mreset, HIGH);
  digitalWrite(store, LOW);

}
void pattern2() {
  time_now = millis();
  for (int i = 0; i < 24; i++) {
    digitalWrite(shift, LOW);
    digitalWrite(data, cube1[i]);
    digitalWrite(shift, HIGH);

  }
  digitalWrite(store, HIGH);
  digitalWrite(store, LOW);
}`enter code here

My Problem Is that when i press the button in Pattern1, Pattern2 Starts 1x time, after that Pattern1 continues where i pressed the Button, runs 1x trough, and after that pattern 2 repeats normaly. Any ideas how to fix it ? Like when i press the Button First Pattern1 repeats infinity as it should. But when i press it again, while Pattern1 is running, its goes to Pattern2 as it should, but it only repeats Pattern2 1x time, than it resumes pattern1, where i pressed the button, and then it repeats pattern2 as it should.

So the Main Problem is that Pattern1 Runs 1x time in pattern2;

Thanks for help

Aucun commentaire:

Enregistrer un commentaire