Array variable declaration problem

Hi to the forum. I have been working through some Arduino programs and have had much help by searching on various error messages from the compiler. Finding that (probably like all flavours of C that I'm not used to) that it is a bit of disciplinarian. However my latest problem is foxing me and some help would be appreciated. My code below. It is for an irrigation timer which will control 4 12v solenoid valves and a couple of pumps using a relay board (not the shield). Physically all that works fine.
My problem is that I am looping through a series of arrays and keep hitting issues with this. Firstly I made the mistake of referencing array elements using () brackets and not []. Fixed that one but now I am having problems with "variable not declared in this scope". Having done various searches I've checked my loop opening and closing braces (they seem to match up OK) and also moved the particular array declarations into the Void loop(). But can't find the solution. My code below:

``
#include <MillisTimer.h>



// These variables store the flash pattern
// and the current state of the LED

int ledPin1 =  12;      // the number of the LED pin
int ledState1 = LOW;             // ledState used to set the LED
unsigned long OldMillis;unsigned long NewMillis();   
// will store last time LED was updated

int MinuteCount=0 ;
int ledPin2 =  13;      // the number of the LED pin
int ledState2 = LOW;             // ledState used to set the LED
unsigned long previousMillis2 = 0;        // will store last time LED was updated
unsigned long previousMillisHeart = 0;    // printing heartbeat
unsigned long BeatTime = 1; // heartbeat time minutes between hearbeats
bool TimeStamp =false   ;  // if true time to print heartbeat
long OnTime2 = 25000;           // milliseconds of on-time
long OffTime2 = 75000;          // milliseconds of off-time
unsigned long Hours ;
unsigned long Minutes ;
 unsigned long CumOntime[]={0,0,0,0,0,0,0,0}; unsigned long CumOffTime[]={0,0,0,0,0,0,0,0};
 bool OnOff[8] {false,false,false,false,false,false,false,false}; //true means channel ON and false meanse Channel OFF
  String InputString[4]= ("");
 bool WaitforCommand = true;
 bool CommandsComplete=false;
 
 int Channel[]={0,0,0,0,0,0,0,0};
 int DurationMins[]{0,0,0,0,0,0,0,0};
 int RepeatHours[]{0,0,0,0,0,0,0,0};
 
 int Chann =0;
 int DurationMin = 0;
 int RepeatHour=0;
 int Index = 0;
 int I;
 int NumCommands = 8;
 bool OnOrOff = false;

//  FUNCTION FOR DATA ENTRY INTO ARRAY TABLE AT INDEX 

                bool DataInput(int Index)
               {
                   
                while (Serial.available() == 0) ; 
                      {  
                         // Wait for User to Input Data
                      }
                          
                                                                           
                            int Chann = Serial.parseInt();
                            int DurationMin =  Serial.parseInt();
                            
                            int RepeatHour = Serial.parseInt();
                            Channel [Index]= Chann; 
                            DurationMins [Index] = DurationMin;
                            RepeatHours[Index]=RepeatHour;
                           
                            Serial.print (Index);
                                  Serial.print("-- Channel # ");Serial.print(Channel[Index]);Serial.print(" ;  Duration - Mins ");
                                  Serial.print(DurationMins[Index]);
                                  Serial.print("  ;  Repeat - Hours  ");Serial.println( RepeatHours[Index]);
                           
                            
                            if ((Serial.read() == '\n')) {
                                 //Serial.println("end of commands received");
                                  WaitforCommand==false;  
                                                                 
                              
                                  }
                      if (Chann==0 ) 
                      {return WaitforCommand;}
 
                  }  /// end of function

     // Function for printing command table
                  int PrintTable(){
                  int i=0;
                  Serial.println(" Current Commands configured :");
                 Serial.println(" INDEX    CHANNEL   DURATION -MINS  REPEAT - HOURS  ");
                for  ( I=1;I <8; I=I+1 ) {
                  if (Channel[I] != 0){
                       Serial.print ("   "); Serial.print(I); Serial.print("           "); Serial.print (Channel[I]);Serial.print("          ");
                      
                      Serial.print(DurationMins[I]); Serial.print ("               ");Serial.println(RepeatHours[I]);
                      i= I; // value will be last valid entry with channel number not 0
                      }
                }
                return (i);}
         //Function for turning on channel
         bool TurnOnChann(int ChanNo)  {
         // switch on here
         digitalWrite (ChanNo+1,HIGH);
         return( true);
         } //end of TurnOnChann

         //Function for turning off channel
         bool TurnOff(int ChanNo)  {
         // switch off here
         digitalWrite(ChanNo,LOW);
         return (false);
         } // end of TurnOff
         
                



void setup() 
{
  // set the digital pin as output:`
 
  for (I=1; I <= 9;I=I+1 ) {
  pinMode(I,OUTPUT);
   TurnOnChann(I);
  }
  
  
  Serial.begin(9600);
  Serial.print("started..."); 
  Serial.print("Enter command: "); //Prompt User for Input
  Serial.println("Channel No (1-8) followed by  ';' followed by duration Minutes(1-9) folowed by Repeat Hours, followed by next Channel No etc  ");
   
}





void loop() {
  // put your main code here, to run repeatedly:
    int I1=0;int I=0 ;
    
   for ( I=1 ; I< 4;I= I+1)  // THIS LOOP TAKES DATA ENTRY THEN PRINTS OUT THE CHANNEL TABLE
      
       {
         ;
         
                Serial.print("? - Datainput  ");Serial.println(I);
                WaitforCommand= DataInput(I);
                             
                
       }
               
                NumCommands= PrintTable() ;  
                Serial.println("Ready to process commands");

       // this loop steps through the command arrays and controls the digital outputs
       // first go through and turn on each digital output then off again
      
       do{ 
       Serial.print(" Sequencing outputs all on, then all off.... ");
       I1=I1+1; Serial.println(I1);
       for (I=2; I<=9;I=I+1) {
         TurnOnChann(I);
        delay(1000);
        
       }
       delay (10000);
       for (I=2; I<=9;I=I+1) {
        TurnOff(I);
        delay(1000);
        
       }
       delay(10000); 
       
       } while (I1<=100);     //does the input/output thing 100 times         

// now do the hard stuff...!
       
        OldMillis=  millis();
        for (;;){   //  start of infinite loop
        for (I=1;I<=8; I=I+1) { //step through command arrays
        
   
        if (OnOff[I]==1)  {  
        if (CumOnTime[I]/6000 <=DurationMins[I]) {
        CumOnTime[I]= CumOnTime[I]+Millis()-OldMillis;
        //channel is ON so accumulate its ON  time
              }}
        else {
        TurnOff(I) ;  //call turnoff
        OnOff[I]=0;
        CumOnTime[I]=0;
             } // turn it off and update the on/off status and zero the cumulataive on time
          
       

          // things to do while its on
         if( OnOff[I]==0)  {
          if (CumoffTime[I]/360000 <=RepeatHours)  {
            CumOffTime[I]=CumOffTime[I]+Millis()-OldMillis;
          }
          else { 
           TurnOn(I);
           OnOff[II]=1;
            CumOffTime[II]=0;
             }  // turn it on and update the on/off status and zero the cumulataive on time
          
            }
        
          
            }
       OldMillis= millis() ;// puts updated time into OldMillis
        }  // end of infinite loop

}  //End of Main loop

I hope I have posted this correctly as have not posted to the forum before...

Many thanks in anticipation: Sammy the Sheltie Dog.

You declare an array "CumOntime" and you try to use an array "CumOnTime". Compilers are picky about capitalization.

In two places you has 'Millis()' instead of 'millis()'.

You used "CumoffTime[]" in one place

You define "TurnOnChann()" and sometimes use "TurnOn()".

You use the variable 'II' where you only have 'I' and 'I1'.

In some loops you are counting 1 to 7, skipping element 0 of your arrays. In others you are counting 1 to 9, skiping 0 and going off the end.

I got rid of the errors and warnings but I don't understand your sketch well enough to know if it does what you want.

//#include <MillisTimer.h>

// These variables store the flash pattern
// and the current state of the LED

const int ledPin1 =  12;      // the number of the LED pin
int ledState1 = LOW;             // ledState used to set the LED
unsigned long OldMillis; unsigned long NewMillis();
// will store last time LED was updated

int MinuteCount = 0 ;
const int ledPin2 =  13;      // the number of the LED pin
int ledState2 = LOW;             // ledState used to set the LED
unsigned long previousMillis2 = 0;        // will store last time LED was updated
unsigned long previousMillisHeart = 0;    // printing heartbeat
unsigned long BeatTime = 1; // heartbeat time minutes between hearbeats
bool TimeStamp = false   ; // if true time to print heartbeat
long OnTime2 = 25000;           // milliseconds of on-time
long OffTime2 = 75000;          // milliseconds of off-time
unsigned long Hours ;
unsigned long Minutes ;
unsigned long CumOnTime[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned long CumOffTime[8] = {0, 0, 0, 0, 0, 0, 0, 0};
bool OnOff[8] {false, false, false, false, false, false, false, false}; //true means channel ON and false meanse Channel OFF
String InputString[4];
bool WaitforCommand = true;
bool CommandsComplete = false;

int Channel[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned DurationMins[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned RepeatHours[8] {0, 0, 0, 0, 0, 0, 0, 0};

int Chann = 0;
int DurationMin = 0;
int RepeatHour = 0;
int Index = 0;
int I;
int NumCommands = 8;
bool OnOrOff = false;

//  FUNCTION FOR DATA ENTRY INTO ARRAY TABLE AT INDEX

bool DataInput(int Index)
{
  while (Serial.available() == 0) ;
  {
    // Wait for User to Input Data
  }

  int Chann = Serial.parseInt();
  int DurationMin =  Serial.parseInt();
  int RepeatHour = Serial.parseInt();
  
  Channel [Index] = Chann;
  DurationMins [Index] = DurationMin;
  RepeatHours[Index] = RepeatHour;

  Serial.print (Index);
  Serial.print("-- Channel # "); 
  Serial.print(Channel[Index]); 
  Serial.print(" ;  Duration - Mins ");
  Serial.print(DurationMins[Index]);
  Serial.print("  ;  Repeat - Hours  "); 
  Serial.println( RepeatHours[Index]);

  if ((Serial.read() == '\n'))
  {
    //Serial.println("end of commands received");
    WaitforCommand = false;
  }

  if (Chann == 0)
  {
    return WaitforCommand;
  }
  return WaitforCommand;
}  /// end of function

// Function for printing command table
int PrintTable()
{
  int i = 0;
  Serial.println(" Current Commands configured :");
  Serial.println(" INDEX    CHANNEL   DURATION -MINS  REPEAT - HOURS  ");
  for  ( I = 0; I < 8; I = I + 1 )
  {
    if (Channel[I] != 0)
    {
      Serial.print ("   "); Serial.print(I); Serial.print("           "); Serial.print (Channel[I]); Serial.print("          ");

      Serial.print(DurationMins[I]); Serial.print ("               "); Serial.println(RepeatHours[I]);
      i = I; // value will be last valid entry with channel number not 0
    }
  }
  return (i);
}

//Function for turning on channel
bool TurnOn(int ChanNo)
{
  // switch on here
  digitalWrite (ChanNo + 1, HIGH);
  return ( true);
} //end of TurnOnChann

//Function for turning off channel
bool TurnOff(int ChanNo)
{
  // switch off here
  digitalWrite(ChanNo, LOW);
  return (false);
} // end of TurnOff

void setup()
{
  // set the digital pin as output:`

  for (I = 0; I < 8; I = I + 1 )
  {
    pinMode(I, OUTPUT);
    TurnOn(I);
  }

  Serial.begin(9600);
  Serial.print("started...");
  Serial.print("Enter command: "); //Prompt User for Input
  Serial.println("Channel No (1-8) followed by  ';' followed by duration Minutes(1-9) folowed by Repeat Hours, followed by next Channel No etc  ");
}

void loop()
{
  // put your main code here, to run repeatedly:
  int I1 = 0; int I = 0 ;

  for ( I = 1 ; I < 4; I = I + 1) // THIS LOOP TAKES DATA ENTRY THEN PRINTS OUT THE CHANNEL TABLE
  {
    Serial.print("? - Datainput  "); 
    Serial.println(I);
    WaitforCommand = DataInput(I);
  }

  NumCommands = PrintTable() ;
  Serial.println("Ready to process commands");

  // this loop steps through the command arrays and controls the digital outputs
  // first go through and turn on each digital output then off again

  do
  {
    Serial.print(" Sequencing outputs all on, then all off.... ");
    I1 = I1 + 1; Serial.println(I1);
    for (I = 2; I <= 9; I = I + 1)
    {
      TurnOn(I);
      delay(1000);

    }
    delay (10000);
    for (I = 2; I <= 9; I = I + 1)
    {
      TurnOff(I);
      delay(1000);

    }
    delay(10000);

  }
  while (I1 <= 100);     //does the input/output thing 100 times

  // now do the hard stuff...!

  OldMillis =  millis();
  for (;;)    //  start of infinite loop
  {
    for (I = 0; I < 8; I = I + 1) //step through command arrays
    {
      if (OnOff[I] == 1)
      {
        if (CumOnTime[I] / 6000 <= DurationMins[I])
        {
          CumOnTime[I] = CumOnTime[I] + millis() - OldMillis;
          //channel is ON so accumulate its ON  time
        }
      }
      else
      {
        TurnOff(I) ;  //call turnoff
        OnOff[I] = 0;
        CumOnTime[I] = 0;
      } // turn it off and update the on/off status and zero the cumulataive on time

      // things to do while its on
      if ( OnOff[I] == 0)
      {
        if (CumOffTime[I] / 360000 <= RepeatHours[I])
        {
          CumOffTime[I] = CumOffTime[I] + millis() - OldMillis;
        }
        else
        {
          TurnOn(I);
          OnOff[I] = 1;
          CumOffTime[I] = 0;
        }  // turn it on and update the on/off status and zero the cumulataive on time
      }
    }
    OldMillis = millis() ; // puts updated time into OldMillis
  }  // end of infinite loop

}  //End of Main loop

If you use inline single backticks, you will get a nice CumoffTime[] as you intended :wink:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.