Go Down

Topic: Make code more smaller (Read 474 times) previous topic - next topic

smallpoul

Hi,
     is there a way to have less code to do this
Code: [Select]
boolean sensor1_status = false;
boolean sensor2_status = false;
boolean sensor3_status = false;
boolean sensor4_status = false;
boolean sensor5_status = false;
boolean sensor6_status = false;
boolean sensor7_status = false;
boolean sensor8_status = false;
boolean sensor9_status = false;


I want 9 variable with boolean value, is this the best way to declare it or there a better way with less coding and more speed.
Thanks in advance
Mike

michinyon

Code: [Select]

boolean sensor_status[9]
for ( int j=0 ; j<9 ; j++ ) sensor_status[j]=false ;

KeithRB

If these are globals that are being initialized in init(), than you can simply ignore it since globals are inited to all zeros (false) at program start.

smallpoul

Thanks, understand now. now here a section of my code
Code: [Select]
// arrays to hold device addresses
DeviceAddress sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8, sensor9;
boolean sensor1_status = false;
boolean sensor2_status = false;
boolean sensor3_status = false;
boolean sensor4_status = false;
boolean sensor5_status = false;
boolean sensor6_status = false;
boolean sensor7_status = false;
boolean sensor8_status = false;
boolean sensor9_status = false;

void setup() {
 Serial.begin(9600);
 mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
 mySwitch.enableTransmit(4);
 mySwitch.setRepeatTransmit(2);
 
 sensors.begin();
 if (sensors.getAddress(sensor1, 0)) sensor1_status = true;
 if (sensors.getAddress(sensor2, 1)) sensor2_status = true;
 if (sensors.getAddress(sensor3, 2)) sensor3_status = true;
 if (sensors.getAddress(sensor4, 3)) sensor4_status = true;
 if (sensors.getAddress(sensor5, 4)) sensor5_status = true;
 if (sensors.getAddress(sensor6, 5)) sensor6_status = true;
 if (sensors.getAddress(sensor7, 6)) sensor7_status = true;
 if (sensors.getAddress(sensor8, 7)) sensor8_status = true;
 if (sensors.getAddress(sensor9, 8)) sensor9_status = true;
 
 // set the resolution to 9 bit
 sensors.setResolution(sensor1, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor2, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor3, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor4, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor5, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor6, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor7, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor8, TEMPERATURE_PRECISION);
 sensors.setResolution(sensor9, TEMPERATURE_PRECISION);
 Serial.println("No RF");
}


I want to do that in the for loop so there's no need to put all this code. The problem is with the Device address, how could i identify sensor one array with the sensor_statut

AWOL

#4
Oct 24, 2013, 09:16 pm Last Edit: Oct 24, 2013, 09:25 pm by AWOL Reason: 1
Code: [Select]
// arrays to hold device addresses
The code is at odds with that statement.

If you find you're writing the same code over again with only name suffixes which change, you should almost certainly be using loops and arrays.
"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.

smallpoul

Yes i know but since DeviceAddress is declare in the 1-wire library, i could not access it like this
Code: [Select]
Serial.println(DeviceAddress[1]);
it say
error: expected primary-expression before '[' token

AWOL

That's because the object you tried to subscript is not declared as an array.
"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.

Tom Carpenter

Arrays are really very simple. If you want sensor_1, then that would be sensor[0] - (note the zero index).

Code: [Select]
DeviceAddress sensor[9];
boolean sensor_status[9] = {0};
...
for (byte i = 0; i < 9; i++){
 if (sensors.getAddress(sensor[i], i)){
   sensor_status[i] = true;
 }
}
~Tom~

smallpoul


Go Up