// This version uses call-backs on the event and RX so there's no data handling in the main loop!
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 8
#define ADAFRUITBLE_RDY 2 //interrupt pin. on Uno that's #2 or #3
#define ADAFRUITBLE_RST 7
Adafruit_BLE_UART uart = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
boolean messageReceived = false;
uint8_t message[20];
// initializing my components
int enA = 10; //LEFT motor enable pin
int in1 = 6; //L input
int in2 = 5; //L input
int in3 = 3; //R input
int in4 = 4; //R input
int enB = 9; //RIGHT motor enable pin
/**************************************************************************/
/*!
This function is called whenever select ACI events happen
*/
/**************************************************************************/
void aciCallback(aci_evt_opcode_t event)
{
switch(event)
{
case ACI_EVT_DEVICE_STARTED:
Serial.println(F("Advertising started"));
break;
case ACI_EVT_CONNECTED:
Serial.println(F("Connected!"));
break;
case ACI_EVT_DISCONNECTED:
Serial.println(F("Disconnected or advertising timed out"));
break;
default:
break;
}
}
/**************************************************************************/
/*!
This function is called whenever data arrives on the RX channel
*/
/**************************************************************************/
void rxCallback(uint8_t *buffer, uint8_t len)
{
Serial.print(F("Received "));
Serial.print(len);
Serial.println(F(" bytes: "));
// constantly check if these are happening
switch(buffer[2]) {
case 53:
Serial.println ( "up");
if(buffer[3] == 49){
digitalWrite(in1, HIGH); //left
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); // sets motor to forward.
digitalWrite(in4, LOW);
analogWrite(enA, 200);
analogWrite(enB, 255); // write enable pin to highest speed (0-255)
}
if(buffer[3] == 48) { //button released, turn all motors off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
}
break; // will stop the code so that the following code in the loop does not run
case 54:
Serial.println ( "down");
if(buffer[3] == 49){
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW); // sets motor to backwards.
digitalWrite(in4, HIGH);
analogWrite(enA, 220);
analogWrite(enB, 255); // write enable pin to highest speed (0-255)
}
if(buffer[3] == 48) { //button released, turn all motors off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
}
break;
case 55:
Serial.println ( "left");
if(buffer[3] == 49){
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, 255);
analogWrite(enB, 255); // write enable pin to highest speed (0-255)
}
if(buffer[3] == 48) { //button released, turn all motors off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
}
break;
case 56:
Serial.println ( "right");
if(buffer[3] == 49){
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, 255);
analogWrite(enB, 255); // write enable pin to highest speed (0-255)
}
if(buffer[3] == 48) { //button released, turn all motors off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
}
break;
}
/* Echo the same data back! */
uart.write(buffer, len);
}
/**************************************************************************/
/*!
Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void){
Serial.begin(9600);
while(!Serial); // Leonardo/Micro should wait for serial init
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Callback Echo demo"));
uart.setRXcallback(rxCallback);
uart.setACIcallback(aciCallback);
uart.setDeviceName("MYCAR"); /* 7 characters max! */
uart.begin();
// setup the pinmodes for the L298 Motor controller
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT); // all three pins are output pins
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
/**************************************************************************/
/*!
Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
void loop()
{
uart.pollACI();
}
Comments