1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5NanoC6、アナログジョイスティックの0点の偏差値を求めて遊ぶ。

Last updated at Posted at 2025-02-20

参考

注意

  • 過去ログを見よ!!!
  • ESP32 Arduino 3.1.2
  • 片側

目的

ADC(アナログ-デジタル-コンバータ)にアナログジョイスティックをつなぎ、中心点の時のバラツキを求める。

結果

o_coq824.jpg

イメージ

o_coq818.jpg

プログラム




//ser_0ten_test1_nanoc6


#define CP (uint8_t *)
#define G_X 2
#define G_Y 1

float o_yl = 0; //L側の0(ゼロ)点
float o_yr = 0; //R側の0(ゼロ)点

void setup() {
  // put your setup code here, to run once:

  //シリアルの初期化 //debug(1)
  Serial.begin(9600);
  Serial.println();
  //シリアルの待ちが0.5*9
  for (int i = 0; i < 9; i++) {
    Serial.print('.'); delay(500); //接続待ち
  } //for
  Serial.println();


  //gpio inte
  pinMode(G_X, ANALOG);  //gpio init
  pinMode(G_Y, ANALOG);  //gpio init

    
    int al;  //一時
    int ave; //平均
    int die; //偏差
    int n;   //個数
    int y[20];
    
    int i_sqrt[] = {
        0,1,1,1,2,2,2,2,2,3,
        3,3,3,3,3,3,4,4,4,4,
        4,4,4,4,4,5,5,5,5,5,
        5,5,5,5,5,5,6,6,6,6,
        6,6,6,6,6,6,6,6,6,7,
        7,7,7,7,7,7,7,7,7,7,
        7,7,7,7,8,8,8,8,8,8,
        8,8,8,8,8,8,8,8,8,8,
        8,9,9,9,9,9,9,9,9,9,
        9,9,9,9,9,9,9,9,9,9,
        10
    };
    
    n = 10;
    
    //R START
    int s = 0;   //input
    for (int i = 1; i <= n; i++) {
    //センサー入力する
    s = s + (y[i]=analogRead(G_X)); //センサーの値
    } //for
    o_yl = ((float)s) * 0.0001;
    Serial.printf("%f\n",o_yl);
    ave = s / n;
    Serial.printf("%d\n",ave);

    
    die = 0;
    for(int i = 1;i<=n;i++ ){
        al = (y[i] - ave);
        al = al * al;
        Serial.printf("= %d , %d\n",y[i],al);
        die  = die + al;
    }
    die = die / n;
    if(die > 100) {die = 100;}
    die = i_sqrt[ die ];
    Serial.printf("die = %d\n",die);
    Serial.printf("...\n");

}


void loop() {
  // put your main code here, to run repeatedly:

  delay(300);  //0.3秒待つ

}



1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?