[SOLVED]Need to find an issue

Hello. I am new to coding and this forum. I do it for around two months at the workshop. I am a trainee. My trainer gave me a code and said that I should find the mistake. The created symbol should go right. In the loop() function I can change "arrayRotateRight();" to either left or right. When it says "arrayRotateLeft();" the symbol moves to the left, but when I change it to "arrayRotateRight();" it doesn't move and stays at the top-left corner of the screen. In "void arrayRotateRight()" should be the error, but I can't imagine what it should look like to fix my problem. Any sort of help would be appreciated and if possible could you add comments in the code so I can understand it? Thanks.

Btw. A normal 16x2 LCD is used

#include <LiquidCrystal.h>

byte gewicht[8]{             //Gewicht
  B00000,
  B11111,
  B01110,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};

LiquidCrystal lcd(12,11,5,4,3,2);

char puffer[16][2];
const int Button = 8; //Anschluss Prototyp
const int LED = 13; //Anschluss Prototyp
const int Speaker =9; //Anschluss Prototyp



void setup() {
  pinMode(LED,OUTPUT);
  pinMode(Button,INPUT_PULLUP);
  lcd.begin(16,2);
  lcd.createChar(0, gewicht); //Gewicht
  arrayInit();
  puffer[0][0]= 0;
}

void loop() {
  arrayToDisplay();
  arrayRotateRight();
  delay(500);
}

/***************************************************************************
 * Hier kommen die Unterprogramme
 * 
 **************************************************************************/

void arrayInit(){
  for(byte i=0; i<16; i++){
    for (byte j=0; j<2; j++){
      puffer[i][j] = ' ';
    }
  }
}

void arrayRotateLeft(){
  char temp1, temp2;
  temp1 = puffer[0][0];
  temp2 = puffer[0][1];
  for (byte i=1; i<16; i++){
    puffer[i-1][0] = puffer[i][0];
    puffer[i-1][1] = puffer[i][1];
  }
  puffer[15][0] = temp1;
  puffer[15][1] = temp2;
}

void arrayRotateRight(){
  char temp1, temp2;
  temp1 = puffer[15][0];
  temp2 = puffer[15][1];
  for (byte i=14; i>=0; i--){
    puffer[i][0] = puffer[i+1][0];
    puffer[i][1] = puffer[i+1][1];
  }
  puffer[0][0] = temp1;
  puffer[0][1] = temp2;
}

void arrayToDisplay(){
  lcd.setCursor (0,0);
  for (int i=0; i<16; i++){
    lcd.write (puffer[i][0]);
  }
  lcd.setCursor (0,1);
  for (int i=0; i<16; i++){
    lcd.write (puffer[i][1]);
  }
}

sample.ino (1.62 KB)

As this is a programming problem I have suggested to the Moderator to move it to the Programming section.

It is much easier for people to help when you include short programs in your Post so they don't have to download them. Like this

#include <LiquidCrystal.h>

byte gewicht[8]{             //Gewicht
  B00000,
  B11111,
  B01110,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};

LiquidCrystal lcd(12,11,5,4,3,2);

char puffer[16][2];
const int Taster = 8; //Anschluss Prototyp
const int LED = 13; //Anschluss Prototyp
const int Speaker =9; //Anschluss Prototyp



void setup() {
  pinMode(LED,OUTPUT);
  pinMode(Taster,INPUT_PULLUP);
  lcd.begin(16,2);
  lcd.createChar(0, gewicht); //Gewicht
  arrayInit();
  puffer[0][0]= 0;
}

void loop() {
  arrayToDisplay();
  arrayRotateRight();
  delay(500);
}

/***************************************************************************
 * Hier kommen die Unterprogramme
 * 
 **************************************************************************/

void arrayInit(){
  for(byte i=0; i<16; i++){
    for (byte j=0; j<2; j++){
      puffer[i][j] = ' ';
    }
  }
}

void arrayRotateLeft(){
  char temp1, temp2;
  temp1 = puffer[0][0];
  temp2 = puffer[0][1];
  for (byte i=1; i<16; i++){
    puffer[i-1][0] = puffer[i][0];
    puffer[i-1][1] = puffer[i][1];
  }
  puffer[15][0] = temp1;
  puffer[15][1] = temp2;
}

void arrayRotateRight(){
  char temp1, temp2;
  temp1 = puffer[15][0];
  temp2 = puffer[15][1];
  for (byte i=14; i>0; i++){
    puffer[i][0] = puffer[i+1][0];
    puffer[i][1] = puffer[i+1][1];
  }
  puffer[0][0] = temp1;
  puffer[0][1] = temp2;
}

void arrayToDisplay(){
  lcd.setCursor (0,0);
  for (int i=0; i<16; i++){
    lcd.write (puffer[i][0]);
  }
  lcd.setCursor (0,1);
  for (int i=0; i<16; i++){
    lcd.write (puffer[i][1]);
  }
}

See How to use the forum

...R

