Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- SoftwareSerial BT(10,9);
- int yPin = A0;
- int xPin = A1;
- int statePin = 3;
- int xPosition = 0;
- int yPosition = 0;
- int state = 0; // State of joystick (pressed or unpressed)
- char sendData; // Placeholder variable used to trigger a block of code in the slave code set.
- void setup() {
- // put your setup code here, to run once:
- BT.begin(9600);
- Serial.begin(9600);
- pinMode(xPin, INPUT);
- pinMode(yPin, INPUT);
- pinMode(statePin, INPUT);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- /*if (BT.available()) {
- //Will print on the serial monitor if some data is receieved.
- Serial.println("Got Some Data");
- Data[2] = BT.read();
- Serial.println(Data);
- }*/
- //The variables for xposition, yposition, and state are set as the values given by the arduino at the digital and analog ports.
- xPosition = analogRead(xPin);
- yPosition = analogRead(yPin);
- state = digitalRead(statePin);
- //Serial.print("X: ");
- //Serial.print(xPosition);
- //Serial.print(" | Y: ");
- //Serial.print(yPosition);
- Serial.print(" | Button: ");
- Serial.println(state);
- if (state == 0) {
- //This is the block of code for when the joystick is NOT pressed down.
- if (xPosition >= 450 & xPosition <= 550 & yPosition >=450 & yPosition <= 550) {
- //It will send this number for sendData if the joystick is not moved at all.
- sendData = 1;
- }
- if (yPosition > 550) {
- //It will send this number for sendData if the joystick is pushed up.
- sendData = 2;
- }
- if (yPosition < 450) {
- //It will send this number for sendData if the joystick is pushed down.
- sendData = 3;
- }
- if (xPosition > 550) {
- //It will send this number for sendData if the joystick is pushed right.
- sendData = 4;
- }
- if (xPosition < 450) {
- sendData = 5;
- }
- BT.write(sendData);
- Serial.print(int(sendData));
- }
- if (state == 1) {
- //This is the block of code for when the joystick is NOT pressed down.
- if (yPosition >= 450 & yPosition <= 550 & xPosition >= 450 & xPosition <= 550) {
- //It will send this number for sendData if the joystick is not moved at all.
- sendData = 6;
- }
- if (yPosition > 550) {
- //It will send this number for sendData if the joystick is pushed up.
- sendData = 7;
- }
- if (yPosition < 450) {
- //It will send this number for sendData if the joystick is pushed down.
- sendData = 8;
- }
- if (xPosition < 450) {
- sendData = 9;
- }
- if (xPosition > 550) {
- //It will send this number for sendData if the joystick is pushed right.
- sendData = 10;
- }
- BT.write(sendData);
- Serial.print(int(sendData));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment