array doubt!

Hello Everybody!

I might have questions.
One regards arrays.

AnalogInput = analogRead(AnSignal*); // read the value from the sensor:(numero da 0 a 1023)*
is that correct?

I am surprised that it prints at all with no Serial.begin() in the program and the fact that the tx and rx arrays are not declared in the program and the final brace is missing from the loop() function. Have you posted the code that you are actually running ?

Why is this a float?
float AnalogInput[3] = {0, 0, 0} ; // variables to store the value coming from the sensor
analogRead only returns an int, and only 10 bits at that.

These only need to be byte, and the 2nd one could be const also - not changing the pins, are you?
const int LED[3] = {9, 11, 12}; // select the pin for the LED
int photoresistor[3] = {6, 7, 8}; // photoresistor input

How can it print when Serial.begin(speed); is missing? Usually goes in here:
void setup()
{

}
Does analogWrite work if the pins are not declared as OUTPUT? I never tried Not declaring them before using them - the IDE might take care of that.

These need to be [ 3 ]:
int tx[ 2 ]; // transmitted signals
int rx[ 2 ]; //Received signals

as you use [ 0 ], [ 1 ], and [ 2 ]
Otherwise [2] is accessing who knows what and possibly creating mayhem as you overwrite something else.

The principle of using an array of pin numbers and a parallel array to hold the inputs from them is fine. However, in your code you have this

int photoresistor[3] = {6, 7, 8}; // photoresistor input

Then later

  for (int i = 0; i < 3; i++) // //cycle to read all the received signal
  {
    rx[i] = analogRead(photoresistor[i]);
    //                    RECEV=map(rx[0],0,1023,120,160);
  }

So you are trying to do alalogRead()s from digital pins

Giulialiuyi:
another things when it prints the value it is so slowly! It is wrong my understanding of arrays? and why it is so slow to print?

Your code will print nothing, as a call of Serial.begin is missing in your code.

And the printing speed depends on the serial baudrate: With Serial.begin(9600) you can print 960 characters per second and with Serial.begin(115200) you can print 11520 characters per second.

If you want to run your loop with maximum speed, don't slow it down by blocking your code with Serial.print commands sent to a full outgoing Serial buffer!

The outgoing Serial buffer can take up to 63 chars. So don't print more than 63 chars in a time (depends on baud rate) that will be needed to send 63 chars. With 9600 baud this is 63/960= ca. 65 milliseconds. With 115200 baud this is 63/11520= ca. 5 milliseconds.

So if you do not want to slow down your application using "Serial.print":

  • do not send more than 63 characters each 65 milliseconds at 9600 baud
  • do not send more than 63 characters each 5 milliseconds at 115200 baud
    Otherwise you will slow down the application as it has to "wait" until Serial characters are sent from the outgoing buffer.

thank you every body for the answers!!! it is all much more clear now.
Thanks @CrossRoads I will apply your suggested modification.
And thank you UKHELIBOB for the clarification of the array.

THE LAST PART OF RX IS NOT important at all. i did that only to see if it was receiveing but I don't need to see what.

And it is not always necessary to decleare pinMode. that it is what I read and it was working without declaration!

Serial.print("  sensor1 = " );
  Serial.print(AnSignal[1]);

Why would it surprise you that it doesn't change? You're printing the pin numbers. If you want to see it change with the changing signal then print the reading instead.

tx[i] = map(AnalogInput[i], 0, 1023, 0, 255);

. . . is an expensive way of writing tx[i] = AnalogInput[i] / 4; which the compiler will probably interpret as tx[i] = AnalogInput[i] >> 2;

yes Delta_G! you are right! :slight_smile: :slight_smile: :slight_smile:
I get it after a little bit.. ahahha I must being tired. :sleeping:
AWOL I don't understand what does it means

tx[i] = AnalogInput[i] >> 2;

It means tx[i] = AnalogInput[i] / 4;

Giulialiuyi:
yes Delta_G! you are right! :slight_smile: :slight_smile: :slight_smile:
I get it after a little bit.. ahahha I must being tired. :sleeping:
AWOL I don't understand what does it means

tx[i] = AnalogInput[i] >> 2;

http://www.arduino.cc/en/Reference/Bitshift

Shifting a binary number to the right is the same as dividing by some power of 2. Just like in decimal when we divide by a power of 10 we simply shift the number over to the right. ie: 4300 / 10 == 430.

Giulialiuyi:
I don't understand what does it means

Bitshifting is explained here: http://www.arduino.cc/en/pmwiki.php?n=Reference/Bitshift

It means that the bits in the binary representation of the variable are either shifted to the right (>>) or to the left (<<). Some bits are then "shifted out" of the variable. And the newly created bits are replaced by zero bits.

As the shifting of bits takes place in the dual system, a left-shifting by one bit is a multiplication by 2, and a right-shifting by one bit is a division by 2.

Giulialiuyi:
yes Delta_G! you are right! :slight_smile: :slight_smile: :slight_smile:
I get it after a little bit.. ahahha I must being tired. :sleeping:

It would help considerably if you used the word pin in the variable name when it refers to a pin number. It will work with (practically) any name but I suspect that you might have spotted the problem yourself if you saw

Serial.print(analogPins[1]);