ARDUINO CODE PROBLEM!

Hey guys,

Can you see if my code below needs adjusting? As i need my arduino to read multiple analog pins which are connected to my Razor IMU 6DOF (voidbot.net) and i dont think i wrote it well.

int analogPin = 0; //pin to get pot voltage
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;
int analogPin4 = 4;
int analogPin5 = 5;
int analogPin6 = 6;

int data;
int voltage=0;

int lowbyte = 0; // variable to store the value read
int highbyte=0;

#define LOWBYTE(v) ((unsigned char) (v))
#define HIGHBYTE(v) ((unsigned char) (((unsigned int) (v)) >> 8))

//init
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate
}

void loop ()
{
if (Serial.available() > 0)
{
data=Serial.read();

voltage = analogRead(analogPin);
voltage = analogRead(analogPin1);
voltage = analogRead(analogPin2);
voltage = analogRead(analogPin3);
voltage = analogRead(analogPin4);
voltage = analogRead(analogPin5);
voltage = analogRead(analogPin6);

lowbyte=LOWBYTE(voltage);
highbyte=HIGHBYTE(voltage);
Serial.write(lowbyte);
Serial.write(highbyte);

}
}

Are my analogPin and analogRead lines written wrongly?

Thanks for any help!

Which board do you have?
Do you really have seven analogue inputs?

(why the poll?)

Of course reading seven inputs and overwriting the same value seem a bit pointless, but who am I to complain?

You may want to read about arrays, and their best friend, the 'for' loop.

No need to write your own high and low byte macro - they're provided, just check the language reference.

I've got the arduino atmega 1280.
And yes, i do need all those inputs as they all read something different from my other board, so i dont understand why they would overwrite eachother. what am i doing wrong?

I'm hoping for some guidance into how to change this program. This is the first time im using arduino so im rusty.
Could really do with some help please!

voltage = analogRead(analogPin);
voltage = analogRead(analogPin1);
voltage = analogRead(analogPin2);

Read a value "from analogPin" and write it to the variable "voltage".
Read a value "from analogPin1" and write it to the variable "voltage".

Didn't you want to do something with the value you read from "analogPin"?

I'm hoping for some guidance into how to change this program.

Some idea of what it is you are trying to achieve would be useful

so i should change the name of the voltages (i.e. voltage 1 = analogPin1; voltage2 = analogPin2...) so that it will read several different ones right?
The aim of this is so that the arduino reads all these different voltages and displays them on one a single axes graph on my MATLAB, thats why i need them all

o i should change the name of the voltages (i.e. voltage 1 = analogPin1; voltage2 = analogPin2...) so that it will read several different ones right?

That will work, but a neater way is to use an array

int voltages [7];

voltages[0] = analogRead(analogPin0); // just for consistency, make this "analogPin0"
voltages[1] = analogRead(analogPin1);
voltages[2] = analogRead(analogPin2);
voltages[3] = analogRead(analogPin3);
voltages[4] = analogRead(analogPin4);
voltages[5] = analogRead(analogPin5);
voltages[6] = analogRead(analogPin6);

Now to clean it up even more

int analogPins [] = {0, 1, 2, 3, 4, 5, 6};

for (int i = 0; i < 7; i++) {
    voltages[i] = analogRead(analogPins[i]);
    Serial.println(voltages[i], HEX);    
}

or it you don't need the data for any other reason don't bother with the voltages array in the first place.

int analogPins [] = {0, 1, 2, 3, 4, 5, 6};
for (int i = 0; i < 7; i++) {
    Serial.println( analogRead(analogPins[i]), HEX);
}

BTW, what is this for?

 if (Serial.available() > 0)
   {
     data=Serial.read();

Rob

#define MAX_ANALOG 6

void loop ()
{
   if (Serial.available() > 0)  // wait for request for data
   {
     data=Serial.read();
     for (byte pin = 0 ; pin <= MAX_ANALOG ; pin++)
     {
       int value = analogRead (pin) ;   // best not to call it voltage if it's not in units of volts?
       Serial.write (LOWBYTE (value));
       Serial.write (HIGHBYTE (value));
     }
   }
}

I'm presuming the PC knows the number of samples per request?

Thanks so much for the help guys, im going to try the codes on my arduino and see if it works with my MATLAB now.

Btw i need the line:

if (Serial.available() > 0)
{
data=Serial.read();

because i need the computer to read the info obtained from the serial port where my arduino is connected to my 6DOF, therefore if there is data available, it will read it for me.

Also, what does "byte pin" do? ive never used that command before. does it take data from the pin byte by byte in other words? and by "pin", you are referring to the "analogPin" i used earlier right?

what does "byte pin" do?

Do you mean here?

for (byte pin = 0 ; pin <= MAX_ANALOG ; pin++)

It declares a variable called "pin" of type "byte" (8 bit unsigned integer), and uses it to iterate.
It just counts up from zero (byte pine = 0;) up to the value MAX_ANALOG, incrementing by one each time through the loop (pin++).
So, consecutively, "pin" takes the values 0, 1, 2, 3 , 4 , 5, 6.
The loop terminates when "pin > MAX_ANALOG".
These values are used as the analogue input pin number when "analogRead" is called, so each time through the "for" loop, you read a different input.
The variable "pin" is only in scope within the "for" loop.

Oh ok, i understand it now. Thanks for clearing that up!

Also,

" I'm presuming the PC knows the number of samples per request? "

What do you mean by this? Wont the pc know the number of samples from this code?

Wont the pc know the number of samples from this code?

How can it?
You're not telling it the number of samples anywhere and the PC can't see your code.

this is my end result code:

int analogPins [] = {0, 1, 2, 3, 4, 5, 6};    //pins to get pot voltage

int data;
int value=0;

#define LOWBYTE(v)   ((unsigned char) (v))
#define HIGHBYTE(v)  ((unsigned char) (((unsigned int) (v)) >> 8))
#define MAX_ANALOG 6

//init
void setup() 
{ 
  Serial.begin(9600); // opens serial port, sets data rate
} 

void loop ()
{
   if (Serial.available() > 0)        //wait for request for data
   {
     data=Serial.read();
     for (byte pin = 0 ; pin <= MAX_ANALOG ; pin++)  //the 8-bit unsigned integer uses "pin" to iterate
     {
       int value = analogRead (pin) ;   
       Serial.write (LOWBYTE (value));
       Serial.write (HIGHBYTE (value));
     } 
 }
}

won't the pc know the amount of samples from the first line of this code since im telling it from how many pins i need it to read the data from or it wont work that way?

Btw i need the line:

if (Serial.available() > 0)
{
data=Serial.read();

because i need the computer to read the info obtained from the serial port where my arduino is connected to my 6DOF, therefore if there is data available, it will read it for me.

But you totally ignore "data" after writing into it, how does the "computer" get this information?

Or are you just using this to trigger the return of the other data?

won't the pc know the amount of samples from the first line of this code

As AWOL said, "How can it?", the PC can't see inside your Arduino.


Rob

http://arduino.cc/en/Reference/LowByte
http://arduino.cc/en/Reference/HighByte
unless you really enjoy typing and debugging.

Can't the PC see into my arduino when i connect it to my MATLAB simulink then (i.e. connect target)?

I really wish i had some experience with this device because i feel so lost right now!

Can't the PC see into my arduino

No, all the PC sees is the end of a length of wire with some ones and zeroes on it.

Can you tell me what i need to change then please? How can i make my pc see the readings from my arduino? All i want to do is see the multiple signals on my arduino displayed on a graph :.

There are a number of ways you could fix your code. You could send some specific value as a start or end of packet marker. The matlab program would wait for the start or end marker to appear ignoring anything that was not the start or end marker, then reading everything until the next start or end marker.

For example:
"|",L0, H0, L1, H1, L2, H2, ..., L6, H6, "|", L0, ...

The problem with this approach is that whatever value you use as start or end of packet marker may represent a valid low or high byte.

You could send the values as text, with start and end markers and delimiters.
"<287, 1000, 14, 299, 127, 943, 883><286, 876, ..."

The disadvantage to this approach is that converting the values to text takes time, and parsing the data on the receiving end takes time, too. The amount of data sent increases, too.

The advantage is that the start and end markers and delimiters are clearly separate values. The PC can probably parse and convert the values in next to no time, and the increase in data sent is typically about 100%. Sending the integer value 12 as two bytes takes two bytes. Sending it as a character string takes two bytes, so no increase. Sending the integer value 1023 as two bytes takes 2 bytes. Sending it as a character string takes 4 bytes, so a 100% increase. Since most values will be in the 10 to 999 range, there will be, on average, 3 characters per value (plus a delimiter) vs. 2 bytes, for an average of 4 bytes per value.

Clearly, though, the PC can then figure out how many values are in each packet.

Hrmm, ive never used or even heard of start or end markers before! then again i have very little experience with arduino and matlab so im not surprised this is new to me..
Do u have an example, or if possible can you show me with the code i provided earlier a way of how to use these markers? I could really do with some help please

in arduino playground I found this:
http://www.arduino.cc/playground/Interfacing/Matlab
witch points to this

Here is another more direct approach: (with example of reading data from a photocell)
http://robotgrrl.com/blog/2010/01/15/arduino-to-matlab-read-in-sensor-data/

D.