Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int numReadings = 10;
- int readings[numReadings]; // the readings from the analog input
- int readIndex = 0; // the index of the current reading
- int total = 0; // the running total
- int average = 0; // the average
- int inputPin = 38;
- int throttleNow;
- int throttleThisSession;
- int throttledeviation;
- int throttleVal;
- int brakeNow;
- int brakeThisSession;
- int brakedeviation;
- int brakeVal;
- void setup() {
- // Analog wheel
- // initialize serial communication with computer:
- Serial.begin(9600);
- // initialize all the readings to 0:
- for (int thisReading = 0; thisReading < numReadings; thisReading++) {
- readings[thisReading] = 0;
- }
- // Analog throttle pedal
- pinMode(39, INPUT);
- throttleThisSession = analogRead(39);
- throttledeviation = throttleThisSession;
- // Analog brake pedal
- pinMode(40, INPUT);
- brakeThisSession = analogRead(40);
- brakedeviation = brakeThisSession;
- //Left buttons
- pinMode(27, INPUT_PULLUP);
- pinMode(0, INPUT_PULLUP);
- pinMode(1, INPUT_PULLUP);
- pinMode(2, INPUT_PULLUP);
- //Right buttons
- pinMode(3, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(5, INPUT_PULLUP);
- pinMode(10, INPUT_PULLUP);
- //Hat
- pinMode(11, INPUT_PULLUP);
- pinMode(12, INPUT_PULLUP);
- pinMode(13, INPUT_PULLUP);
- pinMode(14, INPUT_PULLUP);
- //Centre button
- pinMode(15, INPUT_PULLUP);
- //Mode button
- pinMode(16, INPUT_PULLUP);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- // Analog wheel
- // subtract the last reading:
- total = total - readings[readIndex];
- // read from the sensor:
- readings[readIndex] = analogRead(inputPin);
- // add the reading to the total:
- total = total + readings[readIndex];
- // advance to the next position in the array:
- readIndex = readIndex + 1;
- // if we're at the end of the array...
- if (readIndex >= numReadings) {
- // ...wrap around to the beginning:
- readIndex = 0;
- }
- // calculate the average:
- average = total / numReadings;
- // send it to the computer as ASCII digits
- Serial.println(average);
- delay(1); // delay in between reads for stability
- Joystick.X(average);
- //Throttle pedal
- throttleNow = analogRead(39)- 20;
- throttleVal = (throttledeviation - throttleNow);
- map(throttleVal, 0, 1, 0, 1023);
- Joystick.Z(throttleVal);
- //Brake pedal
- brakeNow = (0.99 * analogRead(40));
- brakeVal = (brakedeviation - brakeNow);
- map(brakeVal, 0, 1, 0, 1023);
- Joystick.slider(brakeVal);
- // All buttons________
- if (digitalRead(27) == LOW) { Joystick.button(1, 1); }
- else
- { Joystick.button(1, 0);}
- // ________
- if (digitalRead(0) == LOW) { Joystick.button(2, 1); }
- else
- { Joystick.button(2, 0);}
- // ________
- if (digitalRead(1) == LOW) { Joystick.button(3, 1); }
- else
- { Joystick.button(3, 0);}
- // ________
- if (digitalRead(2) == LOW) { Joystick.button(4, 1); }
- else
- { Joystick.button(4, 0);}
- // ________
- if (digitalRead(3) == LOW) { Joystick.button(5, 1); }
- else
- { Joystick.button(5, 0);}
- // ________
- if (digitalRead(4) == LOW) { Joystick.button(6, 1); }
- else
- { Joystick.button(6, 0);}
- // ________
- if (digitalRead(5) == LOW) { Joystick.button(7, 1); }
- else
- { Joystick.button(7, 0);}
- // ________
- if (digitalRead(10) == LOW) { Joystick.button(8, 1); }
- else
- { Joystick.button(8, 0);}
- // ________
- if (digitalRead(15) == LOW) { Joystick.button(13, 1); }
- else
- { Joystick.button(13, 0);}
- // ________
- if (digitalRead(16) == LOW) { Joystick.button(14, 1); }
- else
- { Joystick.button(14, 0);}
- //Hat buttons
- // Hat up________
- if (digitalRead(11) == LOW) { Joystick.hat(0); }
- // Hat 45________
- if ((digitalRead(11) == LOW)
- && (digitalRead(13) ==LOW)) { Joystick.hat(45); }
- // Hat right________
- if (digitalRead(13) == LOW) { Joystick.hat(90); }
- // Hat 135________
- if ((digitalRead(13) == LOW)
- && (digitalRead(12) ==LOW)) { Joystick.hat(135); }
- // Hat down________
- if (digitalRead(12) == LOW) { Joystick.hat(180); }
- // Hat 225________
- if ((digitalRead(14) == LOW)
- && (digitalRead(12) ==LOW)) { Joystick.hat(225); }
- // Hat left________
- if (digitalRead(14) == LOW) { Joystick.hat(270); }
- // Hat 315________
- if ((digitalRead(14) == LOW)
- && (digitalRead(11) ==LOW)) { Joystick.hat(315); }
- // Hat centred
- if ((digitalRead(11) == HIGH)
- && (digitalRead(12) == HIGH)
- && (digitalRead(13) == HIGH)
- && (digitalRead(14) == HIGH)) {Joystick.hat(-1); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement