Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

How do i make the display count? #3

Closed
Guacamole-1 opened this issue May 26, 2020 · 2 comments
Closed

How do i make the display count? #3

Guacamole-1 opened this issue May 26, 2020 · 2 comments

Comments

@Guacamole-1
Copy link

Guacamole-1 commented May 26, 2020

how do i make the display count?
when i add delay() it just starts flashing

@Guacamole-1 Guacamole-1 changed the title Im having a error when running example . May 26, 2020
@Guacamole-1 Guacamole-1 reopened this May 26, 2020
@Guacamole-1 Guacamole-1 changed the title . How do i make the display count? May 26, 2020
@CTXz
Copy link
Contributor

CTXz commented May 27, 2020

Hi,

The display is multiplexed, meaning that we're not actually turning on all digits at once, but toggle each digit on and off to the point that the human eye perceives them as all being on at the same time.

Since the delay function halts the entire program, the Arduino won't be able to switch between digits fast enough. This is exactly why the d.print function must be looped at all times!

To count upwards, you must work with timestamps instead of delays, ie. you'll need to protothread.

Something like this would count from 0 to 100:

#include <Arduino.h>

/* Include this library */
#include "Segment.h"
#include "Digit.h"
#include "Display.h"

const byte digit_pins[4] {6, 9, 10, 12}; // From com1 - com4
const byte segment_pins[7] {7, 11, 2, 4, 5, 8, 13}; // From a - g (abc...g)
const byte dp_pin = 3;

Display d(digit_pins, segment_pins, dp_pin);

void setup() {
  for (int i = 0; i <= A5; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() 
{
    // Count from 0 to 100
    for (int i = 0; i <= 100; i++)
    {
        unsigned long timestamp = millis() + 1000; // Set timestamp 1000ms in the future

        while (millis() < timestamp)
        {
            d.print(String(i).c_str()); // Loop number until 1 second has passed
        }
    }
}

Note, I haven't tested the code.

@Guacamole-1
Copy link
Author

Hi,

The display is multiplexed, meaning that we're not actually turning on all digits at once, but toggle each digit on and off to the point that the human eye perceives them as all being on at the same time.

Since the delay function halts the entire program, the Arduino won't be able to switch between digits fast enough. This is exactly why the d.print function must be looped at all times!

To count upwards, you must work with timestamps instead of delays, ie. you'll need to protothread.

Something like this would count from 0 to 100:

#include <Arduino.h>

/* Include this library */
#include "Segment.h"
#include "Digit.h"
#include "Display.h"

const byte digit_pins[4] {6, 9, 10, 12}; // From com1 - com4
const byte segment_pins[7] {7, 11, 2, 4, 5, 8, 13}; // From a - g (abc...g)
const byte dp_pin = 3;

Display d(digit_pins, segment_pins, dp_pin);

void setup() {
  for (int i = 0; i <= A5; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() 
{
    // Count from 0 to 100
    for (int i = 0; i <= 100; i++)
    {
        unsigned long timestamp = millis() + 1000; // Set timestamp 1000ms in the future

        while (millis() < timestamp)
        {
            d.print(String(i).c_str()); // Loop number until 1 second has passed
        }
    }
}

Note, I haven't tested the code.

thank you that worked :)

@CTXz CTXz closed this as completed May 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants