I need help with my project code using arduino zero!

Hi, I need to complete a project by creating a programming code to let the led matrix display the required number from left to right when push button pressed from the first time and displaying another string character when the same push button pressed for second time. I can only figure out on how to make it scroll from right to left and using two different push button to change between (animation1) and (animation2). Could I get help with the same push button pressed for second time and the text scrolling from left to right?

int c[8] = {14,15,16,17,18,19,20,21};
int r[8] = {6,7,8,9,10,11,12,13};
const int but1 = 2;
const int but2 = 3;
int buttoncount = 0;
int but1State = 0;
int but2State = 0;
int but1Now;
int but2Now;

int value1;
int value2;

int matrix1[8][8] = {};
int matrix2[8][8] = {};

//Array variable for output
int position = 0;
long t = 0;
#define scrolltime 90
int all_num[8] [54] = {
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,0,1,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,1,0,0,1,1,1,1,0,0,0,1,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0},
  {0,1,0,0,1,0,0,1,1,0,0,0,1,0,0,1,0,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,1,0,0,1,0,0},
  {0,1,0,0,1,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0,0,1,0,0},
  {0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,1,1,1,1,0,0,0,0,1,0,1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,1,1,1,0,0},
  {0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
  {0,0,1,1,0,0,1,1,1,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};

void setup() {
  //Pin initialization
  for (int i = 0; i<8; i++){
    pinMode(r[i], OUTPUT);
    pinMode(c[i], OUTPUT);
    digitalWrite(c[i], LOW); 
    digitalWrite(r[i], HIGH); 
    }

    pinMode(but1, INPUT_PULLUP);
    pinMode(but2, INPUT_PULLUP);

    but1State = digitalRead(but1);
    but2State = digitalRead(but2);

  for (int i=0; i<8 ; ++i)  {
    for (int j=0; j<8 ; ++j) {
      matrix1[i][j] = 0;
    }
  }
  for (int i=0; i<8 ; ++i)  {
    for (int j=0; j<8 ; ++j) {
      matrix2[i][j] = 0;
    }
  }
  //Store initial elapsed time
  t = millis();
}
void loop() {

    int but1Now = digitalRead(but1);
    delay(5);
    int but1Now1 = digitalRead(but1);

    if(but1Now == but1Now1) 
    {
        if(but1Now != but1State)
        {
            value1 = 1;
            value2 = 0;
            //show(img1);
        }

        but1State = but1Now;
    }

    int but2Now = digitalRead(but2);
    delay(5);
    int but2Now1 = digitalRead(but2);

    if(but2Now == but2Now1) 
    {
        if(but2Now != but2State)
        {
            value1 = 0;
            value2 = 1;
            //show(img2);
        }

        but2State = but2Now;
    }

    if(value1 == 1)
    {
      animation1();
    }

    if(value2 == 1)
    {
      animation2();  
    }
}

void animation1() {
  //Store the character data in the output array for each elapsed time
  if (t+scrolltime <= millis()) {
    for (int i=0; i<8 ; i++)  {
      for (int j=0; j<8 ; j++) {
        matrix1[i][j] = all_num[i][j+position];
      }
    }
    
    //Slide the storage location and return to the beginning when you reach the end
    if (position<52) {
      ++position; }
    else {
      position=0; }
    //Remeasure the elapsed time
    t=millis();
  }
  // Character data output processing
  for (int i=0; i<8; i++) {
    digitalWrite(r[i], LOW);
    for (int j=0; j<8; j++) {
      digitalWrite(c[j], matrix1[j][i]); }
      for (int j=0; j<8; j++) {
        digitalWrite(c[j], LOW); }
  digitalWrite(r[i], HIGH); } }
void animation2() {
  //Store the character data in the output array for each elapsed time
  if (t+scrolltime <= millis()) {
    for (int i=0; i<8 ; i++)  {
      for (int j=0; j<8 ; j++) {
        matrix2[i][j] = Eng20009[i][j+position];
      }
    }
    
    //Slide the storage location and return to the beginning when you reach the end
    if (position<52) {
      ++position; }
    else {
      position=0; }
    //Remeasure the elapsed time
    t=millis();
  }
  // Character data output processing
  for (int i=0; i<8; i++) {
    digitalWrite(r[i], LOW);
    for (int j=0; j<8; j++) {
      digitalWrite(c[j], matrix2[j][i]); }
      for (int j=0; j<8; j++) {
        digitalWrite(c[j], LOW); }
  digitalWrite(r[i], HIGH); } }

is that a school project?
can you use a button library?

have a global variable remembering the scrolling direction. When you detect a button press, inverse the direction.

I could only figure out on how to use two different button for each animation. But it needs to be using the same button to control each function first and second time pressed respectively. Can you help me with that?

start by small building blocks:

  • build a circuit with just one button
  • write a small code that just prints "Button Pressed" each time you detect a press

post the code and a schema of your circuit here.

What am I suppose to do next?

int buttonPin = 2;
int oldbuttonstate = LOW;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonstate = digitalRead(buttonPin);
  if (buttonstate!=oldbuttonstate && buttonstate==HIGH) {
    Serial.println("button pressed");
    delay(500);
  }
  else {
    Serial.println("button is not pressed");
    delay(500);
  }
  oldbuttonstate = buttonstate;
}


image
Here is the circuit schema!

Add a global variable

byte animationNumber = 0;
const byte animationCount = 2; // we have two animations (0 and 1)

Then when you detect a press do

animationNumber++; 
if (animationNumber >= animationCount) animationNumber = 0;

Modify the print then to show the value of animationNumber

Think how you can use that then

I have tried to implement the method, but it is only displaying the animation1 even the button is not pressed. and the second pressed isn't working. Here is the code I write:

void loop() {
  int buttonstate = digitalRead(buttonPin);
  if (buttonstate!=oldbuttonstate && buttonstate==HIGH){
    animationNumber++;
    if (animationNumber >= animationCount) {
      animationNumber = 0; } }

  if (buttonstate==HIGH && animationNumber==1) {
    animation1();
    delay(3);
  }
  else if (buttonstate==HIGH && animationNumber==2) {
    animation2();
    delay(3);
  }
  else {
    for (int i = 0; i<8; i++) {
    digitalWrite(c[i], LOW); 
    digitalWrite(r[i], HIGH); }
  }
}

Can you help me fix it?

you are not too far off

you don't deal with button bouncing so this might be an issue, but assuming you don't have a bouncing problem, may be something like this:

you check if the button is pressed. You trigger the animation and go the the next animation number to be ready for the next press

void loop() {
  int buttonstate = digitalRead(buttonPin);

  if (buttonstate != oldbuttonstate && buttonstate == HIGH) {
    // button is pressed, trigger animation
    switch (animationNumber) {
      case 0: animation1(); break;
      case 1: animation2(); break;
      default: break;
    }
    // get ready for next animation
    animationNumber++;
    if (animationNumber >= animationCount) animationNumber = 0;
  }

  oldbuttonstate = buttonstate;
}

It is still not working. It showed quickly and disappear as I click the button each time.

implement a correct way to handle bouncing

also what do you do in the animation? is it blocking ? is it long ?

I try to use debounce and now it works, thank for your help, I appreciate.

Great have fun