Go Down

Topic: Replacing UNIT_x with variable (Read 646 times) previous topic - next topic

JABfreak

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: [Select]
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: [Select]
UNIT_1 with
Code: [Select]
device[i]

Code: [Select]
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);
   
  }
Mike

Arrch

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: [Select]
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: [Select]
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)

JABfreak

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: [Select]
#include <x10.h>
#include <x10constants.h>

Looking into the source of these I see that it is  byte data type.
I tried:
Code: [Select]
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 []'
Mike

Arrch

Post your full code and full error message.

The following:

Code: [Select]
#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.

JABfreak

#4
Jun 07, 2013, 10:37 pm Last Edit: Jun 07, 2013, 10:40 pm by JABfreak Reason: 1
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: [Select]
#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 []'
Mike

JABfreak

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

Arrch


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.

JABfreak

Totally agree and will do better next time!

Thanks for your help.
Mike

Go Up