Dicey

Dicey © GPL3+

Easy Arduino dice project, great for beginners learning how to create homemade functions.

  • 2,789 views
  • 0 comments
  • 13 respects

Components and supplies

Apps and online services

About this project

Overview

Hello everyone!

Sitting at this computer desk, I remembered from a book I have on Arduino that many programmers have used a built in function, random, for a plethora of projects that need to follow a unpredictable course, action or movement. By using this function in a simple random number-generating sketch, I modified the code into a fun little project that acts like rolling dice. I had never used random and quickly found it to be very useful for adding some more "life" into your project.

This project is great for beginners learning to use homemade functions. The sketches you have already run are made out of functions already there for you to use, like pinMode, digitalWrite,delay and more. There are multiple homemade functions in this code like "one"-"six " etc.

Imagine I'm sending a chocolate chip cookie recipe to a relative. I write every ingredient down with instructions on the order of mixing and baking. I stop, deciding that I want these cookies to be more than amazing. I decide to make handcrafted chocolate chips first before mixing them in. This little "recipe" for chocolate chips corresponds like a homemade function in Arduino. The Uno will read the "recipe" from top to bottom, but will also read a small "recipe" separate from the loop. That "recipe" is a homemade function. In the simple code I wrote, the homemade function, "one" will turn on one LED when the random number is 1. Likewise, when the random number is six, five or another number, the function connected to the random number will turn on the correct number of LEDs. Homemade functions save you lots of time, and help to organize your sketch much more.

Setting up the circuit

Alright, let's set up the circuit. Take the LEDs and line them up in the breadboard with the long end pointed to the right. (This leg will be connected to the pins ). To connect the LEDs, we will be using pins 1-6 for simplicity. Using the diagram, the LED on the left side will be connected to pin 6, the next LED to pin 5, continuing to the right the last LED will be connected to pin 1. Each short leg of the LEDs will be connected to a 220 Ohm resistor. The other leg of the resistor will be connected to the ground line. Take a wire and connect the ground to your Uno. We are done! The circuit should look something like this.

Now upload the code and enjoy!

Code

dicey.inoC/C++
int onePin = 1;
int twoPin = 2;
int threePin = 3;
int fourPin = 4;
int fivePin = 5;
int sixPin = 6;           //six leds connected with 220 ohm resistors
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(onePin,OUTPUT);
pinMode(twoPin,OUTPUT);
pinMode(threePin,OUTPUT);
pinMode(fourPin,OUTPUT);
pinMode(fivePin,OUTPUT);
pinMode(sixPin,OUTPUT);
}

void loop() {
int number = random(1,7);  //find random number and do void 1,6 accordingly
if (number==1)
 one();
else if(number==2)
 two();
else if (number==3)
 three();
else if (number==4)
 four();
else if (number==5)
 five();
else if (number==6)
 six();
}
void one()
  {
  Serial.println("one");       //Serial monitor also runs for viewing
  digitalWrite(onePin,HIGH);
  delay(50000);
  digitalWrite(onePin,LOW);
  delay(1000);
  }
void two()
  {
  Serial.println("two");
  digitalWrite(onePin,HIGH);
  digitalWrite(twoPin,HIGH);
  delay(50000);
  digitalWrite(onePin,LOW);
  digitalWrite(twoPin,LOW);
  delay(1000);
  }
void three()
  {
  Serial.println("three");
  digitalWrite(onePin,HIGH);
  digitalWrite(twoPin,HIGH);
  digitalWrite(threePin,HIGH);
  delay(50000);
  digitalWrite(onePin,LOW);
  digitalWrite(twoPin,LOW);
  digitalWrite(threePin,LOW);
  delay(1000);
  }
void four()
  {
  Serial.println("four");
  digitalWrite(onePin,HIGH);
  digitalWrite(twoPin,HIGH);
  digitalWrite(threePin,HIGH);
  digitalWrite(fourPin,HIGH);
  delay(50000);
  digitalWrite(onePin,LOW);
  digitalWrite(twoPin,LOW);
  digitalWrite(threePin,LOW);
  digitalWrite(fourPin,LOW);
  delay(1000);
  }
void five()
  {
  Serial.println("five");
  digitalWrite(onePin,HIGH);
  digitalWrite(twoPin,HIGH);
  digitalWrite(threePin,HIGH);
  digitalWrite(fourPin,HIGH);
  digitalWrite(fivePin,HIGH);
  delay(50000);
  digitalWrite(onePin,LOW);
  digitalWrite(twoPin,LOW);
  digitalWrite(threePin,LOW);
  digitalWrite(fourPin,LOW);
  digitalWrite(fivePin,LOW);
  delay(1000);
  }
void six()
  {
  Serial.println("six");
  digitalWrite(onePin,HIGH);
  digitalWrite(twoPin,HIGH);
  digitalWrite(threePin,HIGH);
  digitalWrite(fourPin,HIGH);
  digitalWrite(fivePin,HIGH);
  digitalWrite(sixPin,HIGH);
  delay(50000);
  digitalWrite(onePin,LOW);
  digitalWrite(twoPin,LOW);
  digitalWrite(threePin,LOW);
  digitalWrite(fourPin,LOW);
  digitalWrite(fivePin,LOW);
  digitalWrite(sixPin,LOW);
  delay(1000);
  }

Schematics

Diagram
Screen shot 2018 05 04 at 10 7ldd3ayd4w

Comments

Similar projects you might like

WiiChuck Controlling

Project showcase by jsheng

  • 785 views
  • 2 comments
  • 8 respects

Basic Arduino + JavaScript (Workshop)

by Alex Glow

  • 11,873 views
  • 1 comment
  • 27 respects

Simplest Way for Voice Recognition Project Using c#toarduino

Project tutorial by Jalal_Mansoori

  • 8,352 views
  • 28 comments
  • 48 respects

Light Sequence Creator

Project tutorial by Reid Paulhus

  • 6,404 views
  • 2 comments
  • 16 respects

Fidget Spinner RPM Counter

by Andriy Baranov

  • 38,707 views
  • 29 comments
  • 101 respects

Buzzer Alarm System With Help Of Arduino

by Team ONYX

  • 37,887 views
  • 5 comments
  • 21 respects
Add projectSign up / Login