Question: I need help with my arduino code. I have 2 motors, 2 button inputs and 1 particulate matter sensor . I need to set the

I need help with my arduino code. I have 2 motors, 2 button inputs and 1 particulate matter sensor. I need to set the motors up to only run for a certain duration after the button is pressed. The button sets the speed profile for the motor (2 buttons =2 different speed profiles). After the motors run according to the button input, the code needs to enter a delay phase where button input is no longer acceptable until the delay is over. During this delay, the particulate matter sensor needs to continuously scan and IF the value of the sensor exceeds a certain value, it will override the delay and allow the motors to go on. In my code, if the button is not pressed exactly at the time the delay is over, it will not work until the new delay time is over. For example if I run the motor for 10 seconds, it enters a 10 second delay. If I press the button exactly after 10 seconds, it will run the motor again. If I press the button lets say 14 seconds after, it will wait an additional 10 seconds before accepting input resulting in a 24 second delay. PLEASE HELP! I have attached the loop of my code, the other functions outside the loop only retrieve the particulate matter value which is indicated as pinOut 2:
void loop(){
if (sensor.read_sensor_value(buf,29)){
SERIAL_OUTPUT.println("HM330X read result failed!!");
}
parse_result_value(buf);
parse_result(buf);
SERIAL_OUTPUT.println("");
if (digitalRead(13)== LOW){
btn_toggle1= digitalRead(3);
btn_toggle2= digitalRead(4);
if (btn_toggle1== HIGH && btn_toggle2== LOW && btn2== false){
btn1= true;
}// toggles btn1
if (btn_toggle2== HIGH && btn_toggle1== LOW && btn1== false){
btn2= true;
}// toggles btn2
}
if (!delayPhase){
if (btn1== true){
motorStart = millis();
Serial.println("button 1 has been pressed");
speedLeft =60;
speedRight =60;
btn1= false;
btn2= false;
digitalWrite(13, HIGH);
// Activate motor with profile 1
} else if (btn2== true){
motorStart = millis();
Serial.println("button 2 has been pressed");
speedLeft =100;
speedRight =100;
btn1= false;
btn2= false;
digitalWrite(13, HIGH);
// Activate motor with profile 2
}
if (millis()- motorStart < motorDuration){
Serial.println("Motors On");
smartDriveDuo30.control(speedLeft, speedRight);
} else {
// Turn off motor
Serial.println("Motors Off");
delayPhase = true;
delayStart = millis();
speedLeft =0;
speedRight =0;
smartDriveDuo30.control(speedLeft, speedRight);
digitalWrite(13, HIGH);
btn1= false;
btn2= false;
}
} else {
// In delay phase
if (digitalRead(2)== HIGH){
//delayPhase = false;
speedLeft =60;
speedRight =60;
smartDriveDuo30.control(speedLeft, speedRight);
digitalWrite(13, HIGH);
} else if (millis()- delayStart > delayDuration){
delayPhase = false; // End delay phase after Y minutes
Serial.println("Delay Reset");
digitalWrite(13, LOW);
}
}
delay(2000);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!