For my project: Arduino1 is the master sender, and Arduino2 houses the Wave shield as the slave receiver. Everything works fine with Arduino1 sending to Arduino2, as long as what Arduino2 is receiving only commands it to control an LED. For example, if k=2, LED1 is lit. However, once what is received commands a sound to be played on the wave shield, the information isn't received.
I've simplified the master sender code for testing, and it works with varying values of k, until it reaches 16 for the receiver code. I've figured out that it is definitely related to the wave shield, because I've varied values of k and the contents in each if loop of the receiver.
Below are the codes. Any suggestions, tips or tricks?
This is the master sender code:
- Code: Select all
#include <Wire.h>
#include <AFMotor.h> //takes from library
byte k = 0;
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Serial.println("Master");
randomSeed(analogRead(0)); //THIS HAS TO BE CHANGED to open input pin
}
void loop()
{
//send information to Arduino 2 to play certain music file and light array of LEDs
k=16;
Serial.println("ACHIEVEMENT");
Wire.beginTransmission(4); // transmit to device #4
Wire.write(k); // sends one byte
Wire.endTransmission(); // stop transmitting
delay(2000); //time for motors to stop
}
Slave Receiver code:
- Code: Select all
#include <Wire.h>
//---------------WAVE SHEILD-------------------------
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
//----------------LEDS Initialize-----------------------
int LED7 = A0; //Scanning LED
int LED8 = A1; //Scanning LED
int LED9 = A2; //Scanning LED
int LED10 = A3; //Scanning LED
int LED1 = 0; //Target LED
int LED2 = 1; //Target LED
int LED3 = 6; //Target LED
int LED4 = 7; //Target LED
int LED5 = 8; //Target LED
int LED6 = 9; //Target LED
//------------------WAVE SHEILD debugging--------
int freeRam(void) { //says what free ram on SD card is
extern int __bss_end;
extern int *__brkval;
int free_memory;
if((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__bss_end);
}
else {
free_memory = ((int)&free_memory) - ((int)__brkval);
}
return free_memory;
}
void sdErrorCheck(void){ //reports on errors
if (!card.errorCode()) return;
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
//------------------SET UP--------------------------------
void setup() {
byte i;
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
Serial.println("Slave");
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
//--//---WAVE SHEILD----
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
Serial.println(freeRam()); // if this is under 150 bytes it may spell trouble!
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1<<TOIE2;
}
SIGNAL(TIMER2_OVF_vect) {
check_switches();
}
void check_switches()
{
}
//-------------------------------------------------------------
void loop()
{
delay(100);
}
//-------------------------------------------------------------
//slavereceiver: For communication with Arduino 1
void receiveEvent(int howMany){
Serial.println("working receive");
int k = Wire.read(); // receive byte as an integer
Serial.println(k); // print the integer
//-------------------------------------------------------------
//Random: Randomly picks a target based on 6 lEDs
if (k==1){
Serial.println("L1");
digitalWrite(LED1, HIGH); // set the LED on
}
if (k==2){
Serial.println("L2");
digitalWrite(LED2, HIGH); // set the LED on
}
if (k==3){
Serial.println("L3");
digitalWrite(LED3, HIGH); // set the LED on
}
if (k==4){
Serial.println("L4");
digitalWrite(LED4, HIGH); // set the LED on
}
if (k==5){
Serial.println("L5");
digitalWrite(LED5, HIGH); // set the LED on
}
if (k==6){
Serial.println("L6");
digitalWrite(LED6, HIGH); // set the LED on
}
//--------------ACHIEVEMENT-------------------------------------
if (k==15){
digitalWrite(LED1, LOW); // set the LED off
digitalWrite(LED2, LOW); // set the LED off
digitalWrite(LED3, LOW); // set the LED off
digitalWrite(LED4, LOW); // set the LED off
digitalWrite(LED5, LOW); // set the LED off
digitalWrite(LED6, LOW); // set the LED off
digitalWrite(LED7, LOW); // set the LED off
digitalWrite(LED8, LOW); // set the LED off
digitalWrite(LED9, LOW); // set the LED off
digitalWrite(LED10, LOW); // set the LED off
//light array?
delay (3000);
}
//------------SCANNING----------------------------------
if (k == 7) {
Serial.println("Scan1");
digitalWrite(LED7, HIGH); // set the LED on
}
if (k == 8) {
digitalWrite(LED7, LOW); // set the LED off
}
if (k == 9) {
Serial.println("Scan2");
digitalWrite(LED8, HIGH); // set the LED on
}
if (k == 10) {
digitalWrite(LED8, LOW); // set the LED off
}
if (k == 11) {
Serial.println("Scan3");
digitalWrite(LED9, HIGH); // set the LED on
}
if (k == 12) {
digitalWrite(LED9, LOW); // set the LED off
}
if (k == 13) {
Serial.println("Scan4");
digitalWrite(LED10, HIGH); // set the LED on
}
if (k == 14) {
digitalWrite(LED10, LOW); // set the LED off
}
//---------------------SOUND-----------------------
if (k==16){
playcomplete("WELCOME.WAV");
digitalWrite(LED1, HIGH); // set the LED on
digitalWrite(LED3, HIGH);
digitalWrite(LED5, HIGH);
delay (500);
digitalWrite(LED1, LOW); // set the LED off
digitalWrite(LED3, LOW); // set the LED off
digitalWrite(LED5, LOW); // set the LED off
digitalWrite(LED2, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED6, HIGH);
delay (500);
digitalWrite(LED2, LOW); // set the LED off
digitalWrite(LED4, LOW); // set the LED off
digitalWrite(LED6, LOW); // set the LED off
digitalWrite(LED1, HIGH); // set the LED on
digitalWrite(LED3, HIGH);
digitalWrite(LED5, HIGH);
delay (500);
digitalWrite(LED1, LOW); // set the LED off
digitalWrite(LED3, LOW); // set the LED off
digitalWrite(LED5, LOW); // set the LED off
digitalWrite(LED2, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED6, HIGH);
delay (500);
digitalWrite(LED1, LOW); // set the LED off
digitalWrite(LED2, LOW); // set the LED off
digitalWrite(LED3, LOW); // set the LED off
digitalWrite(LED4, LOW); // set the LED off
digitalWrite(LED5, LOW); // set the LED off
digitalWrite(LED6, LOW); // set the LED off
digitalWrite(LED7, HIGH); // set the LED off
digitalWrite(LED8, HIGH); // set the LED off
digitalWrite(LED9, HIGH); // set the LED off
digitalWrite(LED10, HIGH); // set the LED off
delay (2000);
digitalWrite(LED7, LOW); // set the LED off
digitalWrite(LED8, LOW); // set the LED off
digitalWrite(LED9, LOW); // set the LED off
digitalWrite(LED10, LOW); // set the LED off
playcomplete("WELCOME.WAV");
}
} // end command receiving loop
//---------------WAVE SHEILD COMMANDS-----------------------------
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
// do nothing while its playing
}
// now its done playing
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
return;
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
return;
}
// ok time to play! start playback
wave.play();
}
//-------------------------------------------------------------
Thanks in advance for the help!
K

