Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* ジョイスティックモジュールショートコード
*/
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