How to adjust 2 different sliders?

sorry please help me. i am having error with my program, when i am trying to create 1 horizontal slider and 1 vertical slider to send control signal..my vertical slider can control up and down but to the horizontal slider I can't control left or right but it goes up and down


pidx_tangy_quayz_apppp.ino (4.7 KB)

I can't get to my PC to read your ino file yet, but I can imagine it's going to have a hard time trying to figure out whether the incoming bytes are for x vs y.

What is your convention?

Come to think of it, consider the one byte number limits.

That's -128 to 127 (give or take (

But what arrives if you try to send 200?

My convention is that I give y=100 between the values ​​0 and 200. When I give y>100 the motor will decrease, y<100 the motor will fly up. Similarly x will tilt left and right

When I drag my y slider, its value increases and decreases, helping the motor fly. but when i try to drag the x slider it doesn't tilt left or right but it flies up and down is there a slider conflict here. help me

control_slider.aia (79.5 KB)
This is my interface

Please help me, I really need it

How to adjust 2 different sliders

In the part where I send my value, is there an error somewhere that causes when I slide the vertical bar and the horizontal bar to be the same

Will my horizontal slider overlap the vertical slider? Why is it that when I control the vertical slider, I drag the horizontal slider and it also works? Is there any way to fix it?

I will copy some of your relevant code here, for more experienced C coders to comment.

Here's the initialization of the variables that will receive data from AI2 via Bluetooth:

int x_n=100, y_n=0;
int x_d=0, y_d=100;
int y_d_dc,x_n_dc;
int y_d_cn=100, x_n_cn=100;

and here's the loop receiving the data:



void loop() {
  gyro_signals();
  timer_overflow();


    
  while( Serial1.available() >=2  )
  {

    y_d = Serial1.read();
    delay(1);
    x_n = Serial1.read();


    
  } 
//    x_N=100, y_N=0;
//    x_D=0, y_D=100;
   

    if( y_d> 99 && y_d<101&& x_n>99 && x_n<101){gocCBx = 0; congy = 0;}


      if(y_d<y_d_cn){
    
       // tính bước giảm của y dọc
       y_d_dc = y_d_cn - y_d;
       
       //cong y tương ứng với y dọc giảm
       congy += y_d_dc;
       
       // cập nhật giá trị y dọc đi lên
       y_d_cn = y_d;
      } 
      else{
      //kiểm tra xem y_d có tăng so với giá trị trước đó không
        if(y_d>y_d_cn){
       // tính bước tăng của y dọc
       y_d_dc = y_d - y_d_cn;
       
       //trừ y tương ứng với y dọc giảm
       congy -= y_d_dc;
       
       // cập nhật giá trị y dọc đi xuống
       y_d_cn = y_d;
      }
    }
    if(x_n>99 && x_n <101){
    if(x_n<99){gocCBx+=1;if(gocCBx>=3)gocCBx=3;}
    else if(x_n>101){gocCBx-=1;if(gocCBx<=-3)gocCBx=-3;}
    }

  }


(I checked the two procedures gyro_signals and timer_overflow for Serial1 data traffic. There is none.)

I see you checking if there are at least 2 bytes available on Serial1, and reading them consecutively into int variables y_d and x_n:

while( Serial1.available() >=2  )
  {

    y_d = Serial1.read();
    delay(1);
    x_n = Serial1.read();


    
  } 

Because of the while() loop, you are only keeping the last 2 bytes.
You are also assuming the byte pairs are arriving interleaved, in x and y pairs judging by your variable names.

Then you do some range checking:


    if( y_d> 99 && y_d<101&& x_n>99 && x_n<101){gocCBx = 0; congy = 0;}


      if(y_d<y_d_cn){
    
       // tính bước giảm của y dọc
       y_d_dc = y_d_cn - y_d;
       
       //cong y tương ứng với y dọc giảm
       congy += y_d_dc;
       
       // cập nhật giá trị y dọc đi lên
       y_d_cn = y_d;
      } 
      else{
      //kiểm tra xem y_d có tăng so với giá trị trước đó không
        if(y_d>y_d_cn){
       // tính bước tăng của y dọc
       y_d_dc = y_d - y_d_cn;
       
       //trừ y tương ứng với y dọc giảm
       congy -= y_d_dc;
       
       // cập nhật giá trị y dọc đi xuống
       y_d_cn = y_d;
      }
    }
    if(x_n>99 && x_n <101){
    if(x_n<99){gocCBx+=1;if(gocCBx>=3)gocCBx=3;}
    else if(x_n>101){gocCBx-=1;if(gocCBx<=-3)gocCBx=-3;}
    }

I don't see any places where you check for negative incoming values, so at first I thought that you should have been sending UnsignedBytes from AI2, but a check of the Bluetooth Blocks Pallette has no such block. (It turns out that Ai2 send unsigned bytes 0-255, and I guessed wrong.)

But there is something we can do to force interleaving of x and y values, by sending lists of (y,x) bytes. I am going to assume 100 is the midpoint value of one coordinate while you are sending the other value.

There is a 50% probability I an sending these pairs in the wrong order. Testing should reveal if I got lucky or not.

Instead of these:




I am guessing you instead need these:





control_slider (1).aia (79.9 KB)

Are you seeing the sprites move on the wrong Canvases?

That's a different problem than the data stream problem.

By the way, Table Arrangements are tricky, and best avoided.

They can corrupt a Project if you subject them to drag and drop in the Designer.

Thank you, really thank you. Your changes have made my project work very stable. Thank you for your advice, wish you good health!!!!!!!

thanks you!!! thanks you!!!thanks you!!!

thanks you very much

I'm surprised it works.

That double slider inside a Table Arrangement looked very awkward.

I would have tried for a single Canvas, single Sprite in the center, and a two dimensional drag to send each (y,x) pair.

1 Like

Thank you, I'm new to mit app inventor in my project so I'm still very confused. I will try your idea

Before you try it, export your current project and save the .aia file somewhere safe so it doesn't get overwritten or lost.

This is a big change.

Why run your project for a while? I feel like the engine is very strange

When I accelerate the motor to fly and pull the slider to change the right amount of angle, my motor seems to lose control.