I need help with my code. I am currently working on a simulation for a ventilator. I am working with Millis to constantly read my sensor, but I am doing something wrong. This is what I have for the code.
I need it to constantly read the sensor so it can execute the loop correctly.
Any inputs or ideas, please?
#include <Stepper.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd (8, 9, 10, 11, 12, 13);
int enablePin = 7; //PWM2 which is B 1st cable Yellow
int in1Pin = A0; //2nd cable Green cable
int in2Pin = A1; //Brown cable
int enablePin2 = A3; //PWM1 which is A Purple Cable
int analogInPin = A4; //potentiometer
int sensorValue = 0;
int outputValue = 0;
//int state = 0;
//Following are for the sensors
int triggerPin = 6;//Sensor A (Bottom)
int echoPin = 5;//Sensor A (Bottom)
int echoPin2 = 4;//Second Sensor Top(B Sensor)
int triggerPin2 = 3; //Second Sensor Top(B Sensor)
int cm, cm1;
int buzzer = A5;
int light = 2;
Stepper stepper1(200, in1Pin, in2Pin); //200 is full revolution
const long eventTime_1 = 1000;
//const long eventTime_2 = 2000;
unsigned long previousTime_1 = 0;
//unsigned long previousTime_2 = 0;
unsigned long currentTime = 0;
/*---------------------------------------*/
//Toggles the input from the trigger to the echo pin
long readSensorA(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
long readSensorB(int triggerPin2, int echoPin2)
{
pinMode(triggerPin2, OUTPUT);
digitalWrite(triggerPin2, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin2, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin2, LOW);
pinMode(echoPin2, INPUT);
return pulseIn(echoPin2, HIGH);
}
/*------------------------------------*/
/*/////VOID SETUP//////*/
void setup()
{
lcd.begin(16,2);
Serial.begin(96000);
Serial.println("------------");
//lcd.begin(16,2);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(enablePin2, OUTPUT);
pinMode(analogInPin, INPUT); //potentiometer
pinMode(light, OUTPUT);
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin2, HIGH);
stepper1.setSpeed(100);
}
/*------------------------------------*/
/*/////VOID LOOP//////*/
void loop()
{
//Comparing difference between current time and previous time in 1 sec
//currentTime is always updating
currentTime = millis();
if(currentTime - previousTime_1 >= eventTime_1)
{
readSensorA(6,5);
readSensorB(3,4);
previousTime_1 = currentTime;
}
checkSensor();
spinMotor();
sensorLOW();
sensorHIGH();
}
void checkSensor(){
cm = (0.01723 * readSensorA(6,5)); //(3,2) is for trigger pin, echo pin
cm1 = 0.01723 * readSensorB(3,4); //(3,2) is for trigger pin, echo pin
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1024, 250, 751);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(outputValue);
lcd.print("mL");
lcd.setCursor(0,1);
Serial.print("Sensor A: ");
Serial.print(cm);
Serial.print("cm ");
Serial.println();
delay(10);
Serial.print("Sensor B: ");
Serial.print(cm1);
Serial.print("cm ");
Serial.println();
delay(10);
float AirSpeed=(0.3/2)*((cm/10000+cm1/10000)/(cm/10000*cm1/10000));
Serial.print("Air flow =");
Serial.print(AirSpeed);
Serial.println(" m/s");
Serial.println("-----------------------");
delay(1000);
previousTime_1 = currentTime;
}
}
void spinMotor(){
if (sensorValue>outputValue)
{
// Turn the stepper 100 steps which means 180 degrees
stepper1.step(100);
// Wait half second
delay(100);
}
if (sensorValue<outputValue){
// Turn the stepper 100 steps back which means 180 degrees
stepper1.step(-100);
// Wait half second
delay(100);
}
sensorValue=outputValue;
}
void sensorLOW()
{
if ((0.01723 * readSensorA(6, 5) < 105) && (0.01723 * readSensorB(3, 4) < 105))
{
lcd.print("Breathe:");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Inhale 2 sec");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Inhale 3 sec");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Inhale 4 sec");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Hold: ");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Exhale 4 sec");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Exhale 5 sec");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Exhale 6 sec");
delay(1000);
}
}
void sensorHIGH()
{
if ((0.01723 * readSensorA(6, 5) > 105) && (0.01723 * readSensorB(3, 4) > 105))
{
digitalWrite(light, HIGH);
tone(buzzer, 659,1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ERROR:");
lcd.setCursor(0,1);
lcd.print("No Airflow!!");
delay(2000);
}
else {
digitalWrite(buzzer, LOW);
digitalWrite(light, LOW);
noTone(buzzer);
}
}