Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15514

Multi tasking structre for my arduino sketch

$
0
0

I have a digital scale project to measure both weight and height in a addition to keypad and a buzzer to enter the the some data
The point that my scale should work in 2 modes
the first mode is when we need to measure weight only when a person step on the load cell unit.
the second mode is when we need to measure weight and height and entering some data , so the person has to press any key to start that mode.
I was able to make the sketch to achieve the second mode but it wasn't possible to make a keypad buzz for every key press, nor I was able to achieve the first mode as both of them need restructuring my sketch to include machine states and I'm not sure what is the best restructuring to solve both problems
here is my code

#include <LiquidCrystal_I2C.h>
#include <Wire.h> 
#include <Keypad.h>
#include <ezBuzzer.h>
#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define LOADCELL_DOUT_PIN  11
#define LOADCELL_SCK_PIN  12
HX711 scale;
int trigPin = 12;    // TRIG pin
int echoPin = 11;    // ECHO pin
float duration_us, distance_cm;
LiquidCrystal_I2C lcd_1(0x27, 16, 2);
const int BUZZER_PIN = 10;
const int ROW_NUM    = 4; // four rows
const int COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};      // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad customKeypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
ezBuzzer buzzer(BUZZER_PIN); // create ezBuzzer object that attach to a pin;

void displayscreen(){   // screen saver 
  
   lcd_1.setCursor(4,0);
   lcd_1.print("*iScale*");
   lcd_1.setCursor(2 ,1);
   lcd_1.print("!Your Scale!");    
   lcd_1.scrollDisplayLeft(); 
   delay(200);  
   lcd_1.scrollDisplayRight();  
  
 }

void setup() {
  
  Serial.begin(4800);
  lcd_1.init();                      // initialize the lcd 
  lcd_1.init();
  // Print a message to the LCD.
  lcd_1.backlight();
  displayscreen();
  pinMode(trigPin, OUTPUT); // config trigger pin to output mode
  pinMode(echoPin, INPUT);  // config echo pin to input mode
  /*Serial.println("HX711 scale demo"); // load cell module to be activated later
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  Serial.println("Readings:");*/
 }  

void Height(){                       // height measurement module 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration_us = pulseIn(echoPin, HIGH);
  distance_cm = 0.017 * duration_us;
  lcd_1.clear();
  lcd_1.setCursor(0, 1); 
  lcd_1.print("Height: ");
  lcd_1.print(distance_cm);
 }

Void weight(){    // weight measurement module to be activated later 

  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  delay(1000);
  
}

void Beep() {
  buzzer.loop(); // MUST call the buzzer.loop() function in loop()
  char customKey = customKeypad.getKey();

  if (customKey) {
    Serial.print(customKey); // prints key to serial monitor
    buzzer.beep(100);  // generates a 100ms beep
  }
}


void loop(){
  
  
  static unsigned long lastKeypressTime = 0; // initialize the lastKeypressTime variable
  char customKey = customKeypad.getKey();  // variable for the input key
  Beep();
  if (customKey)  //if there is any input
   { 
    lcd_1.clear();
    lastKeypressTime = millis(); // update the lastKeypressTime variable with the current time
    lcd_1.setCursor(0,0);
    lcd_1.print("Enter your PhoneNo.:");
    Serial.print("Enter your PhoneNo.: "); 
    class String PhoneString = "";                   // Initialize an empty string to hold the user's age
    while (millis() - lastKeypressTime <= 10000) {  // Wait for the user to enter their Phone (up to 10 seconds)
    char PhoneKey = customKeypad.getKey();
      if (PhoneKey) {              
        // Initialize if a key is pressed
         lastKeypressTime = millis();
         Serial.print(PhoneKey);
         PhoneString += PhoneKey; // Append the entered digit to the age string
         lcd_1.setCursor(2,1);
         lcd_1.print(PhoneString);
         //PhoneString += PhoneKey;
          if (PhoneString.length() == 11) {              // If the age string has two digits, categorize the age
           // Print the user's age and age group
           lcd_1.clear();
           lcd_1.setCursor(1,0);
           lcd_1.print("Your Phone is: ");
           lcd_1.setCursor(2,1);
           lcd_1.print(PhoneString);
           //delay(1000); 
           // Convert the age string to an integer
           int Phone = PhoneString.toInt(); 
           //lastKeypressTime = 0;
           //if (millis() -lastKeypressTime > 10000) {  
            PhoneString = "";
           delay(1000);
            Age3();
          } 
      }
    }
  }
}


