Pages: [1]   Go Down
Author Topic: How do you generate 3 different random numbers in Arduino Mega & send to Serial  (Read 1049 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I need help in generating 3 different random numbers in Arduino Mega, and send them differently at the same rate, to the serial port.
Logged

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

Have you thought of using the "random" function?
http://arduino.cc/en/Reference/Random

Quote
and send them differently
Morse?
Semaphore?
Heliograph?
« Last Edit: April 11, 2011, 03:46:11 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.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, I already used it to generate  a random number from 1-50, and sent it to the serial port. But I need to also generate another set of random numbers, and send thorugh the same port. But how do I separate them?
Logged

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

Quote
But how do I separate them?
Commas?
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I dont understand..where do I put the comma?
This is what I used to generate the 1st set of numbers from 1-50:

long randNumber;
void setup(){
  Serial.begin(9600);

    randomSeed(analogRead(0));
}

void loop() {
   // print a random number from 1 to 50
  randNumber = random(1, 51);
  Serial.println(randNumber);

  delay(50);
}

Logged

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

OK, there you've used new-line to separate your values.
What are you asking?
You specification isn't very clear.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm supposed to be reading 3 different random numbers from the Serial port using C#. I want to simulate data from 3 different sensors. So thats why I need to generate random numbers
The 1st set of random numbers is called speed
the 2nd set is  called pressure
the 3rd one is Angular displacement
Logged

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

I'm sorry, I still don't understand.
You're already reading fifty numbers from the serial port - what's the big deal about reading another three (a mere 6% increase)?

Physical constant like speed, pressure and displacement don't normally vary randomly - this doesn't seem very "real-world", IMHO.
« Last Edit: April 11, 2011, 05:39:16 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.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, so what about in a normal life scenario whereby you have about 3 different sensors attached to an arduino. How do you send, and how does C# read them independently?
Logged

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

Quote
How do you send, and how does C# read them independently?
You've already answered the first part in your sketch above.
The second is a C# question, and is best addressed to a C# forum.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok
Logged

California
Offline Offline
Full Member
***
Karma: 0
Posts: 129
That's me, the boy with the broken halo.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think he's trying to debug the Arduino side of a project.  He'll later add three sensors, but right now he's using random numbers generated by static on analog pin zero to get his sensor inputs.

Your test sketch looks like it'll work, to me.  Ever 50 delay you get a new data sample (from static right now, but later from your sensors).  I know nothing about C# so I can't help you there.

What else are you trying to do with the data?  Do they go into some kind of physics/ME equation?
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 156
It was all digital
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oi

Is this what you are looking for?

Code:
void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {
   // print a random number from 1 to 50
  for (int i = 0; i <= 2; i++){
    Serial.print(random(1,51));
    Serial.print(" ");
  }
  Serial.println();
  delay(50);
}

Numbers are space seperated.

-Fletcher
Logged

California
Offline Offline
Full Member
***
Karma: 0
Posts: 129
That's me, the boy with the broken halo.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think he just wants to verify that he's getting any sort of data at all, not really important how it's separated (commas or spaces) as long as he can read it.  I'm guessing he's going to assign the numbers he gets from his sensors to variables that plug into an equation.

... and I base that on absolutely nothing, so take it for what it is.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're trying to simulate data from three sensors, I suspect what you're trying to do is get one reading from each, then more sets of three until you have sent 50 sets. If so, send a prefix character before each reading to identify it, say 'S' for speed, 'P' for pressure, 'D' for displacement. Then on the C# side of the house you can parse out the identifiers to get the reading's type.
Logged

Pages: [1]   Go Up
Jump to: