Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active December 7, 2018 02:36
Show Gist options
  • Save baobao/57954adf485cdf4cb19a6e2d6dd71994 to your computer and use it in GitHub Desktop.
Save baobao/57954adf485cdf4cb19a6e2d6dd71994 to your computer and use it in GitHub Desktop.
/**
* ジョイスティックモジュールショートコード
*/
int x_Pin = A5;
int y_Pin = A4;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int raw_x_val = analogRead(x_Pin);
// スティックの上方向を+にする
int raw_y_val = -analogRead(y_Pin);
// -1.0 ~ 1.0の値に変換する
int tempX = 2 * raw_x_val - 1024;
int tempY = 2 * raw_y_val + 1024;
float x_val = (float)tempX / 1024;
float y_val = (float)tempY / 1024;
Serial.print("x : ");
Serial.print(x_val);
Serial.print(" | ");
Serial.print(raw_x_val);
Serial.print(" / y : ");
Serial.print(y_val);
Serial.print(" | ");
Serial.println(raw_y_val);
delay(300);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment