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 motors, button inputs and 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 buttons 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 seconds, it enters a second delay. If I press the button exactly after seconds, it will run the motor again. If I press the button lets say seconds after, it will wait an additional seconds before accepting input resulting in a 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 :
void loop
if sensorreadsensorvaluebuf
SERIALOUTPUT.printlnHMX read result failed!!";
parseresultvaluebuf;
parseresultbuf;
SERIALOUTPUT.println;
if digitalRead LOW
btntoggle digitalRead;
btntoggle digitalRead;
if btntoggle HIGH && btntoggle LOW && btn false
btn true;
toggles btn
if btntoggle HIGH && btntoggle LOW && btn false
btn true;
toggles btn
if delayPhase
if btn true
motorStart millis;
Serial.printlnbutton has been pressed";
speedLeft ;
speedRight ;
btn false;
btn false;
digitalWrite HIGH;
Activate motor with profile
else if btn true
motorStart millis;
Serial.printlnbutton has been pressed";
speedLeft ;
speedRight ;
btn false;
btn false;
digitalWrite HIGH;
Activate motor with profile
if millis motorStart motorDuration
Serial.printlnMotors On;
smartDriveDuocontrolspeedLeft speedRight;
else
Turn off motor
Serial.printlnMotors Off";
delayPhase true;
delayStart millis;
speedLeft ;
speedRight ;
smartDriveDuocontrolspeedLeft speedRight;
digitalWrite HIGH;
btn false;
btn false;
else
In delay phase
if digitalRead HIGH
delayPhase false;
speedLeft ;
speedRight ;
smartDriveDuocontrolspeedLeft speedRight;
digitalWrite HIGH;
else if millis delayStart delayDuration
delayPhase false; End delay phase after Y minutes
Serial.printlnDelay Reset";
digitalWrite LOW;
delay;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