int Age3() {
  int iAge;
  lcd_1.clear();
  lcd_1.print("Enter Your Age :");
  lcd_1.setCursor(4, 1);
  char Key = NO_KEY;
  while (Key == NO_KEY) {
    Key = customKeypad.getKey();
    if (Key > '0' && Key <= '9')  iAge = Key - '0';
    else Key = NO_KEY;
  }
  Serial.print(Key);
  lcd_1.print(Key);
  Key = NO_KEY;
  while (Key == NO_KEY) {
    Key = customKeypad.getKey();
    if (Key >= '0' && Key <= '9')  iAge = iAge * 10 + Key - '0';
    else Key = NO_KEY;
  }

  Serial.print(Key);
  lcd_1.println(Key);

  lcd_1.clear();
  lcd_1.setCursor(1, 0);
  lcd_1.print("Your Age is: ");
  lcd_1.setCursor(8, 1);
  lcd_1.print(iAge);
  delay(1000);
  Gender();
  return iAge;
}

int Gender() {
  class String iGender;
  lcd_1.clear();
  lcd_1.print("Choose Gender:");
  delay(100); 
  lcd_1.setCursor(0, 1);
  lcd_1.print("*=Male #=Female");
  delay(500); 
  lcd_1.clear();
  lcd_1.setCursor(0, 0);
  lcd_1.print("Choose Gender:");
  lcd_1.setCursor(4, 1);
  char Key = NO_KEY;
  while (Key == NO_KEY) {
    Key = customKeypad.getKey();
    if (Key == '*')  iGender == "Male";
    else if (Key =='#')  iGender == "Female";
    else Key = NO_KEY;
  }
  Serial.print(Key);
  lcd_1.print(Key);
  Key = NO_KEY;
  while (Key == NO_KEY) {
    Key = customKeypad.getKey();
    if (Key == '*')  iGender = "Male";
    else if (Key == '#')  iGender = "Female";
    else Key = NO_KEY;
  }
  Serial.print(Key);
  lcd_1.println(Key);
  lcd_1.clear();
  lcd_1.setCursor(1, 0);
  lcd_1.print("Your are a ");
  lcd_1.setCursor(8, 1);
  lcd_1.print(iGender);
  delay(1000); 
  Receipt();
 //return iGender;
 // Height();
}

int Receipt() {
  class String iReceipt;
  lcd_1.clear();
  lcd_1.print("Print Receipt ?");
  delay(100); 
  lcd_1.setCursor(0, 1);
  lcd_1.print("*=Yes #=No");
  delay(500); 
  lcd_1.clear();
  lcd_1.setCursor(0, 0);
  lcd_1.print("Print Receipt ?");
  lcd_1.setCursor(4, 1);
  char Key = NO_KEY;
  while (Key == NO_KEY) {
    Key = customKeypad.getKey();
    if (Key == '*')  iReceipt == "Print Receipt";
    else if (Key =='#')  iReceipt == "Not to Print Receipt";
    else Key = NO_KEY;
  }
  Serial.print(Key);
  lcd_1.print(Key);
  Key = NO_KEY;
  while (Key == NO_KEY) {
    Key = customKeypad.getKey();
    if (Key == '*')  iReceipt = "Print a Receipt";
    else if (Key == '#')  iReceipt = "Not to Print ";
    else Key = NO_KEY;
  }
  Serial.print(Key);
  lcd_1.println(Key);
  lcd_1.clear();
  lcd_1.setCursor(1, 0);
  lcd_1.print("You choosed ");
  lcd_1.setCursor(0, 1);
  lcd_1.print(iReceipt);
  delay(1000); 
  Height();
  delay(5000); 
  lcd_1.clear();
  lcd_1.setCursor(4, 1);
  lcd_1.print("Thank You");
  delay(1000); 
  lcd_1.clear();
  displayscreen();
 //return iReceipt;
 
}

9 posts - 5 participants

Read full topic


Viewing all articles
Browse latest Browse all 15514

Trending Articles