I have an arduino code, the circuit contains 2 motors, 2 momentary push buttons, and a particulate matter sensor. Throughout the duration of the code, the particulate sensor needs to keep running. The 2 buttons set speed profiles for the motors and allow the motors to turn on for a certain time. After a button is pushed, the motor will be allowed to be powered for X amount of minutes. After the button is ran, there needs to be a delay that does not allow button input unless the particulate matter sensor has a certain output. How do I make this timer system work? The timer has 2 phases, 1 to set the duration of the motors on, and 1 to not allow input after motor is off unless sensor allows it.
#include <Seeed_HM330X.h>
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL_OUTPUT SerialUSB
#else
#define SERIAL_OUTPUT Serial
#endif
#include <Cytron_SmartDriveDuo.h>
#define IN1 6 // Arduino pin 4 is connected to MDDS30 pin IN1.
#define AN1 8 // Arduino pin 5 is connected to MDDS30 pin AN1.
#define AN2 7 // Arduino pin 6 is connected to MDDS30 pin AN2.
#define IN2 5 // Arduino pin 7 is connected to MDDS30 pin IN2.
Cytron_SmartDriveDuo smartDriveDuo30(PWM_INDEPENDENT, IN1, IN2, AN1, AN2);
char inChar;\
signed int speedLeft, speedRight;
int btn_toggle1 = 0;
int btn_toggle2 = 0;
bool btn1 = false;
bool btn2 = false;
int period = 20000;
int time_running = 0;
unsigned long delayStart = 0;
bool delayRunning = false;
int offset;
HM330X sensor;
uint8_t buf[30];
const char* str[] = {"sensor num: ", "PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
"PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
"PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
"PM1.0 concentration(Atmospheric environment,unit:ug/m3): ",
"PM2.5 concentration(Atmospheric environment,unit:ug/m3): ",
"PM10 concentration(Atmospheric environment,unit:ug/m3): ",
};
HM330XErrorCode print_result(const char* str, uint16_t value) {
if (NULL == str) {
return ERROR_PARAM;
}
SERIAL_OUTPUT.print(str);
SERIAL_OUTPUT.println(value);
return NO_ERROR;
}
/*parse buf with 29 uint8_t-data*/
HM330XErrorCode parse_result(uint8_t* data) {
uint16_t value = 0;
if (NULL == data) {
return ERROR_PARAM;
}
for (int i = 1; i < 8; i++) {
value = (uint16_t)data[i * 2] << 8 | data[i * 2 + 1];
print_result(str[i - 1], value);
}
if (data[13] >= 50||data[15] >= 50) {
digitalWrite(2, HIGH);
}
else {
digitalWrite(2, LOW);
}
return NO_ERROR;
}
HM330XErrorCode parse_result_value(uint8_t* data) {
if (NULL == data) {
return ERROR_PARAM;
}
for (int i = 0; i < 26; i++) {
//SERIAL_OUTPUT.print(data[i], HEX);
SERIAL_OUTPUT.print(" ");
if ((0 == (i) % 5) || (0 == i)) {
SERIAL_OUTPUT.println("");
}
}
uint8_t sum = 0;
for (int i = 0; i < 28; i++) {
sum += data[i];
}
if (sum != data[28]) {
SERIAL_OUTPUT.println("wrong checkSum!!");
}
SERIAL_OUTPUT.println("");
return NO_ERROR;
}
/*30s*/
void setup() {
pinMode(2, OUTPUT); // light
pinMode(13, OUTPUT);
pinMode(3, INPUT);//Button 1
pinMode(4, INPUT); //Button 2
delayStart = millis();
delayRunning = true;
SERIAL_OUTPUT.begin(115200);
delay(100);
offset = 0;
/* SERIAL_OUTPUT.println("Serial start");
if (sensor.init()) {
SERIAL_OUTPUT.println("HM330X init failed!!");
while (1);
}
*/
}
void loop() {
time_running = millis() - delayStart - offset;
if(delayRunning && (time_running >= period) && digitalRead(2) == LOW){ //20 mins running motor done
delayStart += period;
offset = 0;
SERIAL_OUTPUT.println("case1: timer over");
btn1 = false;
btn2 = false;
// digitalWrite(2, HIGH);
//speedLeft = 0;
//speedRight = 0;
}
else if(digitalRead(2) == HIGH){// resets motor timer
time_running = 0;
SERIAL_OUTPUT.println("timer reset");
delayRunning = true;
}
if (sensor.read_sensor_value(buf, 29)) {
SERIAL_OUTPUT.println("HM330X read result failed!!");
}
parse_result_value(buf);
parse_result(buf);
SERIAL_OUTPUT.println("");
btn_toggle1 = digitalRead(3);
btn_toggle2 = digitalRead(4);
if (btn_toggle1 == HIGH && btn_toggle2 == LOW && btn2 == false && offset == 0) {
btn1 = true;
offset += time_running;
}// toggles btn1
if (btn_toggle2 == HIGH && btn_toggle1 == LOW && btn1 == false && offset == 0) {
btn2 = true;
offset += time_running;
} // toggles btn2
if (btn1 == true) { //Case1: btn1 is toggled On and btn2 is toggled Off
SERIAL_OUTPUT.println("button 1 has been pressed");
speedLeft = 60;
speedRight = 60;
// smartDriveDuo30.control(speedLeft, speedRight);
}
else if (btn2 == true) { //Case2: btn2 is toggled On and btn1 is toggled off
SERIAL_OUTPUT.println("button 2 has been pressed");
speedLeft = 100;
speedRight = 100;
// smartDriveDuo30.control(speedLeft, speedRight);
}
else{
speedLeft = 0;
speedRight = 0;
}
// smartDriveDuo30.control(speedLeft, speedRight);
// }
SERIAL_OUTPUT.print("delayStart: ");
SERIAL_OUTPUT.println(delayStart);
SERIAL_OUTPUT.print("millis: ");
SERIAL_OUTPUT.println(millis());
SERIAL_OUTPUT.print("offset: ");
SERIAL_OUTPUT.println(offset);
SERIAL_OUTPUT.print("time_running: ");
SERIAL_OUTPUT.println(time_running);
delay(2000);
}