Here is your problem (or one of them):

  for (byte i=14; i>0; i++){

Change i++ to i--

You have narrowed the problem down to the arrayRotateRight() function, try the for loop by hand with a few values of i and see what it is doing.

void arrayRotateRight(){
  char temp1, temp2;
  temp1 = puffer[15][0];
  temp2 = puffer[15][1];
  for (byte i=14; i>0; i++){
    puffer[i][0] = puffer[i+1][0];
    puffer[i][1] = puffer[i+1][1];
  }
  puffer[0][0] = temp1;
  puffer[0][1] = temp2;
}

Robin2:
As this is a programming problem I have suggested to the Moderator to move it to the Programming section.

It is much easier for people to help when you include short programs in your Post so they don't have to download them.

Okay Thanks. I have changed it now. I also try to follow the steps from the link you send. Again, new here.

ToddL1962:
Here is your problem (or one of them):

  for (byte i=14; i>0; i++){

Change i++ to i--

I tried but ut didn't work. I also added "=" between ">" and "0" but it still doesn't move to the right, stays in the top-left corner. I am lost and there can't be anything wrong with how it is build because in the "left" function it works.

You need to post your code

TheMemberFormerlyKnownAsAWOL:
You need to post your code

You can't see it? I thought I have post it after Robin2 mentioned it.

But here you go:

#include <LiquidCrystal.h>

byte gewicht[8]{             //Gewicht
  B00000,
  B11111,
  B01110,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};

LiquidCrystal lcd(12,11,5,4,3,2);

char puffer[16][2];
const int Button = 8; //Anschluss Prototyp
const int LED = 13; //Anschluss Prototyp
const int Speaker =9; //Anschluss Prototyp



void setup() {
  pinMode(LED,OUTPUT);
  pinMode(Button,INPUT_PULLUP);
  lcd.begin(16,2);
  lcd.createChar(0, gewicht); //Gewicht
  arrayInit();
  puffer[0][0]= 0;
}

void loop() {
  arrayToDisplay();
  arrayRotateRight();
  delay(500);
}

/***************************************************************************
 * Hier kommen die Unterprogramme
 * 
 **************************************************************************/

void arrayInit(){
  for(byte i=0; i<16; i++){
    for (byte j=0; j<2; j++){
      puffer[i][j] = ' ';
    }
  }
}

void arrayRotateLeft(){
  char temp1, temp2;
  temp1 = puffer[0][0];
  temp2 = puffer[0][1];
  for (byte i=1; i<16; i++){
    puffer[i-1][0] = puffer[i][0];
    puffer[i-1][1] = puffer[i][1];
  }
  puffer[15][0] = temp1;
  puffer[15][1] = temp2;
}

void arrayRotateRight(){
  char temp1, temp2;
  temp1 = puffer[15][0];
  temp2 = puffer[15][1];
  for (byte i=14; i>=0; i--){
    puffer[i][0] = puffer[i+1][0];
    puffer[i][1] = puffer[i+1][1];
  }
  puffer[0][0] = temp1;
  puffer[0][1] = temp2;
}

void arrayToDisplay(){
  lcd.setCursor (0,0);
  for (int i=0; i<16; i++){
    lcd.write (puffer[i][0]);
  }
  lcd.setCursor (0,1);
  for (int i=0; i<16; i++){
    lcd.write (puffer[i][1]);
  }
}

codenewb:
You can't see it? I thought I have post it after Robin2 mentioned it.

The request in Reply #6 was to post the latest version of the program with the changes that you say (in Reply #5) that you tried but which did not solve the problem.

Is the code in Reply #7 the latest version?

...R

Robin2:
The request in Reply #6 was to post the latest version of the program with the changes that you say (in Reply #5) that you tried but which did not solve the problem.

Is the code in Reply #7 the latest version?

...R

Yep. The last code is the newest.

codenewb:
I tried but ut didn't work. I also added "=" between ">" and "0" but it still doesn't move to the right, stays in the top-left corner. I am lost and there can't be anything wrong with how it is build because in the "left" function it works.

Have you looked at the "left" function, to understand what it is doing and how it works? The if statement in the "right" function is incorrect, but that does not mean it is the only problem.

david_2018:
The if statement in the "right" function is incorrect, but that does not mean it is the only problem.

Are we looking at the same code?

Okay guys, I got the solution.

Befor:

void arrayRotateRight(){
  char temp1, temp2;
  temp1 = puffer[15][0];
  temp2 = puffer[15][1];
  for (byte i=14; i>=0; i--){
    puffer[i][0] = puffer[i+1][0];
    puffer[i][1] = puffer[i+1][1];
  }
  puffer[0][0] = temp1;
  puffer[0][1] = temp2;
}

After:

void arrayRotateRight(){
  char temp1, temp2;
  temp1 = puffer[15][0];
  temp2 = puffer[15][1];
  for (int i=14; i>=0; i--){
    puffer[i+1][0] = puffer[i][0];
    puffer[i+1][1] = puffer[i][1];
  }
  puffer[0][0] = temp1;
  puffer[0][1] = temp2;
}

I changed the datatype "byte" to "int" int the for function. I also changed

puffer[i][0] = puffer[i+1][0];
   puffer[i][1] = puffer[i+1][1];

to

puffer[i+1][0] = puffer[i][0];
   puffer[i+1][1] = puffer[i][1];

That was the error. Thank you for trying to help. How can I close this thread or mark it as solved or something like that?

codenewb:
That was the error. Thank you for trying to help. How can I close this thread or mark it as solved or something like that?

There is no requirement to do anything, but, if you wish, you can edit your Original Post and add the word [SOLVED] to the title.

...R

It can be done with a data type of byte, just have to avoid the test for >= 0 because byte is an unsigned integer, so can never be < 0.

for (byte i=15; i>0; i--){
    puffer[i][0] = puffer[i-1][0];
    puffer[i][1] = puffer[i-1][1];
  }