Guest User

Untitled

a guest
May 1st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial BT(10,9);
  3. int yPin = A0;
  4. int xPin = A1;
  5. int statePin = 3;
  6. int xPosition = 0;
  7. int yPosition = 0;
  8. int state = 0; // State of joystick (pressed or unpressed)
  9. char sendData; // Placeholder variable used to trigger a block of code in the slave code set.
  10.  
  11. void setup() {
  12. // put your setup code here, to run once:
  13. BT.begin(9600);
  14. Serial.begin(9600);
  15. pinMode(xPin, INPUT);
  16. pinMode(yPin, INPUT);
  17. pinMode(statePin, INPUT);
  18. }
  19.  
  20. void loop() {
  21. // put your main code here, to run repeatedly:
  22. /*if (BT.available()) {
  23. //Will print on the serial monitor if some data is receieved.
  24. Serial.println("Got Some Data");
  25. Data[2] = BT.read();
  26. Serial.println(Data);
  27. }*/
  28. //The variables for xposition, yposition, and state are set as the values given by the arduino at the digital and analog ports.
  29. xPosition = analogRead(xPin);
  30. yPosition = analogRead(yPin);
  31. state = digitalRead(statePin);
  32. //Serial.print("X: ");
  33. //Serial.print(xPosition);
  34. //Serial.print(" | Y: ");
  35. //Serial.print(yPosition);
  36. Serial.print(" | Button: ");
  37. Serial.println(state);
  38. if (state == 0) {
  39. //This is the block of code for when the joystick is NOT pressed down.
  40. if (xPosition >= 450 & xPosition <= 550 & yPosition >=450 & yPosition <= 550) {
  41. //It will send this number for sendData if the joystick is not moved at all.
  42. sendData = 1;
  43. }
  44. if (yPosition > 550) {
  45. //It will send this number for sendData if the joystick is pushed up.
  46. sendData = 2;
  47. }
  48. if (yPosition < 450) {
  49. //It will send this number for sendData if the joystick is pushed down.
  50. sendData = 3;
  51. }
  52. if (xPosition > 550) {
  53. //It will send this number for sendData if the joystick is pushed right.
  54. sendData = 4;
  55. }
  56. if (xPosition < 450) {
  57. sendData = 5;
  58. }
  59. BT.write(sendData);
  60. Serial.print(int(sendData));
  61. }
  62. if (state == 1) {
  63. //This is the block of code for when the joystick is NOT pressed down.
  64. if (yPosition >= 450 & yPosition <= 550 & xPosition >= 450 & xPosition <= 550) {
  65. //It will send this number for sendData if the joystick is not moved at all.
  66. sendData = 6;
  67. }
  68. if (yPosition > 550) {
  69. //It will send this number for sendData if the joystick is pushed up.
  70. sendData = 7;
  71. }
  72. if (yPosition < 450) {
  73. //It will send this number for sendData if the joystick is pushed down.
  74. sendData = 8;
  75. }
  76. if (xPosition < 450) {
  77. sendData = 9;
  78. }
  79. if (xPosition > 550) {
  80. //It will send this number for sendData if the joystick is pushed right.
  81. sendData = 10;
  82. }
  83. BT.write(sendData);
  84. Serial.print(int(sendData));
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment