Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string.h>
  2. #include <Arduino.h>
  3. #include <SPI.h>
  4. #if not defined (_VARIANT_ARDUINO_DUE_X_)
  5. //#include <SoftwareSerial.h>
  6. #endif
  7.  
  8. #include "Adafruit_BLE.h"
  9. #include "Adafruit_BluefruitLE_SPI.h"
  10. #include "Adafruit_BluefruitLE_UART.h"
  11.  
  12. #include "BluefruitConfig.h"
  13.  
  14. #include <Wire.h>
  15.  
  16. #define frontLeftMotor 13
  17. #define backLeftMotor 12
  18. #define frontRightMotor 11
  19. #define backRightMotor 10
  20.  
  21.  
  22. //Name your RC here
  23.  
  24. String BROADCAST_NAME = "ASSpelunker";
  25.  
  26. String BROADCAST_CMD = String("AT+GAPDEVNAME=" + BROADCAST_NAME);
  27.  
  28. Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
  29.  
  30. // A small helper
  31. void error(const __FlashStringHelper*err) {
  32.   Serial.println(err);
  33.   while (1);
  34. }
  35.  
  36. // function prototypes over in packetparser.cpp
  37. uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
  38. float parsefloat(uint8_t *buffer);
  39. void printHex(const uint8_t * data, const uint32_t numBytes);
  40.  
  41. // the packet buffer
  42. extern uint8_t packetbuffer[];
  43.  
  44. char buf[60];
  45.  
  46. void setup(void)
  47. {
  48.  
  49.   pinMode(frontLeftMotor, OUTPUT);
  50.   pinMode(backLeftMotor, OUTPUT);
  51.   pinMode(frontRightMotor, OUTPUT);
  52.   pinMode(backRightMotor, OUTPUT);
  53.  
  54.   //digitalWrite(13, HIGH);
  55.   //digitalWrite(10, HIGH);
  56.   //digitalWrite(12, LOW);
  57.   //digitalWrite(11, LOW);
  58.   //Serial.begin(9600);
  59.  
  60.  Serial.begin(115200);
  61.  
  62.   /* Initialize the module */
  63.   BLEsetup();
  64.  
  65. }
  66.  
  67.  
  68. bool modeToggle = false;
  69.  
  70. void loop(void)
  71. {
  72.    
  73.     //digitalWrite(13, HIGH);
  74.     //digitalWrite(10, HIGH);
  75.     //digitalWrite(12, LOW);
  76.     //digitalWrite(11, LOW);
  77.     // read new packet data
  78.     uint8_t len = readPacket(&ble, BLE_TIMEOUT);
  79.      if (len == 0) return;
  80.  
  81.  
  82.  
  83.     //if no accelerometer, use control pad
  84.     if( !modeToggle ) buttonMode();
  85.    
  86. }
  87.  
  88.  
  89.  
  90. bool isMoving = false;
  91.  
  92.  
  93. bool buttonMode(){ // potentially change to void function
  94.  
  95.   static unsigned long lastPress = 0;
  96.  
  97.  
  98.  // Buttons
  99.   if (packetbuffer[1] == 'B') {
  100.  
  101.     uint8_t buttnum = packetbuffer[2] - '0';
  102.     boolean pressed = packetbuffer[3] - '0';
  103.  
  104.   Serial.println(buttnum);
  105.   Serial.println(pressed);
  106.   //Serial.println(isMoving);
  107.     if (pressed) {
  108.       isMoving = true;
  109.      
  110.       if(buttnum == 7){              // "1" hard pivot LEFT button
  111.     digitalWrite(frontLeftMotor, LOW);
  112.     digitalWrite(backRightMotor, HIGH);
  113.     digitalWrite(backLeftMotor, HIGH);
  114.     digitalWrite(frontRightMotor, LOW);
  115.       }
  116.       if(buttnum == 8){             // "2" hard pivot RIGHT button
  117.     digitalWrite(frontLeftMotor, HIGH);
  118.     digitalWrite(backRightMotor, LOW);
  119.     digitalWrite(backLeftMotor, LOW);
  120.     digitalWrite(frontRightMotor, HIGH);    
  121.       }
  122.       if(buttnum == 5){               // "5" both motors backwards
  123.     digitalWrite(frontLeftMotor, HIGH);
  124.     digitalWrite(backRightMotor, HIGH);
  125.     digitalWrite(backLeftMotor, LOW);
  126.     digitalWrite(frontRightMotor, LOW);
  127.       }
  128.       if(buttnum == 6){               // "6" both motors forwards
  129.     digitalWrite(frontLeftMotor, LOW);
  130.     digitalWrite(backRightMotor, LOW);    
  131.     digitalWrite(backLeftMotor, HIGH);
  132.     digitalWrite(frontRightMotor, HIGH);
  133.       }
  134. //
  135.       lastPress = millis();
  136.    
  137.     }
  138.     if(!pressed) {
  139.     isMoving = false;
  140.     digitalWrite(frontLeftMotor, LOW);
  141.     digitalWrite(backRightMotor, LOW);
  142.     digitalWrite(backLeftMotor, LOW);
  143.     digitalWrite(frontRightMotor, LOW);
  144.       }
  145.     return true;
  146.   }
  147.   return false;
  148.  
  149. }
  150.  
  151. void BLEsetup(){
  152.   Serial.print(F("Initialising the Bluefruit LE module: "));
  153.  
  154.   if ( !ble.begin(VERBOSE_MODE) )
  155.   {
  156.     error(F("Couldn't find Bluefruit, make sure it's in Command mode & check wiring?"));
  157.   }
  158.   Serial.println( F("OK!") );
  159.  
  160.   /* Perform a factory reset to make sure everything is in a known state */
  161.   Serial.println(F("Performing a factory reset: "));
  162.   if (! ble.factoryReset() ){
  163.        error(F("Couldn't factory reset"));
  164.   }
  165.  
  166.   //Convert the name change command to a char array
  167.   BROADCAST_CMD.toCharArray(buf, 60);
  168.  
  169.   //Change the broadcast device name here!
  170.   if(ble.sendCommandCheckOK(buf)){
  171.     Serial.println("name changed");
  172.   }
  173.   delay(250);
  174.  
  175.   //reset to take effect
  176.   if(ble.sendCommandCheckOK("ATZ")){
  177.     Serial.println("resetting");
  178.   }
  179.   delay(250);
  180.  
  181.   //Confirm name change
  182.   ble.sendCommandCheckOK("AT+GAPDEVNAME");
  183.  
  184.   /* Disable command echo from Bluefruit */
  185.   ble.echo(true);
  186.  
  187.   Serial.println("Requesting Bluefruit info:");
  188.   /* Print Bluefruit information */
  189.   ble.info();
  190.  
  191.   ble.verbose(false);  // debug info is a little annoying after this point!
  192.  
  193.   /* Wait for connection */
  194.   while (! ble.isConnected()) {
  195.       delay(500);
  196.   }
  197.   // Set Bluefruit to DATA mode
  198.   ble.setMode(BLUEFRUIT_MODE_DATA);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement