Ok, I'm having a hard time figuring out how to get my nRF8001 BLE module and nRF24l01+ transceiver working on the same Arduino Uno. I would like to have the BLE take over when a button/switch is ON and the transceiver take over when a different button/switch is ON (and the BLE button/switch is OFF). I've had some success, but it seems to be hit or miss. Therefore, I believe I'm not selecting the chips correctly. I have attempted to read about CS with multiple devices, but its over my head. I would really appreciate some help with the code below, so I can use both forms of communication. Any suggestions, edits, tutorials, explantations, etc. would be greatly appreciated! Below is my sketch and attached is horrible schematic drawling. If anyone knows a good free Arduino schematic maker, that would be awesome.
//*Arduino Uno Master (Receiver)*
//*********( THE START)***********
/*-----( Import needed libraries )-----*/
#include <SPI.h> // Comes with Arduino IDE
#include "RF24.h" //Transceiver Library
#include "Adafruit_BLE_UART.h" //Bluetooth Library
#include <OBD2UART.h> //OBD-II Adapter Library
/*-----( Declare Constants and Pin Numbers )-----*/
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST 9
/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
RF24 myRadio (7, 8); // "myRadio" is the identifier you will use in following methods
Adafruit_BLE_UART uart = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
// Data that will be received from the transmitter
int msg[1];
int LED1 = 4;
float in, out;
COBD obd;
int RPMvalue;
int RPM_button = 5;
int RPM_value = 0;
int backup_button = 6;
int backup_value = 0;
/**************************************************************************/
/*!
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;
}
}
void setup() /****** SETUP: RUNS ONCE ******/
{
// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
Serial.begin(9600);
while (!Serial);
delay(1000);
Serial.println(F("LETS DO THIS"));
//*******Transeiver********
myRadio.begin(); // Start up the physical nRF24L01 Radio
myRadio.setChannel(108); // Above most Wifi Channels
myRadio.setPALevel(RF24_PA_MIN);
myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
myRadio.startListening();
//*******LED/Buzzer********
pinMode(LED1, OUTPUT);
pinMode(RPM_button, INPUT);
pinMode(backup_button, INPUT);
//*******Bluetooth*******
uart.setACIcallback(aciCallback);
uart.begin();
//*******OBD-II Adapter********
obd.begin();
while (!obd.init());
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//*******RF24 Transeiver********
backup_value = digitalRead(backup_button);
if (backup_value == HIGH) {
Serial.print(F("RF ON"));
if ( myRadio.available()) // Check for incoming data from transmitter
{
while (myRadio.available()) // While there is data ready
{
myRadio.read(msg, 1);
Serial.println(msg[0]);
if (msg[0] == 110)
{
for (in = 0; in < 6.283; in = in + 0.003) {
out = sin(in) * 127.5 + 127.5;
analogWrite(LED1, out);
}
}
else if (msg[0] == 100)
{
for (in = 0; in < 6.283; in = in + 0.011) {
out = sin(in) * 127.5 + 127.5;
analogWrite(LED1, out);
}
}
else if (msg[0] == 111)
{
for (in = 0; in < 6.283; in = in + 0.0009) {
out = sin(in) * 127.5 + 127.5;
analogWrite(LED1, out);
}
}
else
{
digitalWrite(LED1, LOW);
}
delay(100);
}
}
else
{
Serial.print(F("No radio available"));
}
}
//*******OBD-II Adapter********
RPM_value = digitalRead(RPM_button);
if (RPM_value == HIGH)
{
uart.pollACI();
if (obd.readPID(PID_RPM, RPMvalue))
{
String s = String(RPMvalue);
uint8_t sendbuffer[20];
s.getBytes(sendbuffer, 20);
char sendbuffersize = min(20, s.length());
uart.print("RPM = ");
uart.write(sendbuffer, sendbuffersize);
}
}
} //End Loop
//*********( THE END )***********