Ok sono riuscito nel mio intento, contare fino a 90, ma per curiosità ho provato a inserire anche la variabile per le centinaia in modo da poter contare anche oltre il 99, ma al nr. 100 appare la scritta 1a0 e a 110 appare 1b0 e così via senza però visualizzare 100...110...120... Cosa c'è di sbagliato?
//State change detection (edge detection)
//We always have to include the library
#include "LedControl.h"
/*
Now we need a LedControl to work with.
pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD
We have only a single MAX72XX.
*/
LedControl lc=LedControl(12,11,10,1);
/* The circuit:
- buttonpin attached to pin 2 from +5V
- buttonreset attached to pin 4 from +5V
- 10 kilohm resistor attached to pin 2&4 from ground
*/
// this constant won't change:
const int buttonPin = 2;// the pin that the pushbutton is attached to
const int buttonReset = 4;// the pin that the buttonreset is attached to
// Variables will change:
int contatore = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int unita = 0;
int decine = 0;
int centinaia = 0;
void setup(){
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
pinMode(buttonReset, INPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
contatore++;
unita=contatore%10;
decine=contatore/10;
centinaia=contatore/100;
lc.setDigit(0,2,(byte)centinaia,false);
lc.setDigit(0,1,(byte)decine,false);
lc.setDigit(0,0,(byte)unita,false);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// if the buttonreset is pressed reset the buttonpushcounter
if (digitalRead(buttonReset) == HIGH){
(contatore = 0);
lc.clearDisplay(0);
}
}