UPDATE
The code include my 5 loads + FLS, I'm testing the code with 2 LEDs (load3 and load4) and the FLS.
Link for MOSFET module: SparkFun MOSFET Power Control Kit - COM-12959 - SparkFun Electronics
I got help from that post: understanding attachinterrupt in a pulse counter - Programming Questions - Arduino Forum
The code here is only the meaningful part and those I think I need to fix:
push button is connected to digital pin2
FLS is connected to digital pin3
//all the physical connection of the push button, loads and FLS
int buttonState; // the current reading from the switch
unsigned long tank1_max = 500; // capacity of maximum mL untreated water tank
unsigned long tank2_max = 700; // capacity of maximum mL water in bottle
//------------ VARIABLES (will change)---------------------
uint8_t state = 0;
uint8_t button_state = 0;
uint8_t fls_state = 0;
long button_timer;
int lastButtonState = 0; // previous state of the button
int button_was_pressed = 0; // the current reading from the button
volatile unsigned long pulse_counter; // this integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
unsigned long copy_count = 0; // accumulates the interrupt count, after
unsigned long speed = 0;
unsigned long tank1 = 0;
unsigned long tank2 = 0;
int time;
//========================================
void setup() {
Serial.begin(9600);
Serial.println("Machine is turn on"); // so we know what sketch is running
pinMode(button, INPUT);
pinMode(led_button, OUTPUT);
//pinMode(all the loads are set to, OUTPUT);
pinMode(flow_pin, INPUT);
attachInterrupt(1, isrCount, RISING); // interrupt programme when signal to pin 3 detected (1 = pin 3)
// count when rising, call isrCount function when happens (isr = interupt service routine)
}
//========================================
void loop() {
check_button();
check_state_machine();
pulse_counter = 0;
measure_pulses();
button_was_pressed = LOW;
}
//========================================
void check_button() {
switch (button_state)
{
case 0:
if (digitalRead(button) == HIGH)
{
button_was_pressed = HIGH;
button_state = 1;
button_timer = millis();
}
break;
case 1:
if (digitalRead(button) == HIGH)
button_timer = millis();
else if ((millis() - button_timer) > 25) // time after bouncing finished
{
button_state = 0;
}
break;
}
}
//========================================
void check_state_machine() {
Serial.print("check_state_machine :: state = ");
Serial.println(state);
switch (state)
{
case 0:
digitalWrite(led_button, LOW);
digitalWrite(load3, LOW);
digitalWrite(load2, LOW);
digitalWrite(load1, LOW);
digitalWrite(load4, LOW);
digitalWrite(load5, LOW);
if (button_was_pressed == HIGH)
{
digitalWrite(led_button, HIGH);
digitalWrite(load3, HIGH);
digitalWrite(load2, HIGH);
digitalWrite(load1, HIGH);
if (copy_count > 0) {
tank1++;
Serial.print("pulses: ");
Serial.println(copy_count);
Serial.print("tank1: ");
Serial.println(tank1);
if (copy_count >= (tank1_max / (1000 / 3480))) // (500mL(tank1_max) / (1000mL per min/ 58*60 pulse signal per min)) = tank1
{
state = 1;
}
}
}
else if (button_was_pressed == HIGH) // turning off machine - emo state
{
digitalWrite(load1, LOW);
digitalWrite(load2, LOW);
digitalWrite(load3, LOW);
digitalWrite(led_button, LOW);
}
break;
case 1:
digitalWrite(load4, HIGH);
digitalWrite(load5, HIGH);
digitalWrite(load3, LOW);
if (copy_count > 0) {
tank2_max++;
Serial.print("pulses: ");
Serial.println(copy_count);
Serial.print("tank2_max: ");
Serial.println(tank2_max);
if (copy_count >= (tank2_max / (1000 / 3480))) { // (700mL(tank2_max) / (1000mL per min/ 58*60 pulse signal per min)) = tank2
state = 2;
}
}
if (button_was_pressed == HIGH) // turning off machine - emo state
{
digitalWrite(load1, LOW);
digitalWrite(load2, LOW);
digitalWrite(load4, LOW);
digitalWrite(load5, LOW);
digitalWrite(led_button, LOW);
state = 0;
}
break;
case 2: // turning off machine
digitalWrite(load5, LOW);
digitalWrite(load4, LOW);
digitalWrite(load1, LOW);
digitalWrite(load2, LOW);
digitalWrite(led_button, LOW);
state = 0;
}
}
//========================================
void measure_pulses() {
Serial.print("fls_state :: pulse counter = ");
Serial.println(pulse_counter);
switch (fls_state)
{
case 0:
time = millis();
fls_state = 1;
break;
case 1: // disable interrupts, make copy of pulse_counter, reenable interrupts
noInterrupts(); // don't interrrupt to count on pin while copying count
copy_count = pulse_counter;
interrupts(); // switch on interrrupts again
break;
}
}
//========================================
void isrCount() {
pulse_counter++;
}
Now there are 3 situation:
- Blowing air in the FLS the serial monitor shows the number of the pulses.
- Pushing the button LED1 (load1) is turning on, no expression in the serial monitor, continues pressing doesn't change the time illumination of the led.
- Blowing air in the FLS the serial monitor shows the number of the pulses, pushing the button while I see the pulses running the code stop.
In the loop section, do I need to do measure_pulses function before the check_state_machine function or first the check_state_machine function?
As I mentioned earlier, I want to do when I push the button the FLS will measure the quantity of the water in the background (of the code) for each tank, after tank1 is full I want to fill tank2.
What am I missing?