RF communication master and slave

I have one arduino slave and one master,RF communication. The slave have to report to the master information like system errors (temperature and others).I have to create a rotine to send erros if they exists but one at a time. I try this:

for(i=2;i>0;i--){
   if(i==1){
      if(temperature>50){
      send message;
    }
}
 if(i==2){
   if(volts>12){
   send message;
 }
}
}

Dont work because its sends simultaneously.Any idea?

I have an idea:

Why not post some real code?

I dont understand.This is my real code or a part of it. I want to send messages but not at the same time.I try with a for rotine that i post.Any idea?

What is i? What is temperature? volts? "send message"? That latter isn't any code that I recognise...

Ok,sorry. send message is:

vw_send((uint8_t *)message, strlen(message));

This this a testing code.I want to make a rotine to send multiple messages but not at the same time.Temperature and volts are errors to send,if temperature is more than 50 send a message.If have volts and temperature errors have to send two messages but i want not at the same time.

I don’t understand why you say your code sends the messages simultaneously. As far as I can see your code first sends the message about volts followed by the message about temperature. The problem, I suspect, is that it sends then very quickly one after the other.

Perhaps that will suggest some new directions for your thoughts …

Because you haven’t really told us what you want to achieve.

…R

Thank you for the reply.Its sends very quickly,i want to send less quickly. Slave reports errors to the master,like temperature more than 50 or volts.My problem is to send two or more errors.I try for routine but is very quick and i dont know if is this the best way to deal with this.

If you read about "blink without delay" it should help you to figure out a solution.

...R

The program blink without delay use millis() that have the value the program start.If this value overflow is not a problem?

Overflow is not a problem as long as the values are held and compared correctly:

Use [u]unsigned[/u] types.

unsigned long startTime;

Use [u]subtraction[/u] to determine whether an interval has elapsed.

if(millis() - startTime >= interval)
{
    ...

I have this code:

for(i=2;i>0;i--){
   if(i==1){
      if(temperature>50){
      vw_send((uint8_t *)message, strlen(message));
    }
}
 if(i==2){
   if(volts>12){
   vw_send((uint8_t *)message, strlen(message));
 }
}
}

I try this to send one by one messages and not simultaneously but like this is very quick.I read the "blink without delay" and try to put on my code to send messages less quickly but i dont know who.Anyone help me or make an example for better understand?

I wouldn't use a "for" loop (as you have done) to separate the two actions of sending volts and sending temperature.

I would have a short function that sends the volts and another that sends the temperature.

In my "loop()" I would call each of those functions in turn.

If I needed a time space between the two functions I would create variables (in setup()) to record the interval between the sends, the time of the last send and a record of which thing was sent the last time. Then the code in my loop would check if the interval had elapsed, would then check which thing had been sent the last time and send the other thing.

I find it very useful if I have a difficult piece of logic to write it as a series of steps in a column down the left-hand edge of a page and then fill in successive columns on the page with the subsequent runs of the loop.

...R

void setup(){
long intervalbetween = 1000;
long previousMillis = 0; 
}

void loop(){
unsigned long currentMillis = millis();
temperature function();
if(currentMillis - previousMillis > intervalbetween){
previousMillis = currentMillis;
volts function();
 }
}

Can someone help me with this?I know like this wrong.

void setup(){
  long intervalbetween = 1000;
  long previousMillis = 0; 
}

Those are local variables. They are specific to the setup() function. They can only be used from within the setup() function. If they have the same name as a global variable, then they will override the global variable while in the setup() function. They should either be defined as global, or local to the loop() function and static. Also, they should be unsigned :)

const unsigned long untervalBetween = 1000;  // This never changes, so let's have it as a const to save RAM
unsigned long previousMillis = 0;  // This could be "static unsigned long ..." inside the loop() function instead

void setup() {
  // Nothing needed here
}

void loop() {

  // Anything here will run continuously.

  if (millis() - previousMillis >= intervalBetween) {
    previousMillis = millis();

    // Anything here will run once per second.

  }
}

However, what I think Robin2 was getting at is more like:

unsigned long lastSentTemp = 0;
unsigned long lastSentVolts = 0;

void setup() {
}

void loop() {
  unsigned long now = millis();

  if (temperatureHasChanged()) { // you would have to decide how to do that
    if (now - lastSentTemp >= 1000) {
      lastSentTemp = now;
      sendTemperature();
    }
  }

  if (voltageHasChanged()) { // you would have to decide how to do that
    if (now - lastSentVolts >= 1000) {
      lastSentVolts = now;
      sendVoltage();
    }
  }
}

boolean temperatureHasChanged() {
  // I don't know what you need in here, but
  return true;
  // if the temperature has changed, or
  return false;
  // if it hasn't.
}

boolean voltageHasChanged() {
  // Likewise for the voltage - return true or false
  // depending on if it has changed since the last time this function was called.
}

void sendTemperature() {
  // Do the sending of the temperature information
}

void sendVoltage() {
  // Send the voltage information
}

What that (might) do is see if the temperature or voltage has changed, then only send them if it has - and no more that once per second for each.

But if temperatureHasChanged() and voltageHasChanged() are true.Like this they send at same time or iam wrong?

vasr: But if temperatureHasChanged() and voltageHasChanged() are true.Like this they send at same time or iam wrong?

Nothing ever happens at the same time. If the temperature and the voltage change together, and they are both > 1s since they last sent, then they will send one after the other in quick succession.

You could have just 1 timer instead of 2, so it will always be at least 1 second between sendings for anything.

Any other idea to send messages?

vasr: Any other idea to send messages?

Carrier pigeon?

If this is a problem put in some code to prevent it happening based on the advice you have already been given.

…R

vasr:
But if temperatureHasChanged() and voltageHasChanged() are true.Like this they send at same time or iam wrong?