Você está na página 1de 2

// Contador 0 a 9999 com Arduino com Driver serial MAX7219

// Professor Barrios
// https://www.youtube.com/watch?v=f9sehapr2UE

//contador simples 0-9999 Arduino


#include <LedControl.h> //Library for LED control with MAX7219XX
#include <Bounce2.h> //Library for Bounce od switches

/*
Pins of Arduino Nano for LedControl1:
Pin #9 is connected do DataIn (DIN)
Pin #11 is connected do CLK (CLK)
Pin #10 is connected do LOAD (CS)
*/

LedControl lc = LedControl(9, 11, 10, 1); //LedControl(dataPin, clkPin, csPin,


numDevices)

int k, lastTime, diffTime;


int a, b, c, d;
int a1, b1, c1, d1;

int pinStartStop = 4; //chave


bool statusSwitch1 = false; // variável que diz o estado da chave

Bounce SW1 = Bounce(); //rotina para tirar o ruido da chave

void setup() {
pinMode(7, OUTPUT); //pino de controle do MAX7219 dataPin
pinMode(8, OUTPUT); //pino de controle do MAX7219 clkPin
pinMode(9, OUTPUT); //pino de controle do MAX7219 csPin
pinMode(pinStartStop, INPUT_PULLUP);

//After setting up the button, setup the Bounce instance


SW1.attach(pinStartStop); //Sets the pin (Internal Pull-Up) & matches the
internal state
SW1.interval(3); //Ajusta o tempo do debounce em miiseconds

lc.shutdown(0, false); //The MAX7219 is in power-saving mode on startup, we have


to do a wakeup call
lc.setIntensity(0, 5); //Set the brightness of display between 0 and 15
lc.clearDisplay(0); //Clear the display

/*
parametriza a memória interna do MAX7219 para sentir o número
a ser decodificado como decimal de 4 dígitos
vai aparecer no display um número bdc
*/

//Mostra (?, dp, valor decimal)


lc.setDigit(0, 7, 0, false); //Unidade
lc.setDigit(0, 6, 0, false); //Dezena
lc.setDigit(0, 5, 0, false); //Centena
lc.setDigit(0, 4, 0, false); //Milhar
}

void loop() {
int pinStartStop = 4; //Start-Stop Pin
SW1.update();
if (SW1.fell()) {
statusSwitch1 = !statusSwitch1;
}

if (statusSwitch1 = true) {
//Exemplo: se k = 123
k = k + 1;
a = k / 1000; //Unidade, só pega o valor inteiro 123 /
1000 = 0, 123;
b = (k - a * 1000) / 100; //(123 - 0 * 1000) / 100 = 123 / 100 = 1,
23;
c = (k - a * 1000 - b * 100) / 10; //(123 - 0 * 1000 - 1 * 100) / 100 = 123 -
0 - 100 / 10 = 23 / 10 = 2;
d = k % 10; // 123 / 10 = 12, 3, onde a parte decimal é
3.

//Mostra (?, dp, valor decimal)


lc.setDigit(0, 7, a, false); //Mostra 1
lc.setDigit(0, 7, b, false); //Mostra 2
lc.setDigit(0, 7, c, false); //Mostra 3
lc.setDigit(0, 7, d, false); //Mostra 4

delay(99);
statusSwitch1 = false;
}
}

Você também pode gostar