Search
 
SCRIPT & CODE EXAMPLE
 

C

c joystick arduino

//Constants
#define NUM_JOY 2
#define MIN_VAL 0
#define MAX_VAL 1023

//Parameters
const int joyPin [2] = {A0, A1};
const int joyBtn  = 2;
const int joyOffset  = 0;

//Variables
int joyVal [NUM_JOY] = {0, 0};

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
  //Init Joystick
  for (int i = 0; i < NUM_JOY; i++) pinMode(joyPin[i], INPUT);
}

void loop() {
  readJoystick();
  delay(500);
}

void readJoystick( ) { /* function readJoystick */
  ////Test routine for Joystick
  for (int i = 0; i < NUM_JOY; i++) {
    joyVal[i] = analogRead(joyPin[i]);
    Serial.print(F("joy")); Serial.print(i); Serial.print(F(" : ")); Serial.println(joyVal[i]);
  }
  for (int i = 0; i < NUM_JOY; i++) {
    Serial.print(F("Conv")), Serial.print(i); Serial.print(F(" : ")); Serial.println(joyRawToPhys(joyVal[i]));
  }
  if (!digitalRead(joyBtn)) {
    Serial.println(F("Joy Button pressed"));
  }
}

float joyRawToPhys(int raw) { /* function joyRawToPhys */
  ////Joystick conversion rule
  float phys = map(raw, MIN_VAL, MAX_VAL, -100 + joyOffset, 100 + joyOffset) - joyOffset; 

  return phys;
}

Copy
Comment

PREVIOUS NEXT
Code Example
C :: clarity ppm jasper domain commands 
C :: add last in list c 
C :: logarithmus c math.h 
C :: analog clock c code for turbo 
C :: allocate a array on strings in c 
C :: wait system call 
C :: python to c converter online free 
C :: Print fabionci with fork in C 
C :: anticonstitutionnellement 
C :: Manage Menu Driven Program using switch statement 
C :: how to compress image in c 
C :: function of science writing of a number 
C :: qgraphicsscene save all items to file 
C :: i765 OPT filing fees october 2 
C :: c declare float 
C :: what is implicit typecasting 
C :: c get string 
C :: install zoom on ubuntu 
Dart :: textfield border radius flutter 
Dart :: text fieldform color flutter 
Dart :: multi dex flutter 
Dart :: flutter appbar icon 
Dart :: flutter get device width 
Dart :: flutter scroll to bottom 
Dart :: flutter getx snackbar 
Dart :: MaterialStateProperty<Color? flutter 
Dart :: how to replace commas in model array of strings in dart 
Dart :: remove duplicates from array dart 
Dart :: dash border style flutter 
Dart :: dart date add day 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =