Pages: [1]   Go Down
Author Topic: Make code more smaller  (Read 399 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
     is there a way to have less code to do this
Code:
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
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3008
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 33
Posts: 1435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, understand now. now here a section of my code
Code:
// 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
Logged

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

Code:
// 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.
« Last Edit: October 24, 2013, 02:25:47 pm by AWOL » 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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Leeds, UK
Online Online
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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;
  }
}
Logged

~Tom~

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Right on
Thank a lot
Logged

Pages: [1]   Go Up
Jump to: