ROLLING DICE 7segment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ROLLING DICE

This example consists of a push button and a single 7 segment display. Every time the push button is
pressed and held, the display loops through numbers 0-9 rapidly. Once the button is released, the
display continues to loop for a period of time almost equal to the time the button was pressed, and
then displays a number along with the decimal point to indicate the new number.

To build the circuit (with the 5161AS display), connect it like this:

Then upload this program to the Arduino:

#include "SevSeg.h"
SevSeg sevseg;

const int buttonPin = 10; // the pin that the pushbutton is attached to
int buttonState = 0; // current state of the button
int lastButtonState = LOW; // previous state of the button
int buttonPushCounter = 0; // counter for the number of button presses
long counter = 0;
long max_long_val = 2147483647L;

void setup(){
byte numDigits = 1;
byte digitPins[] = {};
byte segmentPins[] = {6, 5, 2 , 3, 4 , 7, 8, 9};
bool resistorsOnSegments = true;

byte hardwareConfig = COMMON_CATHODE;


sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,
resistorsOnSegments);
sevseg.setBrightness(90);

pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
lastButtonState = LOW;
}

void loop(){
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH){
buttonState = LOW;
}
else
buttonState = HIGH;

if(buttonState == HIGH){
Serial.println("on");
lastButtonState = HIGH;
buttonPushCounter++;
if(counter < max_long_val)
counter++;
buttonPushCounter %= 9;
sevseg.setNumber(buttonPushCounter, 1);
sevseg.refreshDisplay();
delay(100 - (counter%99));
}

else{
Serial.println("off");
if(lastButtonState == HIGH){
Serial.println("in");
buttonPushCounter++;
buttonPushCounter %= 7;
if(buttonPushCounter == 0)
buttonPushCounter = 1;
counter--;
sevseg.setNumber(buttonPushCounter, 1);
sevseg.refreshDisplay();
delay(100 - (counter%99));
if(counter == 0){
lastButtonState = LOW;
sevseg.setNumber(buttonPushCounter, 0);
sevseg.refreshDisplay();
}
}
}
}

4 DIGIT 7-SEGMENT DISPLAYS


So far we have only worked with single digit 7-segment displays. To display information such as the
time or temperature, you will want to use a 2 or 4 digit display, or connect multiple single digit
displays side by side.

I’m using a 4 digit 7-segment display with the model number 5641AH, but the wiring diagrams
below will also work with the 5461AS.
Here is a diagram showing the pinout of these displays:
The digit pins D1, D2, D3 and D4 need to be connected to current limiting resistors, since they are
the common terminals of the digits. The connections are shown below:

This simple program will print the number “4.999” to the display:

#include "SevSeg.h"
SevSeg sevseg;

void setup(){
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};

bool resistorsOnSegments = true;


bool updateWithDelaysIn = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,
resistorsOnSegments);
sevseg.setBrightness(90);
}

void loop(){
sevseg.setNumber(4999, 3);
sevseg.refreshDisplay();
}

In the code above, we set the number of digits in line 5 with byte numDigits = 4.
Since multi-digit displays use digit pins, we also need to define which Arduino pins will connect to
the digit pins. Using byte digitPins[] = {10, 11, 12, 13} on line 6 sets Arduino pin 10 as the
first digit pin, Arduino pin 11 to the second digit pin, and so on.
To print numbers with a decimal point, we set the second parameter in sevseg.setNumber(4999,
3) to three, which puts it three decimal places from the right most digit.
DISPLAYING SENSOR DATA
Now let’s read the temperature from a thermistor and display it on a 4 digit display.
Connect the circuit like this:

If you want to learn more about thermistors, check out our tutorial on using a thermistor with an
Arduino.
Once everything is connected, upload this code to the Arduino:

#include "SevSeg.h"
SevSeg sevseg;

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

void setup() {
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_CATHODE;

sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,


resistorsOnSegments);
}

void loop() {
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
T = T - 273.15;
T = (T * 9.0) / 5.0 + 32.0; //Comment out for Celsius

static unsigned long timer = millis();

if (millis() >= timer) {


timer += 300;
sevseg.setNumber(T, 2);
}

sevseg.refreshDisplay();
}

This will display the temperature in Fahrenheit on the 7-segment display. To display the temperature
in Celsius, comment out line 28.

By itself, the display will update every time the temperature changes even slightly. This creates an
annoying flickering. In order to deal with this, we introduce a timer mechanism, where we only read
the value from the thermistor every 300 milliseconds (lines 30 to 34).

The temperature variable “T” is printed to the display on line 35 with sevseg.setNumber(T, 2,
false).

Hopefully this article should be enough to get you started using seven segment displays. If you want
to display readings from other sensors, the example program above can easily be modified to do that.
If you have any questions or trouble setting up these circuits, feel free to leave a comment below.

You might also like