dimanche 1 mai 2022

How to use const byte in arduino?

Serial.println("start");
   Serial.println(dx+1);
    const byte vass = 201;  
    int x_arr_alt[vass];
    for(int i = 0; i < vass; i++)
    {
        x_arr_alt[i] = x_arr[i];
        
    } 
     Serial.println("end");

Hi all, refering to the code above only, there is such as strange bug that occurs in this arduino ide. Since dx+1 value is 201 from the Serial-print, we can equate vass=dx+1; However, I have encountered a strange bug because it does not print start followed by an end as I have stated with Serial.print() which means the code is not runned properly. May I know how to fix this weird bug?

Thank you and have a nice day.You can just plug in arduino to see the serial monitor:). The whole code is below this,

int x_a_ini=20;   int y_a_ini=10;
int x_b_ini=220; int y_b_ini=50;
int x_a_ini_alt=x_a_ini;   int y_a_ini_alt=y_a_ini;
int x_b_ini_alt=x_b_ini;   int y_b_ini_alt=y_b_ini;

int x=x_a_ini;   int y=y_a_ini;
int x_b=x_b_ini; int y_b=y_b_ini;
int get_all_coor(){
if ( (y_b_ini-y_a_ini)/(x_b_ini-x_a_ini)<0)  {
  y_a_ini=y_b_ini_alt;
  y_b_ini=y_a_ini_alt;
};

if (y_b_ini-y_a_ini > x_b_ini-x_a_ini){
x=y_a_ini;   y=x_a_ini;
x_b=y_b_ini; y_b=x_b_ini;
};

if ( (y_b_ini_alt-y_a_ini_alt)/(x_b_ini_alt-x_a_ini_alt)<0 && abs(y_b_ini_alt-y_a_ini_alt)<=abs(x_b_ini_alt-x_a_ini_alt) ){
  y_a_ini=y_b_ini_alt;
  y_b_ini=y_a_ini_alt;
  x=x_a_ini;   y=y_a_ini;
x_b=x_b_ini; y_b=y_b_ini;
};

int dx=x_b-x; int dy=y_b-y ; int diff_1= 2*dy-2*dx;  int twice_dy=2*dy;
int P=2*dy-dx;
int haha=30;
 int x_arr_alt[haha];
int x_arr[dx+1];    
int y_arr[dx+1];
 int P_arr[dx+1];
 int P_all=0;


for(int i = 0; i <dx+1; i++){
x_arr[i]=x;
y_arr[i]=y;
P_arr[i]=P;
if (P>0){
P_all = P+ diff_1;
y=y+1;
}
if (P<=0){
P_all = P + twice_dy;
}
P=P_all;
x=x+1;
}
Serial.println(dx+1);


 for(int i = 0; i < haha; i++)
    {
        x_arr_alt[i] = x_arr[i];
        
         Serial.print(x_arr_alt[i]); Serial.print(",");
        
    }
     Serial.println("stop");
//int x_arr_array[dx+1]={x_arr};
for(int i = 0; i <dx+1; i++){
  // Serial.print(x_arr[i]);  Serial.print(",");
  
};
//Serial.println("skip");
for(int i = 0; i <dx+1; i++){
  // Serial.print(y_arr[i]);  Serial.print(",");
  
};


};














const long interval = 0;   const long interval_alt = 0;   
unsigned long dirPin = 2;                 unsigned long dirPin_alt = 8;
unsigned long stepPin = 3;                       unsigned long stepPin_alt = 9;
unsigned long previousMillis = 0;   unsigned long previousMillis_alt = 0;  
unsigned long stepDelay=700;                       unsigned long stepDelay_alt=700; 
unsigned long speed_1=1;  unsigned long speed_2=2; 
void setup()  
{
  // Declare pins as Outputs
Serial.begin(9600);
  pinMode(stepPin, OUTPUT);   pinMode(stepPin_alt, OUTPUT);
  pinMode(dirPin, OUTPUT);     pinMode(dirPin_alt, OUTPUT);
}

  int arr_a = 1;
int  ccc=0;
int art[200]={ 0  ,   0  ,   1 ,    0 ,    0   ,  0   ,  0 ,    1   ,  0 ,    0   ,  0   ,  0,
               1 ,    0   ,  0   ,  0  ,   0   ,  1  ,   0   ,  0   ,  0   ,  0   ,  1 ,    0,
               0   ,  0   ,  0   ,  1  ,   0   ,  0   ,  0    , 0   ,  1 ,    0  ,   0 ,    0,
               0   ,  1   ,  0  ,   0  ,   0   ,  0   ,  1   ,  0   ,  0   ,  0  ,   0  ,   1,
               0   ,  0   ,  0  ,   0  ,   1  ,   0   ,  0  ,   0   ,  0    , 1   ,  0    , 0,
               0   ,  0   ,  1   ,  0  ,   0  ,   0   ,  0   ,  1   ,  0    , 0   ,  0   ,  0,
               1   ,  0   ,  0   ,  0  ,   0   ,  1   ,  0   ,  0   ,  0    , 0   ,  1   ,  0,
               0  ,   0   ,  0   ,  1   ,  0   ,  0   ,  0   ,  0   ,  1    , 0   ,  0   ,  0,
               0   ,  1   ,  0  ,   0   ,  0   ,  0   ,  1   ,  0   ,  0    , 0  ,   0   ,  1,
               0   ,  0   ,  0  ,   0   ,  1   ,  0   ,  0   ,  0   ,  0    , 1  ,   0   ,  0,
               0   ,  0   ,  1   ,  0   ,  0   ,  0    , 0   ,  1   ,  0    , 0   ,  0    , 0,
               1  ,   0   ,  0   ,  0   ,  0   ,  1   ,  0   ,  0   ,  0    , 0  ,   1   ,  0,
               0  ,   0   ,  0   ,  1   ,  0   ,  0   ,  0   ,  0   ,  1    , 0  ,   0   ,  0,
               0   ,  1   ,  0   ,  0   ,  0   ,  0   ,  1   ,  0   ,  0    , 0  ,   0   ,  1,
               0  ,   0   ,  0   ,  0   ,  1   ,  0   ,  0   ,  0   ,  0    , 1  ,   0   ,  0,
               0   ,  0   ,  1   ,  0    , 0   ,  0   ,  0   ,  1  ,   0  ,   0  ,   0   ,  0,
                1   ,  0   ,  0  ,   0   ,  0   ,  1   ,  0  ,   0};
unsigned long countt= 0; unsigned long stopper=0;
void loop() {
get_all_coor();
  for(int x = 0; x < 200; x++){
    speed_1=art[ccc];  speed_2=1; 
haha();
haha_2();

            ccc=ccc+1;
  }

  
delay(1000000);
}





int haha(){
  digitalWrite(dirPin, HIGH);
  // Spin motor
  for(int x = 0; x < speed_1; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay);
  }

}


int haha_2(){
  digitalWrite(dirPin_alt, HIGH);
  // Spin motor
  for(int x = 0; x < speed_2; x++)
  {
    digitalWrite(stepPin_alt, HIGH);
    delayMicroseconds(stepDelay_alt);
    digitalWrite(stepPin_alt, LOW);
    delayMicroseconds(stepDelay_alt);
  }

}

Aucun commentaire:

Enregistrer un commentaire