VirtualWire receiver never gets its message

I have a radio receiver wired up with an LED array (32 x 15) and am trying to define the animation that is displayed on the LED array based off of what the radio receiver...receives. The problem right now though is that the VirtualWire library is never receiving a message in the main program. But, in the test program (same wiring and everything) I receive all the messages just fine.

The test program that receives everything and which works correctly is here:

#include <VirtualWire.h>

//Pin to receive data from the radio receiver
const byte receivePin = 4;

void setup() {
  //set pins to output because they are addressed in the main loop
  Serial.begin(9600);
  Serial.println("*");
  
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_set_rx_pin(receivePin);
  vw_setup(2000);// Bits per sec
  vw_rx_start();// Start the receiver PLL running
}

void loop() {
  byte buf[VW_MAX_MESSAGE_LEN];
  byte buflen = VW_MAX_MESSAGE_LEN;
  
  if (vw_get_message(buf, &buflen)) { // Non-blocking
    Serial.println("RECEIVE");
    for (int i = 0; i < buflen; i++) {
      char asciiChar = buf[i];
      Serial.print(asciiChar);
    }
    
    Serial.println();
  }
}

The program that I have that is not receiving anything is attached (slightly too big to post, 672 lines) and the function that is supposed to read the message(s) starts on line 444. In this program you see that I Serial.println() and I always get the message "checking for a message" in my console but I never get "got a message" in the attached program.

Could this be a memory issue? I did try to comment out the largest of my boolean arrays but it didn't help at all. The animations play perfectly fine in the LED board always, but I just can't receive a radio message.

Backpack.ino (64.4 KB)

Instead of doing posts with a fraction of the program, attach the whole file. At the bottom when you write your post, "Additional options" ...
Then we can see the problem.

mark7w:
Instead of doing posts with a fraction of the program, attach the whole file. At the bottom when you write your post, "Additional options" ...
Then we can see the problem.

Im not sure what you mean, I did attach the whole program as an additional file and the code within the post is just the example code that I used to prove that everything is wired up and functioning correctly.

Sorry to reply to myself but after a bit more screwing around I found something strange. In an old post that I had here I had the same issue.

Basically, when the program that I attached in my original post has the variable of "currentFrameKey" set to 0, I can not receive any input from the radio receiver. If the currentFrameKey variable is set to anything else, I get my data in.

Why / how could this be?

Im not sure what you mean, I did attach the whole program as an additional file and the code within the post is just the example code that I used to prove that everything is wired up and functioning correctly.

didn't see that part. it was late. sorry.

  1. Booleans are a whole byte large. Since you need such large arrays of them, consider packing them into bytes to save a significant amount of space.
  2. This is how you should approach this problem: Start with something that works. Add in piece by piece the rest of your program until it breaks. Bingo! You know what the issue is now.

Yes:

  1. Booleans are a whole byte large. Since you need such large arrays of them, consider packing them into bytes to save a significant amount of space.

Oh, I thought that booleans were smaller in memory size than bytes but apparently not. How many bits does a boolean take up? Is there any way to use only 1 bit of memory to store a value of either 0 or 1? I thought that boolean would be that but alas, nope. The smallest I can find is byte but that still takes 8 bits of memory.

Edit:
I found the problem line in the code - in the function writeRow(int row) if I comment out the line digitalWrite(latchPin, HIGH); then I receive all radio messages just perfectly. Obviously this is a problem since if I don't turn the latchPin high the shift registers never turn off/on. How / why could this affect it?

Just a heads up, you cant use any PWM on pins 9 or 10, because the virtualwire library uses that timer. If you have anything on 9 or 10 it will give you weird results.

HazardsMind:
Just a heads up, you cant use any PWM on pins 9 or 10, because the virtualwire library uses that timer. If you have anything on 9 or 10 it will give you weird results.

Well I wasn't using the PWM abilities but I did move the clear pin and clock pins from 9 and 10 to pins 2 and 3. Sadly, this didn't change anything, I still have to comment out digitalWrite(latchPin, HIGH); to receive any radio messages. The latchPin is set to pin 11 on my Mirco.

digitalWrite(latchPin, HIGH); ?
I was under the impression that you were using the code you posted above. What code are you using now?

HazardsMind:
digitalWrite(latchPin, HIGH); ?
I was under the impression that you were using the code you posted above. What code are you using now?

The code posted in my first post is just proof that all my stuff is wired up correctly. The code that isn't working properly is the code that is attached as a file to my first post.

I see that you have two digitalWrite(latchPin, HIGH); Which one are you commenting out?
Im also wondering if it is because of your AcceleroMMA7361.h library, did you copy those pin arrangements from an example sketch? They might already be set to output as PWM, from within the library.

In the code you posted before, try to insert the library along with the same pins for clock, latch, data, and clear, and see if your able to receive data. Is that also the same library for your animation stuff?

One last thing, you can make a seperate tab and store your program mem arrays in it, so that your code does not look so cluttered. Just make sure to save it as a .h file and remember to #include it. If you want an example of what Im talking about, download the Gameduino library, and open up the Ball sketch.

Ok, I have tried to make the most basic program that has this error which is here:

#include <VirtualWire.h>

//Pin to clear the register
const byte clearPin = 2;
//Pin connected to clock pin (SH_CP) of 74HC595
const byte clockPin = 3;
//Pin connected to latch pin (ST_CP) of 74HC595
const byte latchPin = 11;
//Pin connected to Data in (DS) of 74HC595
const byte dataPin = 12;

//Pin to receive data from the radio receiver
const byte receivePin = 4;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(clearPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("*");
  
  // Always start by sentting SRCLR high
  digitalWrite(clearPin, HIGH);
  
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_set_rx_pin(receivePin);
  vw_setup(2000);// Bits per sec
  vw_rx_start();// Start the receiver PLL running
}

void loop() {
  getMessage();
  writeRow(5);
  delay(500);
  
  getMessage();
  writeRow(6);
  delay(500);
}

void getMessage() {
  byte buf[VW_MAX_MESSAGE_LEN];
  byte buflen = VW_MAX_MESSAGE_LEN;
  
  if (vw_get_message(buf, &buflen)) { // Non-blocking
    Serial.print("RECEIVE: ");
    for (int i = 0; i < buflen; i++) {
      char asciiChar = buf[i];
      Serial.print(asciiChar);
    }
    
    Serial.println();
  }
}

//used only if we are blinking row by row only
void writeRow(int row) {
  Serial.print("write row: ");
  Serial.println(row);
  byte bitsToSend0 = 0;
  byte bitsToSend1 = 0;
  byte bitsToSend2 = 0;
  byte bitsToSend3 = 0;
  byte bitsToSend4 = 0;
  byte bitsToSend5 = 0;
  
  if (row <= 7) {
    bitWrite(bitsToSend1, row, HIGH);
  }
  else {
    bitWrite(bitsToSend0, row - 8, HIGH);
  }
  
  for (int i = 0; i < 32; i++) {
    if (i <= 7) {
      bitWrite(bitsToSend5, i, HIGH);
    }
    else if (i <= 15) {
      bitWrite(bitsToSend4, i - 8, HIGH);
    }
    else if (i <= 23) {
      bitWrite(bitsToSend3, i - 16, HIGH);
    }
    else {
      bitWrite(bitsToSend2, i - 24, HIGH);
    }
  }
  
  digitalWrite(latchPin, LOW);
  
  // shift the bits out
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend0);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend1);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend2);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend3);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend4);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend5);
  
  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
}

The output is as follows:

write row: 6
write row: 5
write row: 6
write row: 5
write row: 6
write row: 5
RECEIVE: vBHrz0ZqM70
write row: 6
RECEIVE: vBHrz0ZqM70
write row: 5
write row: 6
write row: 5
write row: 6
write row: 5
RECEIVE: vBHrz0ZqM70
write row: 6
RECEIVE: vBHrz0ZqM70
write row: 5
write row: 6
write row: 5
write row: 6
write row: 5
write row: 6
RECEIVE: vBHrz0ZqM70
write row: 5
RECEIVE: vBHrz0ZqM70
write row: 6
write row: 5
write row: 6
write row: 5
write row: 6
RECEIVE: vBHrz0ZqM70
write row: 5
RECEIVE: vBHrz0ZqM70
write row: 6
write row: 5
write row: 6

So you can see that sometimes, it just doesn't work properly but with some pattern to it.

Also, with this sketch, the lights are not lighting up very properly. The shift register that I am sending bit "bitsToSend2" is always out of sync in a way, not lighting up until it switches for only a brief instant. I have a capacitor between the ground and VCC on this register and if I hold the capacitor between my fingers then it works just fine. I am not sure if that has anything to do with it but it is still quite strange.

Any ideas?

bump can anyone see why I am only receiving radio messages some of the time? I should get the "RECEIVE: ..." message between each blink (the transmitter is transmitting quite quickly) but it only comes every so many seconds as shown.

You might need to add vw_wait_rx(); Maybe it will help, maybe not.

Well, while adding vw_wait_rx(); certainly does solve the issue of not receiving the message, it slows the program down because it is blocking. This has the sad affect of causing my LEDs to light up too slowly and show a very noticeable blinking.

Is there any way to multithread so I have 1 thread that is always checking for a new message and that thread can manipulate a few variables in the main thread that is running the LED blinking functions?

Ok so why not send the data faster. I think you have it set for 2000? make it 4000, on both sides.

HazardsMind:
Ok so why not send the data faster. I think you have it set for 2000? make it 4000, on both sides.

Sadly that doesn't work. If I set it to 4000 this still gives a very obvious blinking and since I have to use vw_wait_rx(). Oddly, even with 4000 and vw_wait_rx() I still don't get messages properly (although still sometimes it comes through) when my variable currentFrameKey is not 0.

If I don't use vw_wait_rx(), I don't get anything.

I dont really know what else to tell you, maybe the receiving code needs to be in the loop and not in another function to work right.

Ok, I think I found the place in the code that is killing the radio receiving. Attached is the whole sketch.

In the method "boolean getBlinkState(int a, int b)" I am only returning the cautionSignal array. When it is like this, the radio receiver always works just fine, no worries.

But, if I change the array to be returned to anything else, for example to stoppingSignal, the radio receiver code never works! So, the problem is that when I run the code for the first time, the array will return cautionSignal which is fine but as soon as I change the array to be returned, it becomes stuck.

So, is there anything that PROGMEM or pgm_read_byte() would do that would keep the radio receiver from functioning properly? I don't understand why just reading one of the other arrays would stop VirtualWire from working.

Arrays.h (22.3 KB)

Backpack.ino (15.2 KB)