Pages: [1]   Go Down
Author Topic: Replacing UNIT_x with variable  (Read 556 times)
0 Members and 1 Guest are viewing this topic.
Montreal, Qc, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure if I should post this here as it is more of a newby programming question but since it is within X10 topic here it is:

I want to cycle through 12 UNIT_number so I have this array:
Code:
char* devices[]={
"UNIT_1", // dining room
"UNIT_2", // front outside
"UNIT_3", // back deck
"UNIT_4", // stairs up
"UNIT_5", // front window
"UNIT_6", // hut
"UNIT_7", // back garage
"UNIT_8", // entrance
"UNIT_9", // Stairs down
"UNIT_10", // basement ceiling
"UNIT_11", // basement lamps
"UNIT_12"}; // basement over TV

how would I go about replacing
Code:
UNIT_1
with
Code:
device[i]

Code:
for (int i=0; i<11; i++){
 
   if timenow == Ontime[i] && stat[i] == 0;
     myHouse.write(HOUSE_J, UNIT_1, 1);
     myHouse.write(HOUSE_J, ON, 1);
   
  }
Logged

Mike

California
Online Online
Faraday Member
**
Karma: 88
Posts: 3356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As written in that for loop, UNIT_1 isn't a string, it's probably a #define or a const. You need to figure out the data type and create an array of those. Lets say it's an int:

Code:
int devices[]={
  UNIT_1, // dining room
  UNIT_2, // front outside
  UNIT_3, // back deck
  UNIT_4, // stairs up
  UNIT_5, // front window
  UNIT_6, // hut
  UNIT_7, // back garage
  UNIT_8, // entrance
  UNIT_9, // Stairs down
  UNIT_10, // basement ceiling
  UNIT_11, // basement lamps
  UNIT_12 // basement over TV
};

Then in the code:

Code:
myHouse.write(HOUSE_J, devices[i], 1);

You should also fix that if statement, as it's completely wrong in numerous aspects (missing parenthesis and braces, erroneous semi-colon)
Logged

Montreal, Qc, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes I am aware of my if statement, I just wrote this here quickly.
as for my issue it doesn't work.

I am using
Code:
#include <x10.h>
#include <x10constants.h>
Looking into the source of these I see that it is  byte data type.
I tried:
Code:
byte devices[]={
  UNIT_1, // dining room
  UNIT_2, // front outside
  UNIT_3, // back deck
  UNIT_4, // stairs up
  UNIT_5, // front window
  UNIT_6, // hut
  UNIT_7, // back garage
  UNIT_8, // entrance
  UNIT_9, // Stairs down
  UNIT_10, // basement ceiling
  UNIT_11, // basement lamps
  UNIT_12 // basement over TV
};

but when I try to compile I get error:
too many initializers for 'byte []'
Logged

Mike

California
Online Online
Faraday Member
**
Karma: 88
Posts: 3356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your full code and full error message.

The following:

Code:
#include <x10.h>
#include <x10constants.h>

byte devices[]={
  UNIT_1, // dining room
  UNIT_2, // front outside
  UNIT_3, // back deck
  UNIT_4, // stairs up
  UNIT_5, // front window
  UNIT_6, // hut
  UNIT_7, // back garage
  UNIT_8, // entrance
  UNIT_9, // Stairs down
  UNIT_10, // basement ceiling
  UNIT_11, // basement lamps
  UNIT_12 // basement over TV
};

void setup()
{
 
}

void loop()
{
 
}

Compiles just fine for me.
Logged

Montreal, Qc, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well... here is my complete code and getting the error.
Note, this code may appear a little funny to people on here, it is an ongoing process...
Code:
#include <Time.h>
#include <x10.h>
#include <x10constants.h>

#define zcPin 2
#define dataPin 3

String timenow;

byte devices[]={
"UNIT_1", // dining room
"UNIT_2", // front outside
"UNIT_3", // back deck
"UNIT_4", // stairs up
"UNIT_5", // front window
"UNIT_6", // hut
"UNIT_7", // back garage
"UNIT_8", // entrance
"UNIT_9", // Stairs down
"UNIT_10", // basement ceiling
"UNIT_11", // basement lamps
"UNIT_12"}; // basement over TV

char* Ontime[]={"8:0","8:30","8:30","8:10","8:10","8:10","8:30","8:10","8:10","","",""};

char* Offtime[]={"8:1","3:0","3:0","0:30","0:30","0:30","3:0","0:30","0:30","","",""};

// t for on/off from timer, m for manual, will figure something better later...
char* mode[]={"t","t","t","t","t","t","t","t","t","m","m","m"};

// 0= on/off lights, >0 = Dimmed lights
int val[]={10,0,0,30,0,0,0,30,30,30,0,0};

// 0=off, 1=on
int stat[]={0,0,0,0,0,0,0,0,0,0,0,0};

x10 myHouse =  x10(zcPin, dataPin);

void setup()
{
  Serial.begin(9600);
// will get time from NTP when I receive Ethenet shield, for now...
  setTime(7,59,55,6,7,13); // set time to noon Jan 1 2011
//  myHouse.write(HOUSE_B, UNIT_1, 1);
//  myHouse.write(HOUSE_B, OFF, 1);
}

void loop()
{
// check what time it is
  timenow = String(hour()) + ":" + String(minute());
  
  for (int i=0; i<11; i++){
  
// Turn on or off devices that are timed
    if (mode[i] == "t"){
      if (timenow == Ontime[i] && stat[i] == 0){
        myHouse.write(HOUSE_B, devices[i], 1);
          if (val[i] > 0){
            myHouse.write(HOUSE_B, ON, 1);
            myHouse.write(HOUSE_B, DIM, val[i]);
            stat[i] = 1;
            Serial.print("dim ");
            Serial.print(val[i]);
            Serial.print(" and stat = ");
            Serial.print(stat[i]);
            Serial.println("");
        }
      }
    }

    if (mode[i] == "t"){
      if (timenow == Offtime[i] && stat[i] == 1){
        myHouse.write(HOUSE_B, devices[i], 1);
        myHouse.write(HOUSE_B, OFF, 1);
        stat[i] = 0;
        Serial.print("off ");
        Serial.print(val[i]);
        Serial.print(" and stat = ");
        Serial.print(stat[i]);
        Serial.println("");
       }
    }
 
// if button1 pressed set TV time
  
  
// if button2 pressed set good night time
    
  
// check door switches and beep if open
  }

}

I am getting this error:

HouseControl:22: error: too many initializers for 'byte []'
« Last Edit: June 07, 2013, 03:40:10 pm by JABfreak » Logged

Mike

Montreal, Qc, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh my God...
I put quotes on my values!
Logged

Mike

California
Online Online
Faraday Member
**
Karma: 88
Posts: 3356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh my God...
I put quotes on my values!

Good example of why copying and pasting your whole code from the start is the better than posting from memory, or assuming what the other person posted is what your wrote into your code.
Logged

Montreal, Qc, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Totally agree and will do better next time!

Thanks for your help.
Logged

Mike

Pages: [1]   Go Up
Jump to: