Hello, I am working on a project, and whilst previously I have completed the two functions I am trying to program at different times, I now want to combine the code and complete them at the same time. When I combined the code I realised that the "delay()"'s I used would be a problem, as they stop the function of the other code. I am aware that I need to log "millis()" and time the sequences that way instead of using delay, however, I am unsure of how to implement this in this instance. Could anyone give me guidance/examples on how to replace all the delays in this code with functions that would not interfere with any other ongoing operations? Thank you.
#include "ArduinoMotorShieldR3.h"
ArduinoMotorShieldR3 md;
#include <avr/pgmspace.h>
#include <Wire.h>
#include <sfm3000wedo.h>
SFM3000wedo measflow(64);
int offset = 32768;
int scale = 120;
void setup() {
md.init();
Wire.begin();
delay(500);
Serial.begin(9600);
measflow.init();
Serial.println("Sensor initialized!");
}
void loop() {
// for reading SFM3300
float flowSFM = measflow.getvalue();
//if (flowSFM > 0) flowSFM = flowSFM + offset;
if (flowSFM > 0) flowSFM = 0;
else if (flowSFM < 0) flowSFM = flowSFM - offset;
flowSFM = flowSFM / scale;
flowSFM = flowSFM * 16.6666;
if (flowSFM > 1.00){
Serial.println(flowSFM);
Serial.print(millis());
Serial.print(", ");
delay(100);
}
if (flowSFM < 1.00){
Serial.println(0.00);
Serial.print(millis());
Serial.print(", ");
delay(100);
}
md.setM1Speed(350);
delay(250);
md.setM1Speed(0);
delay(500);
md.setM1Speed(-350);
delay(250);
md.setM1Speed(0);
delay(2000);
}
Robin2
September 23, 2020, 6:12pm
2
An Arduino can only do one thing at a time.
However because it is fast it can give the illusion of doing Several Things at a Time if the tasks are broken down into small elements so that it does (for example) 1% of task A followed by 1% of task B then another 1% of task A etc.
...R
blh64
September 23, 2020, 8:59pm
3
You can treat your motor like a state machine
#include "ArduinoMotorShieldR3.h"
ArduinoMotorShieldR3 md;
#include <avr/pgmspace.h>
#include <Wire.h>
#include <sfm3000wedo.h>
int state = -1;
unsigned long startTime;
unsigned long duration;
SFM3000wedo measflow(64);
int offset = 32768;
int scale = 120;
void setup() {
md.init();
Wire.begin();
delay(500);
Serial.begin(9600);
measflow.init();
Serial.println("Sensor initialized!");
}
void loop() {
// for reading SFM3300
float flowSFM = measflow.getvalue();
//if (flowSFM > 0) flowSFM = flowSFM + offset;
if (flowSFM > 0) flowSFM = 0;
else if (flowSFM < 0) flowSFM = flowSFM - offset;
flowSFM = flowSFM / scale;
flowSFM = flowSFM * 16.6666;
if (flowSFM > 1.00) {
Serial.println(flowSFM);
Serial.print(millis());
Serial.print(", ");
delay(100);
}
if (flowSFM < 1.00) {
Serial.println(0.00);
Serial.print(millis());
Serial.print(", ");
delay(100);
}
if ( millis() - startTime >= duration ) {
// advance to next state
state++;
if ( state > 3 ) state = 0;
startTime = millis();
switch (state) {
case 0:
md.setM1Speed(350);
duration = 250;
break;
case 1:
md.setM1Speed(0);
duration = 500;
break;
case 2:
md.setM1Speed(-350);
duration = 250;
break;
case 3:
md.setM1Speed(0);
duration = 2000;
break;
}
}
}
NOTE: your offset variable is declared as type 'int' which has the range -32,768 .. 32,767 yet you are trying to assign the value 32768.
Here ya go:
#include "ArduinoMotorShieldR3.h"
ArduinoMotorShieldR3 md;
#include <avr/pgmspace.h>
#include <Wire.h>
#include <sfm3000wedo.h>
SFM3000wedo measflow(64);
int offset = 32768;
int scale = 120;
void setup() {
md.init();
Wire.begin();
delay(500);
Serial.begin(9600);
measflow.init();
Serial.println("Sensor initialized!");
}
void loop() {
readSFM3000();
runMotor();
}
long sfm3000_time = 0;
void readSFM3000() {
if(millis()-sfm3000_time < 100)
return;
sfm3000_time = millis();
// for reading SFM3300
float flowSFM = measflow.getvalue();
//if (flowSFM > 0) flowSFM = flowSFM + offset;
if (flowSFM > 0) flowSFM = 0;
else if (flowSFM < 0) flowSFM = flowSFM - offset;
flowSFM = flowSFM / scale;
flowSFM = flowSFM * 16.6666;
if (flowSFM > 1.00){
Serial.println(flowSFM);
Serial.print(millis());
Serial.print(", ");
}
if (flowSFM < 1.00){
Serial.println(0.00);
Serial.print(millis());
Serial.print(", ");
}
}
long motor_time = 0;
int motorStep = 3; // force motor to move to step 0
void runMotor() {
switch(motorStep) {
case 0:
// duration of step 0
if(millis() - motor_time >= 250) {
// start step 1
md.setM1Speed(0);
motor_time = millis();
motorStep = 1;
}
break;
case 1:
// duration of step 1
if(millis() - motor_time >= 500) {
// start step 2
md.setM1Speed(-350);
motor_time = millis();
motorStep = 2;
}
break;
case 2:
// duration of step 2
if(millis() - motor_time >= 250) {
// start step 3
md.setM1Speed(0);
motor_time = millis();
motorStep = 3;
}
break;
case 3:
// duration of step 3
if(millis() - motor_time >= 2000) {
// start step 0
md.setM1Speed(350);
motor_time = millis();
motorStep = 0;
}
break;
}
}
Thank you, everyone, for the help. I have now been able to get it working.