Arduino uint8_t sensor value char array storage

Asked 2 years ago, Updated 2 years ago, 69 views

void scan_rows(uint8_t *rows){

  char A[65];
  int i;

  uint8_t z,u,b;

  Serial.print("");

  for(z=0; z<8; z++){
    if(z)
    {
      pinMode(z+29,INPUT);
    }
    pinMode(z+30,OUTPUT);

    digitalWrite(z+30,LOW);
    for(u=22; u<=29; u++){
      rows[z]<<=1;
      b=digitalRead(u);
      rows[z]|=1;

      if(z==0){
        for(int i=0;i<65;i++){
        A[i]=b;
        }
      }

      if(z==1){
        for(int i=8;i<65;i++){
        A[i]=b;
        }
      }

      if(z==2){
        for(int i=16;i<65;i++){
        A[i]=b;
        }
      }

      if(z==3){
        for(int i=24;i<65;i++){
        A[i]=b;
        }
      }

      if(z==4){
        for(int i=32;i<65;i++){
        A[i]=b;
        }
      }

      if(z==5){
        for(int i=40;i<65;i++){
        A[i]=b;
        }
      }

      if(z==6){
        for(int i=48;i<65;i++){
        A[i]=b;
        }
      }

      if(z==7){
        for(int i=56;i<65;i++){
        A[i]=b;
        }
      }
    }
  }
  Serial.print(A[i]);
}

Use the Arduino Mega 2560. A function that scans sensor values arranged in an 8x8 matrix.

I'm going to use the char array to fill it up from 1 to 64. Number 0 is intentionally left blank. I gave up because I heard that I can't do the string.

But if I save it like that, it doesn't print properly and 가 appears... I want it printed as Serial.print. I will change it to Serial1.print and send it to Android application through Bluetooth module.

Please give me some advice.

arduino char array

2022-09-21 19:18

1 Answers

Overall, there seems to be a problem with the code.

First of all, it is difficult to determine what variables z, u, and b are, so why don't you write down the variable name more intuitively?

And if you want to store the value of the 8x8 matrix, why don't you declare A[8][8] and superimpose the for statement?

I don't know where the rows[z]|=1 variable was declared, but doesn't the result change all bits to 1?

Variable b is initialized every time the value of z increases by 1, so it has the same value as 8 in array A.

Lastly, I think the problem you mentioned is caused by the fact that the value of b is not converted into ASCII values in the array A.

b=digitalRead(u); Here, depending on the range of the return value of digitalRead(), the value may be incorrectly stored in b, but I think you may have considered that part.

If the range of b is within 3 digits

tmp[0] = b/100;
tmp[1] = (b%100)/10;
tmp[2] = b%10;
Serial.print(tmp);

I think we can print the value like this.


2022-09-21 19:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.