Artwork

Nội dung được cung cấp bởi Programming Electronics Academy. Tất cả nội dung podcast bao gồm các tập, đồ họa và mô tả podcast đều được Programming Electronics Academy hoặc đối tác nền tảng podcast của họ tải lên và cung cấp trực tiếp. Nếu bạn cho rằng ai đó đang sử dụng tác phẩm có bản quyền của bạn mà không có sự cho phép của bạn, bạn có thể làm theo quy trình được nêu ở đây https://vi.player.fm/legal.
Player FM - Ứng dụng Podcast
Chuyển sang chế độ ngoại tuyến với ứng dụng Player FM !

Using Random Numbers with Arduino

13:02
 
Chia sẻ
 

Manage episode 180063768 series 1448561
Nội dung được cung cấp bởi Programming Electronics Academy. Tất cả nội dung podcast bao gồm các tập, đồ họa và mô tả podcast đều được Programming Electronics Academy hoặc đối tác nền tảng podcast của họ tải lên và cung cấp trực tiếp. Nếu bạn cho rằng ai đó đang sử dụng tác phẩm có bản quyền của bạn mà không có sự cho phép của bạn, bạn có thể làm theo quy trình được nêu ở đây https://vi.player.fm/legal.

This video tutorial talks about using the random() and randomSeed() functions with Arduino. It is pretty straight forward, but there are some intricacies worth noting.

Creating truly random numbers in Arduino is harder than you might think. The closest we can get in Arduino, and just about anywhere else, is using pseudo random numbers. That is, numbers that mimic randomness, but in fact do have a pattern if Want to fast-track your Arduino skills?

Click here to join our 12-part HD Video Course.

Why are Random Numbers with Arduino All the Same?

The most important thing to understand when using the random() function with Arduino is that it will generate the exact same list of pseudo random numbers every time.

So if you build a slot machine, and the first crank of the handle is a winner, then you can be sure that if you reset the Arduino board and pull the handle again - it will still be a winner the first time.

The easy way to overcome this is using the randomSeed() function.

This function takes a value (an integer for example), and uses the number to alter the random list generated by the random() function. The number you pass to the randomSeed() function is called a 'seed'.

You might put randomSeed() in the setup, and then use the random() function in the loop. Something like this:

//this variable will hold a random number generated by the random() function long randomNumber; //Set up - this is where you get things "set-up". It will only run once void setup() { //setup serial communications through the USB Serial.begin(9600); //Let's make it more random randomSeed(42); }//close setup void loop() { //generate a random number randomNumber = random(2,5); //display the random number on the serial monitor Serial.print("The Random Number is = "); Serial.println(randomNumber); }

But there is still an issue - even though the sequence of random numbers is different when using the randomSeed() function - it will still be the same every time the sketch is run. It is just a different list of pseudo random numbers!

One Solution to the Random Problem

So, what to do? Lucky for us the Arduino reference has a great solution. Use the analogRead() function to read a value from an unused analog pin. Since an unused pin that has no reference voltage attached to it is basically 'floating', it will return a "noise" value. This noise value can seed the randomSeed() function to produce differing sequences of random numbers every time the sketch is run.

Below is the sketch from the video using analogRead() and randomSeed() in unison:

/*How to use the random() and randomSeed() function YOU WILL NEED: (3) LEDs (3) 220OHM resistors (1) Jumper Wire (4) Hot cakes CIRCUIT: Connect a resitor to pin 2 and then to a breadboard. Connect the long leg of an LED to the resitor and the short leg to one of the ground rails on the breadboard Repeat this for the other components at pin 3 and 4 Coonect the ground on the breadboard to one of the Arduino GND pins. Created JUL 2014 by Michael James https://programmingelectronics.com/thearduinocourse */ //Declare and initialize LED pin variables int LED_1 = 2; int LED_2 = 3; int LED_3 = 4; //this variable will hold a random number generated by the random() function long randomNumber; //Set up - this is where you get things "set-up". It will only run once void setup() { //setup serial communications through the USB Serial.begin(9600); //Let's print a start messgae to the serial monitor when a new sequence of random numbers starts Serial.println("Starting new Random Number Sequence"); //set the LED pins as outputs pinMode(LED_1, OUTPUT); pinMode(LED_2, OUTPUT); pinMode(LED_3, OUTPUT); //Let's make it more random randomSeed(analogRead(A0)); }//close setup //The loop() runs over and over again void loop() { //generate a random number randomNumber = random(2,5); //display the random number on the serial monitor Serial.print("The Random Number is = "); Serial.println(randomNumber);

Well, hopefully that was random enough for you! If you enjoy this tutorial, I highly recommend checking out our free Arduino Crash Course (You can sign up below).

  continue reading

61 tập

Artwork
iconChia sẻ
 
Manage episode 180063768 series 1448561
Nội dung được cung cấp bởi Programming Electronics Academy. Tất cả nội dung podcast bao gồm các tập, đồ họa và mô tả podcast đều được Programming Electronics Academy hoặc đối tác nền tảng podcast của họ tải lên và cung cấp trực tiếp. Nếu bạn cho rằng ai đó đang sử dụng tác phẩm có bản quyền của bạn mà không có sự cho phép của bạn, bạn có thể làm theo quy trình được nêu ở đây https://vi.player.fm/legal.

This video tutorial talks about using the random() and randomSeed() functions with Arduino. It is pretty straight forward, but there are some intricacies worth noting.

Creating truly random numbers in Arduino is harder than you might think. The closest we can get in Arduino, and just about anywhere else, is using pseudo random numbers. That is, numbers that mimic randomness, but in fact do have a pattern if Want to fast-track your Arduino skills?

Click here to join our 12-part HD Video Course.

Why are Random Numbers with Arduino All the Same?

The most important thing to understand when using the random() function with Arduino is that it will generate the exact same list of pseudo random numbers every time.

So if you build a slot machine, and the first crank of the handle is a winner, then you can be sure that if you reset the Arduino board and pull the handle again - it will still be a winner the first time.

The easy way to overcome this is using the randomSeed() function.

This function takes a value (an integer for example), and uses the number to alter the random list generated by the random() function. The number you pass to the randomSeed() function is called a 'seed'.

You might put randomSeed() in the setup, and then use the random() function in the loop. Something like this:

//this variable will hold a random number generated by the random() function long randomNumber; //Set up - this is where you get things "set-up". It will only run once void setup() { //setup serial communications through the USB Serial.begin(9600); //Let's make it more random randomSeed(42); }//close setup void loop() { //generate a random number randomNumber = random(2,5); //display the random number on the serial monitor Serial.print("The Random Number is = "); Serial.println(randomNumber); }

But there is still an issue - even though the sequence of random numbers is different when using the randomSeed() function - it will still be the same every time the sketch is run. It is just a different list of pseudo random numbers!

One Solution to the Random Problem

So, what to do? Lucky for us the Arduino reference has a great solution. Use the analogRead() function to read a value from an unused analog pin. Since an unused pin that has no reference voltage attached to it is basically 'floating', it will return a "noise" value. This noise value can seed the randomSeed() function to produce differing sequences of random numbers every time the sketch is run.

Below is the sketch from the video using analogRead() and randomSeed() in unison:

/*How to use the random() and randomSeed() function YOU WILL NEED: (3) LEDs (3) 220OHM resistors (1) Jumper Wire (4) Hot cakes CIRCUIT: Connect a resitor to pin 2 and then to a breadboard. Connect the long leg of an LED to the resitor and the short leg to one of the ground rails on the breadboard Repeat this for the other components at pin 3 and 4 Coonect the ground on the breadboard to one of the Arduino GND pins. Created JUL 2014 by Michael James https://programmingelectronics.com/thearduinocourse */ //Declare and initialize LED pin variables int LED_1 = 2; int LED_2 = 3; int LED_3 = 4; //this variable will hold a random number generated by the random() function long randomNumber; //Set up - this is where you get things "set-up". It will only run once void setup() { //setup serial communications through the USB Serial.begin(9600); //Let's print a start messgae to the serial monitor when a new sequence of random numbers starts Serial.println("Starting new Random Number Sequence"); //set the LED pins as outputs pinMode(LED_1, OUTPUT); pinMode(LED_2, OUTPUT); pinMode(LED_3, OUTPUT); //Let's make it more random randomSeed(analogRead(A0)); }//close setup //The loop() runs over and over again void loop() { //generate a random number randomNumber = random(2,5); //display the random number on the serial monitor Serial.print("The Random Number is = "); Serial.println(randomNumber);

Well, hopefully that was random enough for you! If you enjoy this tutorial, I highly recommend checking out our free Arduino Crash Course (You can sign up below).

  continue reading

61 tập

Tất cả các tập

×
 
Loading …

Chào mừng bạn đến với Player FM!

Player FM đang quét trang web để tìm các podcast chất lượng cao cho bạn thưởng thức ngay bây giờ. Đây là ứng dụng podcast tốt nhất và hoạt động trên Android, iPhone và web. Đăng ký để đồng bộ các theo dõi trên tất cả thiết bị.

 

Hướng dẫn sử dụng nhanh