Hello all. I really want this present to do what I envisioned, it's crunch time, and I'm getting stressed out.
Goal:
Detect if any of 8 contacts are open, indicating an open box. Send the value of which box is open over an NRF24L01 transceiver.
Receive that value at the receiver end, and use that to play an audio file using the TMRpcm library.
What works:
Detecting which contact is open
What doesn't work:
Sending or receiving the value wirelessly. I don't know where the problem is (TX or RX), but I'm not getting the data at the receiver.
Initializing the SD Card for the TMRpcm library. (Prints "SD Fail" when initializing)
The SD card is a 16gb (SDHC) formatted to FAT32.
Anyone see any glaring issues in my code?
Transmitter (Present):
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte Box[] = {2, 3, 4, 5, 6, 7, 8, A0}; //Pin 2 = Bottom, Pin A0 = Lid, Pin 3,4, 5, 6, 9, and 10 correspond to Boxes 1-6
bool CurrentReading[8];
bool PreviousReading[8];
int Signal;
RF24 radio(9, 10); // CE, CSN
const byte address[6] = {"00001"};
/*
Pin 9 - CE
Pin 10 - CSN
Pin 11 - MOSI
Pin 12 - MISO
Pin 13 - SCK
*/
void setup(){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address); // 00001
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
for(int i=0; i<=7; i++){
pinMode(Box[i], INPUT_PULLUP);
}
}
void loop(){
for(int i=0; i<=7; i++){
CurrentReading[i] = digitalRead(Box[i]);
if(CurrentReading[i] == HIGH && PreviousReading[i] == LOW){
Signal = i+1;
}
PreviousReading[i] = CurrentReading[i];
}
Serial.print("Signal: ");
Serial.println(Signal);
radio.write(&Signal, sizeof(Signal));
Serial.println("Data Sent");
delay(6);
Signal = 0;
}
Receiver (Speaker):
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define SD_ChipSelectPin 4
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
int Signal = 0; //Starts with the Signal variable set to 0
int LastPlayed;
TMRpcm tmrpcm;
#define DISABLE_SPEAKER2 //Disables default second speaker pin for compatibility with other libs. (pin 10 on Uno)
/*
Arduino - Devices
Pin 4 - CS (SD)
Pin 9 - CE (NRF24L01)
Pin 10 - CSN (NRF24L01)
Pin 11 - MOSI (SD + NRF24L01)
Pin 12 - MISO (SD + NRF24L01)
Pin 13 - SCK (SD + NRF24L01)
Pin 5 - Speaker Positive / Mono Headphone Positive
*/
void setup(){
tmrpcm.speakerPin = 5;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
tmrpcm.setVolume(6);
}
void loop(){
if (radio.available()) {
radio.read(&Signal, sizeof(Signal)); //Read data from NRF24L01
}
Serial.print("Signal = ");
Serial.println(Signal);
if(Signal != 0){
if(Signal != LastPlayed){
switch(Signal){
case(1):
tmrpcm.play("Red Pill Blue Pill.wav"); //Play red pill blue pill
break;
case(2):
tmrpcm.play("Nyan Cat.wav"); //
break;
case(3):
tmrpcm.play("Derp Song.wav"); //
break;
case(4):
tmrpcm.play("Harlem Shake.wav"); //
break;
case(5):
tmrpcm.play("Rick Roll.wav"); //
break;
case(6):
tmrpcm.play("Troll Song.wav"); //
break;
case(7):
tmrpcm.play("Russian Song.wav"); //
break;
case(8):
tmrpcm.play("Far Away.wav"); //Play "Far Away"
break;
}
}
Signal = LastPlayed;
}
delay(4);
}