Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ArduinoBLE.h>
- #include <Stepper.h>
- ///////////////////////////////////////////////////////
- // ----------------------------------------------------
- /// THIS CODE RUNS ON THE Arduino R4 - TANK
- // ----------------------------------------------------
- ///////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- // this code sends the ultra sonic distance data to the controller
- // we are the peripheral...
- ////////////////////////////////////////////////////////////////////
- // WE ARE UPDATING THE BUTTON CHARACTERISTIC AND READING THE LED STATE CHARACTERISITC
- // we send button value
- // we recieve LED value
- // variables for button:
- const int buttonPin = 2;
- int oldButtonState = LOW;
- BLEService duplexService("1000"); // create service
- BLEByteCharacteristic buttonCharacteristic("1001", BLERead | BLENotify); // create button characteristic and allow remote device to get notifications
- BLEIntCharacteristic joystick_1_characteristic("1002", BLERead | BLEWrite); // create joystick_1 characteristic and allow remote device to read and write
- BLEIntCharacteristic joystick_2_characteristic("1003", BLERead | BLEWrite); // create joystick_2 characteristic and allow remote device to read and write
- const int stepsPerRevolution = 800;
- const int stepMotor1 = 3; //step
- const int dirMotor1 = 2; //dir
- const int stepMotor2 = 5; //step
- const int dirMotor2 = 4; //dir
- const int stepMotor3 = 6; //step
- const int dirMotor3 = 7; //dir
- const int stepMotor4 = 9; //step
- const int dirMotor4 = 8; //dir
- Stepper motor1(stepsPerRevolution, stepMotor1, dirMotor1);
- Stepper motor2(stepsPerRevolution, stepMotor2, dirMotor2);
- Stepper motor3(stepsPerRevolution, stepMotor3, dirMotor3);
- Stepper motor4(stepsPerRevolution, stepMotor4, dirMotor4);
- const int MOTOR_SPEED = 60;
- int POS = 0;
- void setup()
- {
- motor1.setSpeed(0);
- motor2.setSpeed(0);
- motor3.setSpeed(0);
- motor4.setSpeed(0);
- motor1.step(0);
- motor2.step(0);
- motor3.step(0);
- motor4.step(0);
- Serial.begin(9600);
- pinMode(LED_BUILTIN, OUTPUT); // use the LED as an output
- pinMode(buttonPin, INPUT_PULLUP); // use button pin as an input
- BLE.begin(); //init BLE module
- BLE.setLocalName("duplexPeripheral"); // set the local name that the peripheral advertises
- BLE.setAdvertisedService(duplexService); // set the UUID for the service the peripheral advertises:
- duplexService.addCharacteristic(joystick_1_characteristic);
- duplexService.addCharacteristic(joystick_2_characteristic);
- duplexService.addCharacteristic(buttonCharacteristic);
- BLE.addService(duplexService); // add the service
- joystick_1_characteristic.writeValue(0);
- joystick_2_characteristic.writeValue(0);
- buttonCharacteristic.writeValue(0);
- BLE.advertise(); // start advertising on BLE network
- Serial.println("Peripheral is running");
- }
- void loop() {
- BLEDevice central = BLE.central();
- if (central)
- {
- Serial.println("Got central connection!");
- while (central.connected())
- {
- byte buttonValue = digitalRead(buttonPin);
- //check if joystick 1 characteristic has been updated...
- if (joystick_1_characteristic.written())
- {
- POS = joystick_1_characteristic.value();
- // if (joy_1_value >1000)
- // {
- // buttonCharacteristic.writeValue(20);
- // } else {
- // buttonCharacteristic.writeValue(50);
- // }
- Serial.print("Position: ");
- Serial.println(POS);
- }
- //check if joystick 2 characteristic has been updated...
- // if (joystick_2_characteristic.written())
- // {
- // int joy_2_value = joystick_2_characteristic.value();
- // digitalWrite(LED_BUILTIN, joy_2_value);
- // Serial.print(" JOY_2 data: ");
- // Serial.println(joy_2_value);
- // }
- if (POS == 0)
- {
- motor1.setSpeed(0);
- motor2.setSpeed(0);
- motor3.setSpeed(0);
- motor4.setSpeed(0);
- motor1.step(0);
- motor2.step(0);
- motor3.step(0);
- motor4.step(0);
- }
- if(POS == 1);
- {
- motor1.setSpeed(60);
- motor2.setSpeed(60);
- motor3.setSpeed(60);
- motor4.setSpeed(60);
- motor1.step(1);
- motor2.step(1);
- motor3.step(1);
- motor4.step(1);
- }
- if (POS == 2)
- {
- motor1.setSpeed(0);
- motor2.setSpeed(0);
- motor3.setSpeed(0);
- motor4.setSpeed(0);
- motor1.step(0);
- motor2.step(0);
- motor3.step(0);
- motor4.step(0);
- }
- if (POS == 3) //Right
- {
- motor1.setSpeed(40);
- motor2.setSpeed(40);
- motor3.setSpeed(40);
- motor4.setSpeed(40);
- motor1.step(1);
- motor2.step(-1);
- motor3.step(1);
- motor4.step(-1);
- }
- if (POS == 4) //Back right
- {
- motor1.setSpeed(0);
- motor2.setSpeed(0);
- motor3.setSpeed(0);
- motor4.setSpeed(0);
- motor1.step(0);
- motor2.step(0);
- motor3.step(0);
- motor4.step(0);
- }
- if (POS == 5) //Backwards
- {
- motor1.setSpeed(60);
- motor2.setSpeed(60);
- motor3.setSpeed(60);
- motor4.setSpeed(60);
- motor1.step(-1);
- motor2.step(-1);
- motor3.step(-1);
- motor4.step(-1);
- }
- if (POS == 6) //Back left
- {
- motor1.setSpeed(0);
- motor2.setSpeed(0);
- motor3.setSpeed(0);
- motor4.setSpeed(0);
- motor1.step(0);
- motor2.step(0);
- motor3.step(0);
- motor4.step(0);
- }
- if (POS == 7) //Left
- {
- motor1.setSpeed(40);
- motor2.setSpeed(40);
- motor3.setSpeed(40);
- motor4.setSpeed(40);
- motor1.step(-1);
- motor2.step(1);
- motor3.step(-1);
- motor4.step(1);
- }
- if (POS == 8) //forward left
- {
- motor1.setSpeed(0);
- motor2.setSpeed(0);
- motor3.setSpeed(0);
- motor4.setSpeed(0);
- motor1.step(0);
- motor2.step(0);
- motor3.step(0);
- motor4.step(0);
- }
- }
- // when the central disconnects, print it out:
- Serial.print("Disconnected from central: ");
- Serial.println(central.address());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment