Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int numReadingsZ = 5;
- const int numReadingsX = 5;
- const int numReadingsY = 5;
- const int numReadingsZrotate = 5;
- const int numReadingssliderLeft = 5;
- const int numReadingssliderRight = 5;
- int readingsZ[numReadingsZ]; // the readings from the analog input
- int readIndexZ = 0; // the index of the current reading
- int totalZ = 0; // the running total
- int averageZ = 0; // the average
- int readingsX[numReadingsX];
- int readIndexX = 0;
- int totalX = 0;
- int averageX = 0;
- int readingsY[numReadingsY];
- int readIndexY = 0;
- int totalY = 0;
- int averageY = 0;
- int readingsZrotate[numReadingsZrotate];
- int readIndexZrotate = 0;
- int totalZrotate = 0;
- int averageZrotate = 0;
- int readingssliderLeft[numReadingssliderLeft];
- int readIndexsliderLeft = 0;
- int totalsliderLeft = 0;
- int averagesliderLeft = 0;
- int readingssliderRight[numReadingssliderRight];
- int readIndexsliderRight = 0;
- int totalsliderRight = 0;
- int averagesliderRight = 0;
- int inputPinZ = A4;
- int inputPinX = A5;
- int inputPinY = A6;
- int inputPinZrotate = A8;
- int inputPinsliderLeft = A7;
- int inputPinsliderRight = A9;
- void setup() {
- Serial.begin(9600);
- for (int thisReadingZ = 0; thisReadingZ < numReadingsZ; thisReadingZ++) { // initialize serial communication with computer:
- readingsZ[thisReadingZ] = 0; // initialize all the readings to 0:
- }
- for (int thisReadingX = 0; thisReadingX < numReadingsX; thisReadingX++) {
- readingsX[thisReadingX] = 0;
- }
- for (int thisReadingY = 0; thisReadingY < numReadingsY; thisReadingY++) {
- readingsY[thisReadingY] = 0;
- }
- for (int thisReadingZrotate = 0; thisReadingZrotate < numReadingsZrotate; thisReadingZrotate++) {
- readingsZrotate[thisReadingZrotate] = 0;
- }
- for (int thisReadingsliderLeft = 0; thisReadingsliderLeft < numReadingssliderLeft; thisReadingsliderLeft++) {
- readingssliderLeft[thisReadingsliderLeft] = 0;
- }
- for (int thisReadingsliderRight = 0; thisReadingsliderRight < numReadingssliderRight; thisReadingsliderRight++) {
- readingssliderRight[thisReadingsliderRight] = 0;
- }
- pinMode(0, INPUT_PULLUP);
- pinMode(1, INPUT_PULLUP);
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(5, INPUT_PULLUP);
- pinMode(6, INPUT_PULLUP);
- pinMode(7, INPUT_PULLUP);
- pinMode(8, INPUT_PULLUP);
- pinMode(9, INPUT_PULLUP);
- pinMode(10, INPUT_PULLUP);
- pinMode(11, INPUT_PULLUP);
- pinMode(12, INPUT_PULLUP);
- pinMode(24, INPUT_PULLUP);
- pinMode(25, INPUT_PULLUP);
- pinMode(26, INPUT_PULLUP);
- pinMode(27, INPUT_PULLUP);
- pinMode(28, INPUT_PULLUP);
- pinMode(29, INPUT_PULLUP);
- pinMode(30, INPUT_PULLUP);
- pinMode(31, INPUT_PULLUP);
- pinMode(32, INPUT_PULLUP);
- pinMode(33, INPUT_PULLUP);
- pinMode(34, INPUT_PULLUP);
- pinMode(35, INPUT_PULLUP);
- pinMode(36, INPUT_PULLUP);
- pinMode(37, INPUT_PULLUP);
- pinMode(38, INPUT_PULLUP);
- pinMode(39, INPUT_PULLUP);
- pinMode(14, INPUT_PULLUP);
- pinMode(15, INPUT_PULLUP);
- pinMode(16, INPUT_PULLUP);
- pinMode(17, INPUT_PULLUP);
- }
- void loop() {
- totalZ = totalZ - readingsZ[readIndexZ]; // subtract the last reading:
- readingsZ[readIndexZ] = analogRead(inputPinZ); // read from the sensor:
- totalZ = totalZ + readingsZ[readIndexZ]; // add the reading to the total:
- readIndexZ = readIndexZ + 1; // advance to the next position in the array:
- if (readIndexZ >= numReadingsZ) { // if we're at the end of the array...
- readIndexZ = 0; // ...wrap around to the beginning:
- }
- averageZ = totalZ / numReadingsZ; // calculate the average:
- // averageZ = map(averageZ, 695, 780, 0, 1023);
- // averageZ = constrain(averageZ, 0, 1023);
- totalX = totalX - readingsX[readIndexX];
- readingsX[readIndexX] = analogRead(inputPinX);
- totalX = totalX + readingsX[readIndexX];
- readIndexX = readIndexX + 1;
- if (readIndexX >= numReadingsX) {
- readIndexX = 0;
- }
- averageX = totalX / numReadingsX;
- totalY = totalY - readingsY[readIndexY];
- readingsY[readIndexY] = analogRead(inputPinY);
- totalY = totalY + readingsY[readIndexY];
- readIndexY = readIndexY + 1;
- if (readIndexY >= numReadingsY) {
- readIndexY = 0;
- }
- averageY = totalY / numReadingsY;
- totalZrotate = totalZrotate - readingsZrotate[readIndexZrotate];
- readingsZrotate[readIndexZrotate] = 1023-analogRead(inputPinZrotate);
- totalZrotate = totalZrotate + readingsZrotate[readIndexZrotate];
- readIndexZrotate = readIndexZrotate + 1;
- if (readIndexZrotate >= numReadingsZrotate) {
- readIndexZrotate = 0;
- }
- averageZrotate = totalZrotate / numReadingsZrotate;
- totalsliderLeft = totalsliderLeft - readingssliderLeft[readIndexsliderLeft];
- readingssliderLeft[readIndexsliderLeft] = analogRead(inputPinsliderLeft);
- totalsliderLeft = totalsliderLeft + readingssliderLeft[readIndexsliderLeft];
- readIndexsliderLeft = readIndexsliderLeft + 1;
- if (readIndexsliderLeft >= numReadingssliderLeft) {
- readIndexsliderLeft = 0;
- }
- averagesliderLeft = totalsliderLeft / numReadingssliderLeft;
- totalsliderRight = totalsliderRight - readingssliderRight[readIndexsliderRight];
- readingssliderRight[readIndexsliderRight] = analogRead(inputPinsliderRight);
- totalsliderRight = totalsliderRight + readingssliderRight[readIndexsliderRight];
- readIndexsliderRight = readIndexsliderRight + 1;
- if (readIndexsliderRight >= numReadingssliderRight) {
- readIndexsliderRight = 0;
- }
- averagesliderRight = totalsliderRight / numReadingssliderRight;
- Serial.println(averageZ); // send it to the computer as ASCII digits
- Joystick.Z(averageZ);
- Joystick.X(averageX);
- Joystick.Y(averageY);
- Joystick.Zrotate(averageZrotate);
- Joystick.sliderLeft(averagesliderLeft);
- Joystick.sliderRight(averagesliderRight);
- if (digitalRead(0) == LOW) {
- Joystick.button(1, 1);
- }
- else {
- Joystick.button(1, 0);
- }
- if (digitalRead(1) == LOW) {
- Joystick.button(2, 1);
- }
- else {
- Joystick.button(2, 0);
- }
- if (digitalRead(2) == LOW) {
- Joystick.button(3, 1);
- }
- else {
- Joystick.button(3, 0);
- }
- if (digitalRead(3) == LOW) {
- Joystick.button(4, 1);
- }
- else {
- Joystick.button(4, 0);
- }
- if (digitalRead(4) == LOW) {
- Joystick.button(5, 1);
- }
- else {
- Joystick.button(5, 0);
- }
- if (digitalRead(5) == LOW) {
- Joystick.button(6, 1);
- }
- else {
- Joystick.button(6, 0);
- }
- if (digitalRead(6) == LOW) {
- Joystick.button(7, 1);
- }
- else {
- Joystick.button(7, 0);
- }
- if (digitalRead(7) == LOW) {
- Joystick.button(8, 1);
- }
- else {
- Joystick.button(8, 0);
- }
- if (digitalRead(8) == LOW) {
- Joystick.button(9, 1);
- }
- else {
- Joystick.button(9, 0);
- }
- if (digitalRead(9) == LOW) {
- Joystick.button(10, 1);
- }
- else {
- Joystick.button(10, 0);
- }
- if (digitalRead(10) == LOW) {
- Joystick.button(11, 1);
- }
- else {
- Joystick.button(11, 0);
- }
- if (digitalRead(11) == LOW) {
- Joystick.button(12, 1);
- }
- else {
- Joystick.button(12, 0);
- }
- if (digitalRead(12) == LOW) {
- Joystick.button(13, 1);
- }
- else {
- Joystick.button(13, 0);
- }
- if (digitalRead(24) == LOW) {
- Joystick.button(14, 1);
- }
- else {
- Joystick.button(14, 0);
- }
- if (digitalRead(25) == LOW) {
- Joystick.button(15, 1);
- }
- else {
- Joystick.button(15, 0);
- }
- if (digitalRead(26) == LOW) {
- Joystick.button(16, 1);
- }
- else {
- Joystick.button(16, 0);
- }
- if (digitalRead(27) == LOW) {
- Joystick.button(17, 1);
- }
- else {
- Joystick.button(17, 0);
- }
- if (digitalRead(28) == LOW) {
- Joystick.button(18, 1);
- }
- else {
- Joystick.button(18, 0);
- }
- if (digitalRead(29) == LOW) {
- Joystick.button(19, 1);
- }
- else {
- Joystick.button(19, 0);
- }
- if (digitalRead(30) == LOW) {
- Joystick.button(20, 1);
- }
- else {
- Joystick.button(20, 0);
- }
- if (digitalRead(31) == LOW) {
- Joystick.button(21, 1);
- }
- else {
- Joystick.button(21, 0);
- }
- if (digitalRead(32) == LOW) {
- Joystick.button(22, 1);
- }
- else {
- Joystick.button(22, 0);
- }
- if (digitalRead(33) == LOW) {
- Joystick.button(23, 1);
- }
- else {
- Joystick.button(23, 0);
- }
- if (digitalRead(34) == LOW) {
- Joystick.button(24, 1);
- }
- else {
- Joystick.button(24, 0);
- }
- if (digitalRead(35) == LOW) {
- Joystick.button(25, 1);
- }
- else {
- Joystick.button(25, 0);
- }
- if (digitalRead(36) == LOW) {
- Joystick.button(26, 1);
- }
- else {
- Joystick.button(26, 0);
- }
- if (digitalRead(37) == LOW) {
- Joystick.button(27, 1);
- }
- else {
- Joystick.button(27, 0);
- }
- if (digitalRead(38) == LOW) {
- Joystick.button(28, 1);
- }
- else {
- Joystick.button(28, 0);
- }
- if (digitalRead(39) == LOW) {
- Joystick.button(29, 1);
- }
- else {
- Joystick.button(29, 0);
- }
- if (digitalRead(14) == LOW) {
- Joystick.button(30, 1);
- }
- else {
- Joystick.button(30, 0);
- }
- if (digitalRead(15) == LOW) {
- Joystick.button(31, 1);
- }
- else {
- Joystick.button(31, 0);
- }
- if (digitalRead(16) == LOW) {
- Joystick.button(32, 1);
- }
- else {
- Joystick.button(32, 0);
- }
- // if (digitalRead(17) == LOW) {
- // Joystick.button(X, 1);
- // }
- // else {
- // Joystick.button(X, 0);
- // }
- // delay(1); // delay in between reads for stability
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement