it means the code sometimes will fail to execute ? Am I correct ?
During development, code will always sometimes fail to execute as we expect it to.
But, no, you are not correct. "Blocking" means the code will loop waiting until a message is received. This may or may not be a problem for you.
The great thing about the Arduino is that you can try things, and if they don't work, try something different.
The frequency of your questions leads me to suspect you're not trying the things you're asking about.
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
pinMode(13, OUTPUT);
}
void loop()
{
uint8_t count;
if(count==true)
{
count = false;
}
count = vw_wait_rx_max(2000);// Non-blocking
if (true)
{
digitalWrite(13, HIGH); // Flash a light to show received good message
delay(500);// Message with a good checksum received, dump it.
digitalWrite(13, LOW);
delay(500);
}
else if(false)
{
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
}
}
I write this code..I want it to be like when it does not receive message for 2s, then it will return false. If it receive message, then return true. Then it will only blink once then the whole system become false again. So what moddification I need to do ?
This is a really great thread. However, Wouldnt it be easier for all of us newbs if someone would just take the time to make a document or tutorial of this information (in plain terms)? This seems to be a hot topic. Not only here but also in other forums as well. I have learned a great deal just reading the questions and answers on this thread and like a good movie, I don't want it to end...LOL
Maybe a real world example would be appropriate. I can post my remote control code. It reads a keypad press and sends out 1 character.
On the receive side, it acts on the 1 character received.
Will be later tonight. The code is from the Virtualwire example, with the message sent being a 1 byte array
byte msg[1];
with the byte coming from the keypad.h library.
I may. However, I am still trying to understand it myself...LOL For now I will just have to keep bugging everyone here. You guys have great responses and are very responsive so thats not a bad thing.. at least not for me...
Here's my loop code for sendinng out keypad presses:
void loop()
{
// go read the keypad
char key = keypad.getKey(); // reading the keypad
if(key) // same as if(key != NO_KEY)- did something change?
{
msg[0]=key; // load the array with the key character
// msg[1]=NULL; // Rx side seems to work without this
digitalWrite(ledPin, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg)); // send the character out
// Serial.println(key); // for debugging only
vw_wait_tx(); // Wait until the whole message is gone
delay (50); // need some delay or seem to miss key presses
digitalWrite(ledPin, false); // turn off the flash of LED
} // end of if(key) & transmitting a character
// do other stuff...
} // end of void loop
and on the receive side
void loop(){
// look for wireless input or for unsolicited message from remote transmitter
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(ledPin, LOW); // Turn on LED to show working a message
switch ( buf[0] )
{ // switch on the first byte in the array for now
// caes '0':, case'1':, 2,3,4,5,6,7,8,9,A,B,C, thru case 'D':, case '*':, and case '#': sections follow for my 16 button keypad
}
}
}
I found this useful for understanding VW a little better. Just make sure that you match the vw_setup(2000) on the transmitter sketch and the receiver sketch if you download the code from that site because they are mismatched and will not work that way.
The constant "true" will always be true, so if you say if(true), whatever follows the experession will always be executed.
If you say if (false), then whatever follows will never be executed.
That's fundamental.
Oh, so it will be a different story if I assigned a variable to be 'true' or ;false' ?
Correct ?
The action, based on whether the variable contains true or false, will be the same. Which action is performed will be different IF you assign the value correctly and IF you write the test correctly.
True or false is different from HIGH or LOW right ?
Yes.
The if(variable == true) test is silly. If variable is true, the test evaluates to true == true, which is true. So, the initial test could simply have been if(variable). That is the convention for testing boolean variables. To do something when the variable contains false, you use if(!variable) (if not true).
For non-boolean variables, the 3rd and 4th examples are correct.