Demo entry 6868093

code

   

Submitted by anonymous on Aug 19, 2019 at 16:50
Language: Arduino. Code size: 3.6 kB.

#include <LiquidCrystal.h>

int sensorPin1 = A0;
int sensorPin2 = A1;

const int buttonPin = 6;
int buttonPushCounter = 0;
int lastButtonState = 0;
int buttonState = 0;
void scrolllnFromRight (int line, char str[]);
void scrollFlowRate (char str1[], char str2[], double a);

int sensorValue1 = 0;
int sensorValue2 = 0;
int threshold1 = 0;
int threshold2 = 0;
int createthreshold(int b);
int judge(int a, int c);
double Length = 10.0;
double flowrate = 0 ;
double avgVelocity = 0;
unsigned long startTime;
unsigned long endTime;
double timeTaken;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  lcd.clear();
  lcd.home();
  lcd.print("Ready?");   // lcd prints "Ready"
  lcd.setCursor(0,1);
  

    while (buttonState == lastButtonState) {
      scrolllnFromRight(1, "Press & hold button");
      buttonState = digitalRead(buttonPin);
    }
    buttonPushCounter++;

  if (buttonState != lastButtonState){
    if (buttonPushCounter % 2 == 1){
      
        lcd.clear();
        lcd.home();
        lcd.print("Please hold...");
        threshold1 = createthreshold(analogRead(sensorPin1));
        threshold2 = createthreshold(analogRead(sensorPin2)); 
        delay(50);
      // loops until both sensors having readings above 400 
      
      lcd.clear();
      lcd.home();
      lcd.print("Take Vitamin"); // lcd prints "Take Vitamin"

      sensorValue1 = analogRead(sensorPin1);                          
      while(sensorValue1 > (threshold1 - 30)){                 // loops until first sensor's reading goes below 400
        sensorValue1 = analogRead(sensorPin1);
      }
      startTime = millis(); // takes note of start time

      sensorValue2 = analogRead(sensorPin2);
      while(sensorValue2 > (threshold2 - 30)) {
        sensorValue2 = analogRead(sensorPin2);
      } // loops until second sensor's reading goes below 400
      endTime = millis(); // takes note of end time
      
      timeTaken=endTime-startTime;
      avgVelocity = Length / (2 * timeTaken / 1000.0);
      flowrate = avgVelocity * 0.0625 * PI; // calculates flowrate
      flowrate = flowrate + 6.0;

      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Press to reset");
      
      /*lcd.print("Flow Rate: ");
      lcd.print(flowrate, 2);
      lcd.print(" ml/s"); // lcd prints flowrate value*/
      /*lcd.setCursor(0,1);
      lcd.print(startTime);
      lcd.print(" ");
      lcd.print(endTime);
      delay(5000);*/
    } 
    
    while (buttonPushCounter % 2 == 1) {
      scrollFlowRate("Flow Rate: ", "ml/s", flowrate);
      if (digitalRead(buttonPin) == HIGH) {
        buttonPushCounter++;
      }
    }
  }
  lastButtonState = buttonState; 
  delay(100);
}



int createthreshold( int b){
  return b;
}

int judge(int a, int c){
  if(a < c){
    return 0;
  }
  else return 1;
}

void scrolllnFromRight (int line, char str[]) {
  int i = 0;
  int j = 0;
  int k = 0;
  
  i = strlen(str);
  
  for (j = 0; j <= (i-16); j++) {
    lcd.setCursor(0,line);
    for (k = 0; k <= 15; k++) {
      lcd.print(str[j+k]);
    }
    delay(400);
  }
}

void scrollFlowRate (char str1[], char str2[], double a) {
  int length1 = 0;
  int i = 0;
  int j = 0;
  int k = 11;

  length1 = strlen(str1);

  for (i = 0; i < 5; i++) {
    lcd.setCursor(0,0);
    for (j = 0; j < k; j++) {
      lcd.print(str1[i+j]);     
    }
    k--;
    lcd.print(a, 2);
    lcd.print(str2);
    delay(400);
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).