Poll
Question: How to read multiple outputs from arduino?
Multiple readings on arduino? - 1 (100%)
More than one reading on arduino? - 0 (0%)
Total Voters: 1

Pages: [1] 2   Go Down
Author Topic: ARDUINO CODE PROBLEM!  (Read 4770 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)) >> smiley-cool)


//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!
Logged

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

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.
« Last Edit: March 10, 2011, 03:29:02 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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
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
« Last Edit: March 10, 2011, 03:49:29 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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 126
Posts: 8475
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:
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:
if (Serial.available() > 0)
   {
     data=Serial.read();
______
Rob


Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11730
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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?
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Quote
what does "byte pin" do?

Do you mean here?
Code:
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.
« Last Edit: March 11, 2011, 09:16:03 am 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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is my end result code:

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

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 126
Posts: 8475
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: March 11, 2011, 11:06:47 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: