Pages: [1]   Go Down
Author Topic: For Cycle - Never Execute - Is Possible ?  (Read 300 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good Morning,
so looking the simple sketch below reported seems that "for cycles is never execute".
I've tried to run without "For" and the code is working correctly... I don't think to have done a mistake or error but it's very strange ... could you help kindly me ?

Code:
#include <Time.h>
#include <TimeAlarms.h>
#include <LiquidCrystal.h> // ensure that the include path is set
#include "M2tk.h"
#include <VirtualWire.h>
#include <Wire.h>
#include <DS1307.h>
#include <EEPROM.h>
#include "utility/m2ghlc.h"

 byte devices;
 byte array [30] [12];

void setup()

Serial.begin(9600); 
Serial.println("ciao");
setTime(7,13,45,11,2,(2013-2000));
array[0][0] = 0;
//array[0][1] = 0;
array[0][2] = 7;
array[0][3] = 14;
array[0][4] = 11;
array[0][5] = 2;
array[0][6] = 13;
array[0][7] = 7;
array[0][8] = 15;
array[0][9] = 11;
array[0][10] = 2;
array[0][11] = 13;
array[0][12] = 0;
devices = 32;
Serial.println(array[0][0]);
Serial.println(array[0][1]);
Serial.println(array[0][2]);
Serial.println(array[0][3]);
Serial.println(array[0][4]);
Serial.println(array[0][5]);
Serial.println(array[0][6]);
Serial.println(array[0][7]);
Serial.println(array[0][8]);
Serial.println(array[0][9]);
Serial.println(array[0][10]);
Serial.println(array[0][11]);
Serial.println(array[0][12]);

Alarm.timerRepeat(30, ReadArray);
}

void loop()
{   
  Alarm.delay(0);
}

void ReadArray()
{
 
  for (int i = 0; i >=  devices ; i = i++)
    {
     char dataNow[15];
     char dataArrayStart[15];
     char dataArrayStop[15];
     time_t t = now();
     sprintf(dataNow, "%04d%02d%02d%02d%02d", year(t), month(t), day(t), hour(t), minute(t));
     Serial.println(dataNow);
     sprintf(dataArrayStart, "%04d%02d%02d%02d%02d", (array[i][6]+2000), array[i][5], array[i][4], array[i][2], array[i][3]);
     Serial.println(dataArrayStart);
     sprintf(dataArrayStop, "%04d%02d%02d%02d%02d", (array[i][11]+2000), array[i][10], array[i][9], array[i][7], array[i][8]);
     
     if (strcmp(dataNow,dataArrayStart) == 0 && strcmp(dataNow,dataArrayStart) <= 0)
       {
         if (array[i][12] == 0)
          {
            array[i][12] = 32 ;
            Serial.println("Turn On");
          }
         else
           if (array[i][12] == 32)
            {
             array[i][12] = 0;
             Serial.println("Turn Off");
            }
       }
      } 


The code should count from 0 to 31 and inside the cycled matches date and that it,

regards,
gnux
Logged

UK
Offline Offline
God Member
*****
Karma: 17
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Only a quick look on my phone as I wake up....but, you look as if you should be calling your readArray function.

You use readArray when it should be readArray()

Setting your values is a very long winded way about it too. You should be able to do that all on a single declaration. I'm sure someone else will be along shortly to check over more thoroughly than I can at present.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4242
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  for (int i = 0; i >=  devices ; i = i++)
This loop will execute while i is greater than or equal to 32.
i starts at 0, it is not greater or equal to 32 so the loop stops before it even starts.  I suspeict that you meant to type i <= devices
Your method of incrementing the loop variable is unusual.  It works, but all you need is i++
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26242
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Only a quick look on my phone as I wake up....but, you look as if you should be calling your readArray function.

You use readArray when it should be readArray()
It's used as a function pointer; there's nothing at all wrong with this usage.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18732
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  for (int i = 0; i >=  devices ; i = i++)
...
Your method of incrementing the loop variable is unusual.  It works, but all you need is i++

I'm not convinced it is correct.

http://www.viva64.com/en/t/0065/

Quote
The expression i=i++ is often given as an example in which the 'i' variable is being assigned to and incremented at the same time. What value will the 'i' variable posses? The language standard must either define one of the possible program behaviors as the only correct one, define a range of behaviors which are correct, or specify that the program's behavior is completely undefined in such a case. In C and C++, calculation of the i=i++ expression causes an undefined behavior since this expression does not contain any sequence points inside.

I strongly advise you not to do that. Not only do people go "huh?" looking at it, it could be simply wrong.

You want two corrections (at least) namely:

Code:
  for (int i = 0; i <=  devices ; i++)

I am also doubtful about your use of this:

Code:
devices = 32;

Given that you are indexing into "array" using "i", and given that array is:

Code:
byte array [30] [12];

Then you are overshooting the bounds of "array" by three. Perhaps find a better name for "array" too, you may as well call your variables "variable".
Logged


East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4242
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Your method of incrementing the loop variable is unusual.  It works, but all you need is i++

I'm not convinced it is correct.
From the link that you gave it appears that it is best avoided.  My basis for saying that 'It works' was based on testing a simple for loop with that construction but it is obviously better not to rely on it working.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good Morning,
sorry but I was sleeping again when I write to code ...

you have reason is necessary change the condition :-)

Thanks again,
enjoy the week,
gnux
Logged

Pages: [1]   Go Up
Jump to: