Go Down

Topic: ARDUINO CODE PROBLEM! (Read 5 times) previous topic - next topic

rebecca

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 (http://voidbot.net/razor-6dof.html) 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!

AWOL

#1
Mar 10, 2011, 09:24 pm Last Edit: Mar 10, 2011, 09:29 pm by AWOL Reason: 1
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.
"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.

rebecca

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!

AWOL

#3
Mar 10, 2011, 09:46 pm Last Edit: Mar 10, 2011, 09:49 pm by AWOL Reason: 1
Code: [Select]
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"?

Quote
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
"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.

rebecca

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

Graynomad

Quote
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

Code: [Select]
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

Code: [Select]
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.

Code: [Select]

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?

Code: [Select]
if (Serial.available() > 0)
   {
     data=Serial.read();

______
Rob


Rob Gray aka the GRAYnomad www.robgray.com

MarkT

Code: [Select]

#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?
[ I won't respond to messages, use the forum please ]

rebecca

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?

AWOL

#8
Mar 11, 2011, 03:08 pm Last Edit: Mar 11, 2011, 03:16 pm by AWOL Reason: 1
Quote
what does "byte pin" do?


Do you mean here?
Code: [Select]
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.
"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.

rebecca

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?

AWOL

Quote
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.
"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.

rebecca

this is my end result code:

Code: [Select]
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?

Graynomad

#12
Mar 11, 2011, 05:04 pm Last Edit: Mar 11, 2011, 05:06 pm by Graynomad Reason: 1
Quote
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?

Quote
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

Rob Gray aka the GRAYnomad www.robgray.com

AWOL

http://arduino.cc/en/Reference/LowByte
http://arduino.cc/en/Reference/HighByte
unless you really enjoy typing and debugging.
"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.

rebecca

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!

Go Up