Hi,
I'm proud to announced that the project seems to work pretty good ! See picture attached.
I corrected some pieces of code (still not changed it with the boolean variables).
I have 1 mini-issue : when i start the arduino, the variable FC51 always goes to 1 by itself, so the counter starts at 1. When i press reset, it goes to 0 and everything becomes normal.
Do you know why the FC51 reacts like that ? Is there a workaround that it doesn't, or at least that the counter really starts at 0 and not 1 ? Maybe something to change in the code ?
Below the current code :
#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 = 12; // the pin that the pushbutton is attached to
const int PinButtonPlus1 = 8; // the pin that the pushbutton is attached to
const int PinButtonMinus1 = 9; // the pin that the pushbutton is attached to
const int PinButtonReset = 10; // the pin that the pushbutton is attached to
const int PinLED = 4; // 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 lastButtonStateReset = 0; // previous 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 - hundreds
lc.setChar(0,1,0,false); //to show 0 as initial number - tens
lc.setChar(0,2,0,false); //to show 0 as initial number - units
lc.setChar(0,3,0,false); //to show 0 as initial number - doesn't exist as 3-digit
}
void loop() {
// if FC51 changes the state, it does +1
ChangeStateFC51();
if (FC51 == 1) {
Plus1();
Update7segment(count);
Serial.print("FC51=");
Serial.println(FC51);
FC51=0;
}
// if PushButtonPlus1 is active, it does +1
PushButtonPlus1();
if (ButtonPlus1 == 1) {
Plus1();
Update7segment(count);
Serial.print("ButtonPlus1=");
Serial.println(ButtonPlus1);
ButtonPlus1=0;
}
// if PushButtonMinus1 is active, it does -1
PushButtonMinus1();
if (ButtonMinus1 == 1) {
Minus1();
Update7segment(count);
Serial.print("ButtonMinus1=");
Serial.println(ButtonMinus1);
ButtonMinus1=0;
}
// if PushButtonReset is active, it resets to 0
PushButtonReset();
if (ButtonReset == 1) {
Reset();
Update7segment(count);
Serial.print("ButtonReset=");
Serial.println(ButtonReset);
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 != lastButtonStateReset) {
// if the state has changed from low to high, increment the counter otherwise don't
if (buttonStateReset == HIGH) {
ButtonReset=1;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonStateReset = buttonStateReset;
}
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.setDigit(0,0,(byte)hundreds,false);
lc.setDigit(0,1,(byte)tens,false);
lc.setDigit(0,2,(byte)ones,false);
Serial.print("count : ");
Serial.println(count);
Serial.print("v : ");
Serial.println(v);
Serial.print("ByteHun : ");
Serial.println((byte)hundreds);
Serial.print("ByteTen : ");
Serial.println((byte)tens);
Serial.print("ByteOne : ");
Serial.println((byte)ones);
}
I now need to build the real protoype, and see if 1 IR sensor works with the plastic bags, or if need to put more, or change to the piezoelectric one.
Thanks again for your help,