Go Down

Topic: How do you generate 3 different random numbers in Arduino Mega & send to Serial (Read 1 time) previous topic - next topic

doughng

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.

AWOL

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

Quote
and send them differently

Morse?
Semaphore?
Heliograph?
"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.

doughng

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?

AWOL

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

doughng

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);
}


AWOL

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

doughng

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

AWOL

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

doughng

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?

AWOL

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


Schmidtn

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?

Fletcher Chr

Oi

Is this what you are looking for?

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

Schmidtn

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.

wildbill

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.

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview