// Variables to store the state and timing
int state = 0;
unsigned long previousMillis = 0;
const unsigned long interval = 1000;
unsigned long previousMillis30Sec = 0;
const unsigned long interval30Sec = 1000*30; // 30 sec
int meterCount=0;
// Pin for the signal
const int signalPin = 2;
// Variables to store the previous and current state of the signal
int currentState[12] = {LOW};
int previousState[12] = {LOW};
// Flag to indicate a rising edge
volatile bool risingEdgeDetected[12] = {false};
volatile bool fallingEdgeDetected[12] = {false};
void setup() {
// Initialize the digital pin as an input
pinMode(signalPin, INPUT);
// Enable serial communication
Serial.begin(9600);
}
void loop() {
//doTask();
checkInputState(2);
switch (state) {
case 0:
//
Serial.println("Task1");
//checkInputState(2);
break;
case 1:
//
Serial.println("Task2");
break;
case 2:
//
Serial.println("Task3");
break;
default:
// Handle any other states or errors here
break;
}
state++;
if (state>2){
state=0;
}
// Get the current time
unsigned long currentMillis = millis();
// Check if the desired interval has elapsed
if (currentMillis - previousMillis30Sec >= interval30Sec) {
// Do job after delay 30 sec
//Send Counter message to server
//TODO:
//digitalWrite(12, HIGH);
// Update the previousMillis to the current time
previousMillis30Sec = currentMillis;
}
//Serial.print("Test");
// Add any other code or tasks you want to run in the loop
delay(100);
}
// Function to handle a rising edge
void checkInputState(int pin) {
// Read the current state of the signal
currentState[pin] = digitalRead(pin);
// Check for rising edge
if (currentState[pin] == HIGH && previousState[pin] == LOW) {
// Rising edge detected
//risingEdgeDetected[pin]=true;
digitalWrite(12, HIGH);
meterCount++;
Serial.println("<<ON>>");
Serial.print("Meter counter: ");
Serial.println(meterCount);
// Update the previous state with the current state
previousState[pin] = HIGH;
}
// Check for falling edge
if (currentState[pin] == LOW && previousState[pin] == HIGH) {
// Falling edge detected
//fallingEdgeDetected[pin]=true;
digitalWrite(12, LOW);
Serial.println("<<OFF>>");
// Update the previous state with the current state
previousState[pin] = LOW;
}
}
void doTask() {
unsigned long currentMillis = millis();
// Perform different tasks based on the current state
switch (state) {
case 0:
// Task 1: Read input pin
//Check pin state
//for (int i = 0; i < 12; i++) {
//checkInputState(i);
//}
//checkInputState(2);
//Serial.println("Read");
// If task 1 is complete, move to the next state
if (currentMillis - previousMillis >= interval) {
state = 1;
previousMillis = currentMillis;
}
break;
case 1:
// Task 2: Write to SD-Card and send
//Check pin state
//for (int i = 0; i < 12; i++) {
//Rising edge
//if (risingEdgeDetected[2] == true) {
//if (i==2){
//digitalWrite(12, HIGH);
//risingEdgeDetected[2] == false;
//}
//}
//Falling edge
//if (fallingEdgeDetected[2] == true) {
//if (i==2){
//digitalWrite(12, LOW);
//fallingEdgeDetected[2] == false;
//}
//}
//}
//Serial.println("Write");
// If task 2 is complete, move to the next state
if (currentMillis - previousMillis >= interval) {
state = 2;
previousMillis = currentMillis;
}
break;
case 2:
// Task 3:
//Serial.println("Done");
// If task 3 is complete, move to the next state
if (currentMillis - previousMillis >= interval) {
state = 0; // Go back to the initial state
previousMillis = currentMillis;
}
break;
default:
// Handle any other states or errors here
break;
}
}