Go Down

Topic: if loop not triggering (Read 573 times) previous topic - next topic

blh64

Memory on an Arduino UNO is a very limited resource so for small positive numbers, like counts and Pin numbers, I like to use 'byte'.  In this case it probably won't make any difference since the compiler is smart enough to treat a small constant as a byte.
Actually, the compiler is clever enough to completely remove the const variable and replace it with the actual value so it really doesn't matter if you make your const variables byte or int.  They both will not consume any of the precious UNO memory as they will be encoded into the code.

Farticus

Thanks John.

I have been playing around with the sample of code you submitted
trying to stop the "IF" statement from triggering unless there is
a number greater than ("0") in the "interval" variable.

I had thought by adding a conditional (&&) to the mix,that the logic of the
"IF" statement would fail unless the (&&) condition was met.This does not seem to work and I dont understand my error.

In my ultimate code the variable ("interval") would only be supplied within the
"void loop" so I only want the ("if") statment actioned ONLY  if there is a value
in the ("interval") variable.

Forgive the waffling I hope you understand my meaning.

How can I get the ("IF") statement to do this??

thanks



Code: [Select]

const byte StationCount = 8;
const byte StationNumbers[StationCount] = {10, 11, 12, 13, 14, 15, 16, 17};
unsigned long MinuteIntervals[StationCount] =  {0, 0, 0, 0, 0, 0, 0, 0};
const unsigned long MillisecondsPerMinute = 60000UL;
unsigned long PreviousTime[StationCount];  // (globals default to zero)

void setup()
{
  Serial.begin(9600);
}

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

  for (byte station = 0; station < StationCount; station++)

  {
    unsigned long interval = MinuteIntervals[station] * MillisecondsPerMinute;
   

    if ((currentTime -  PreviousTime[station] >= interval) [u]&& (interval > 0)[/u]);
    {
      PreviousTime[station] += interval;
      Serial.print(" Station ");
      Serial.print(StationNumbers[station]);
      Serial.println(' ');
      // digitalWrite(StationPins[station], LOW);

      delay (1000);
    }
  }
}



cattledog

Code: [Select]
if ((currentTime -  PreviousTime[station] >= interval) && (interval > 0));

Loose the semicolon at the end of the line.

This is a common error, and Nick Gammon has enshrined it as Trap #13 in his Tips, Traps, and Style Guide.
http://www.gammon.com.au/tips

Farticus

Thanks Cattledog.

I make this mistake once a day and usually figure it out but this time I guess

i'll put it down to "brain freeze".

Tx again

Farticus



  Hi again. Could someone please point out my "idiot" mistake for the day?

I am trying to get the code to cease transmitting when the parameters are reached,

               "" if ((mn1 < 6) && ((datasent[0]) < 15)) {
                transMit();
                }  ""

but  even when parameters are exceeded it continues to transmit the "last Transmission

over and over.


I have had to truncate the code to fit the "limits" of 9000 characters.

Would appreciate any help.

Thanks

Code: [Select]

void transMit() {
  radio.stopListening();
  Serial.print("        MN1   IS  "); Serial.print(mn1);
  Serial.print("    DA[0]   IS  "); Serial.println(datasent[0]);

  if (radio.write( &datasent, sizeof(datasent) ))
    Serial.print ("SENT  ... "); Serial.print (datasent[0]); Serial.print ("/"); Serial.print (datasent[1]);
  Serial.print ("/"); Serial.print (datasent[2]); Serial.print ("/"); Serial.print (datasent[3]);
  Serial.print ("/"); Serial.print (datasent[4]);
  digitalWrite (6, HIGH); delay(500); digitalWrite(6, LOW);
  radio.startListening();
}
mn1 ++;
  if (mn1 > 300) {
    mn1 = 0;
  }
  if (mn1 < 6) {




    if (mn1 == 1)         {
      datasent[0] = 10;
      datasent[1] = 1;
      datasent[2] = 0;
    }

    if (mn1 == 2)             {
      datasent[0] = 11;
      datasent[1] = 2;
      datasent[2] = 0;
    }

    if  (mn1 == 3)          {
      datasent[0] = 12;
      datasent[1] = 3;
      datasent[2] = 0;
    }

    if (mn1 == 4)          {
      datasent[0] = 13;
      datasent[1] = 4;
      datasent[2] = 0;
    }

    if  (mn1 == 5)            {
      datasent[0] = 14;
      datasent[1] = 4;
      datasent[2] = 0;
    }



  }







  //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,NRF24,,,,,,,,,,,,,,,,,,,,,,,,




  //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,TRANSMITTING,,,,,,,,,,,,,,,,,,,,,,,,

  Serial.print("        MN1   IS  "); Serial.print(mn1);
  Serial.print("    DA[0]   IS  "); Serial.println(datasent[0]);

  if ((mn1 < 6) && ((datasent[0]) < 15)) {
    transMit();
  }





johnwasser

Too much of the sketch is missing for me to figure out what might be going wrong.  Please include your sketch as an attachment.  If you don't see the "Attachments and other options" menu below the reply box, click the "Preview" button.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

blh64

Auto format your code (CTRL-T in the IDE) and the indentation will give you a clue.  Only 1 statement is part of the if(radio.write(...)) conditional.

You should really get in the habit of ALWAYS using curly braces for your if/else, do/while, etc.

Farticus

Tx John.  Have complete RX sketch in attachment.
Have also attached sketch for TX side in case the problem in in the
Transmit function

BLH64 I now understand what you mean but altering that made no difference to my problem.
Tx for the advice though I will try to ensure I do this in the future.

Thanks

Go Up