Go Down

Topic: Variable declaring problem (Read 390 times) previous topic - next topic

Lentrave

Hi, i have a little piece of coding based on EasyTransfer. It's a soil moisture meter that take data and send it to another arduino. The code:

Code: [Select]
#include <EasyTransfer.h>

//create object
EasyTransfer ET;

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to send
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int soil1;
  int soil2;
  int soil3;
  int soil4;
  int soil5;
  int soil6;

};

//give a name to the group of data
SEND_DATA_STRUCTURE mydata;

void setup(){
  Serial.begin(9600);
  //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  ET.begin(details(mydata), &Serial);

  for(int i=2;i<=13;i++){
    pinMode(i,OUTPUT);
    digitalWrite(i, LOW);
  }

  for(int i=0;i<=5;i++){
    pinMode(i,INPUT);
  }

}

void loop(){
  for (int i=1;i<=6;i++){
    sensorRead(i);
    ET.sendData();
  }
}

void sensorRead(int sensor){
  digitalWrite(sensor*2, HIGH);
  digitalWrite(sensor*2+1, LOW);
  delay(1000);
  int val1 = analogRead(sensor-1);
  digitalWrite(sensor*2, LOW);
  digitalWrite(sensor*2+1, HIGH);
  delay(1000);
  int val2 = 1023 - analogRead(sensor-1);
  soil[sensor] = (val1 + val1) / 2;
  digitalWrite(sensor*2, LOW);
  digitalWrite(sensor*2+1, LOW);
}


but when i try to compile it i have this error:

Code: [Select]
EasyTransfer_TX_Example.cpp: In function 'void sensorRead(int)':
EasyTransfer_TX_Example.pde:-1: error: 'soil' was not declared in this scope


how can i declare the soil[] variable?
tanks

Professor Chaos

1) You need to declare soil as an array.  As your code stands, you would need to declare
Code: [Select]
int soil[7]; so you could reference members 1 to 6.  If you number your sensors 0 to 5 instead you could declare it to have only 6 members.

2) You would reference the array as a member of mydata:
Code: [Select]
mydata.soil[sensor] =  . . .

3) You declare val2 but never use it;  I think you intend to use (val1 + val2) rather than (val1 + val1) ?

marco_c

In your structure declare the soil data as soil[6]. This will give you variables soil[0] thru soil[5].

In your function you need to reference mydata.soil instead of just soil.

You will also need to change the loop call sensor read to start at 0 and end <6 because of the change to an array.

Whenever you need many types of the same related variable you simplify your code by using an array rather that declaring them the way that you did. This allows loops to work properly and in a more compact fashion.

It would also be useful to declare a constant value line numSoilSamples that is equal to 6. This is then used to size the array and as the termination number for loops. If in future you want 10 samples, then a simple change to the constant will allow all the code to work without other changes. The use of magic numbers in code is discouraged as in 6 months time you will not remember whether the 6 refers to the number is soil samples or the number of something else. Also if there are more than one 6, it is easy to chage the wrong one and screw up the code in mysterious ways.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Lentrave

Tanks. I will not use 10 sensor in the future because with 6 sensor all pin of the 328 is used but could you make me an example of your "constant value line numSoilSamples that is equal to 6" because i think i don't understand.

this is the modified code:

Code: [Select]
#include <EasyTransfer.h>

//create object
EasyTransfer ET;

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to send
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int soil[7];

};

//give a name to the group of data
SEND_DATA_STRUCTURE mydata;

void setup(){
  Serial.begin(9600);
  //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  ET.begin(details(mydata), &Serial);

  for(int i=2;i<14;i++){
    pinMode(i,OUTPUT);
    digitalWrite(i, LOW);
  }

  for(int i=0;i<6;i++){
    pinMode(i,INPUT);
  }

}

void loop(){
  //send the data
  for (int i=0;i<6;i++){
    sensorRead(i);
    ET.sendData();
   
  }
}

void sensorRead(int sensor){
  digitalWrite(sensor*2, HIGH);
  digitalWrite(sensor*2+1, LOW);
  delay(1000);
  int val1 = analogRead(sensor-1);
  digitalWrite(sensor*2, LOW);
  digitalWrite(sensor*2+1, HIGH);
  delay(1000);
  int val2 = 1023 - analogRead(sensor-1);
  mydata.soil[sensor] = (val1 + val2) / 2;
  digitalWrite(sensor*2, LOW);
  digitalWrite(sensor*2+1, LOW);
}

marco_c

Spell check got in the way.

Code: [Select]
const int numOfSamples = 6;

Then use that constant where you use 6.  You may want to look up the use of constants in the Arduino reference.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Go Up