Hi,
Below the code i just finished. The "verify" buttons says it's correct, but i don't have yet all components to test it in real.
Can you already check it and tell me if you see any weird/useless/wrong thing inside ? Or any comment on how to make it better already ?
I put sometimes some comments when i was not sure about something. You'll see
Thank you very much for your support,
#include "LedControl.h"
LedControl lc = LedControl(4,5,6,4);
// ----- Code for Change State for FC-51
// this constant won't change:
const int PinFC51 = 2; // the pin that the pushbutton is attached to
const int PinButtonPlus1 = 3; // the pin that the pushbutton is attached to
const int PinButtonMinus1 = 4; // the pin that the pushbutton is attached to
const int PinButtonReset = 5; // the pin that the pushbutton is attached to
const int PinLED = 13; // the pin that the LED is attached to
// Variables will change:
int buttonStateFC51 = 0; // current state of the FC51
int lastButtonStateFC51 = 0; // previous state of the FC51
int buttonStatePlus1 = 0; // current state of the button
int lastButtonStatePlus1 = 0; // previous state of the button
int buttonStateMinus1 = 0; // current state of the button
int lastButtonStateMinus1 = 0; // previous state of the button
int buttonStateReset = 0; // current state of the button
int FC51 = 0;
int ButtonPlus1 = 0;
int ButtonMinus1 = 0;
int ButtonReset = 0;
// ----- Code for Max7219 + LEDs
int count = 0;
void setup()
{
// ----- Code for Change State for FC-51
// initialize the button pin as an input:
pinMode(PinFC51, INPUT);
pinMode(PinButtonPlus1, INPUT);
pinMode(PinButtonMinus1, INPUT);
pinMode(PinButtonReset, INPUT);
// initialize the LED as an output:
pinMode(PinLED, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
// ----- Code for Max7219 + LEDs
lc.shutdown(0,false); //to activate the led's
lc.setIntensity(0,5); //to define the brightness of the led's
lc.clearDisplay(0); //to cancel all info on the led's
lc.setDigit(0,0,0,false); //to show 0 as initial number
lc.setChar(0,1," ",false); //to show 0 as initial number
lc.setChar(0,2," ",false); //to show 0 as initial number
lc.setChar(0,3," ",false); //to show 0 as initial number
}
void loop() {
// if FC51 changes the state, it does +1
ChangeStateFC51();
if (FC51 == 1) {
Plus1();
Update7segment(count);
FC51=0;
}
// if PushButtonPlus1 is active, it does +1
PushButtonPlus1();
if (ButtonPlus1 == 1) {
Plus1();
Update7segment(count);
ButtonPlus1=0;
}
// if PushButtonMinus1 is active, it does -1
PushButtonMinus1();
if (ButtonMinus1 == 1) {
Minus1();
Update7segment(count);
ButtonMinus1=0;
}
// if PushButtonReset is active, it resets to 0
PushButtonReset();
if (ButtonReset == 1) {
Reset();
Update7segment(count);
ButtonReset=0;
}
}
void ChangeStateFC51() {
// read the FC51 input pin:
buttonStateFC51 = digitalRead(PinFC51);
// compare the buttonState to its previous state
if (buttonStateFC51 != lastButtonStateFC51) {
// if the state has changed from low to high, increment the counter otherwise don't
if (buttonStateFC51 == HIGH) {
FC51=1;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonStateFC51 = buttonStateFC51;
}
void PushButtonPlus1() {
// read the pushbutton input pin:
buttonStatePlus1 = digitalRead(PinButtonPlus1);
// compare the buttonState to its previous state
if (buttonStatePlus1 != lastButtonStatePlus1) {
// if the state has changed from low to high, increment the counter otherwise don't
if (buttonStatePlus1 == HIGH) {
ButtonPlus1=1;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonStatePlus1 = buttonStatePlus1;
}
void PushButtonMinus1() {
// read the pushbutton input pin:
buttonStateMinus1 = digitalRead(PinButtonMinus1);
// compare the buttonState to its previous state
if (buttonStateMinus1 != lastButtonStateMinus1) {
// if the state has changed from low to high, increment the counter otherwise don't
if (buttonStateMinus1 == HIGH) {
ButtonMinus1=1;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonStateMinus1 = buttonStateMinus1;
}
void PushButtonReset() {
// read the pushbutton input pin:
buttonStateReset = digitalRead(PinButtonReset);
// compare the buttonState to its previous state
if (buttonStateReset = 1) {
ButtonReset=1;
}
// Delay a little bit to avoid bouncing
delay(50);
}
void Plus1()
{
if (count=999) {
count = 0;
} else {
count = count + 1;
}
}
void Minus1()
{
if (count<=0) {
count = 0;
} else {
count = count - 1;
}
}
void Reset()
{
count = 0;
}
void Update7segment(int v) {
int ones;
int tens;
int hundreds;
//set the different numbers
ones=v%10;
v=v/10;
tens=v%10;
v=v/10;
hundreds=v;
//print the numbers digit by digit
lc.shutdown(0,true); //to deactivate the calculation of the led's : usefull ??
lc.setDigit(0,2,(byte)hundreds,false);
lc.setDigit(0,1,(byte)tens,false);
lc.setDigit(0,0,(byte)ones,false);
lc.shutdown(0,false); //to activate all led's at once : usefull ??
}