Advertisement
Guest User

Scale Sim Arduino

a guest
Jan 13th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Load and use pre-defined LCD system
  2. #include <LiquidCrystal.h>
  3. #include <Servo.h>
  4. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  5. //create integers used
  6. int ServoPin = 5;
  7. int Status = 1;
  8. int Refresh = 1;
  9. int RandomNr;
  10. int Position;
  11. int Move;
  12. int RedLed = 12;
  13. int GreenLed = 11;
  14. int Button1 = 2;
  15. int Button2 = 3;
  16. int ZeroSetting = 0;
  17. int LCDLight = 6;
  18. int MaxSetting = 0;
  19. int RandomMode = 0;
  20. int BState1 = 0;
  21. int BState2 = 0;
  22. int MenuPos = 0;
  23. int ScreenMls;
  24. int ScreenRefresh = 0;
  25. int RandCount = 0;
  26. int RandomCalc;
  27. const int ScreenRefreshInterval = 70;
  28. unsigned long PreviousMls = 0;
  29. const long Interval = 5000;
  30. const long IntervalButton = 600;
  31. String vers = "    Version 0.23  ";
  32. Servo Servo1;
  33.  
  34. void setup() {
  35.   // set up the LCD's number of columns and rows:
  36.   lcd.begin(16, 2);
  37.   Serial.begin(9600);
  38.   // Declare pinmode's
  39.   Servo1.attach(ServoPin);
  40.   pinMode(Button1, INPUT_PULLUP);
  41.   pinMode(Button2, INPUT_PULLUP);
  42.   pinMode(RedLed, OUTPUT);
  43.   pinMode(GreenLed, OUTPUT);
  44.   pinMode(LCDLight, OUTPUT);
  45.   analogWrite(LCDLight, 140); // brightness setting for LCD, 0-255 (0-5v)
  46.   // Welcome user on the LCD
  47.   lcd.setCursor(0, 0);
  48.   lcd.print(" #ScaleSimTool#   ");
  49.   lcd.setCursor(0, 1);
  50.   lcd.print(vers);
  51.   delay(500);
  52.   lcd.setCursor(0, 0);
  53.   lcd.print(" *ScaleSimTool*   ");
  54.   lcd.setCursor(0, 1);
  55.   lcd.print(vers);
  56.   delay(500);
  57.   lcd.setCursor(0, 0);
  58.   lcd.print(" /ScaleSimTool/   ");
  59.   lcd.setCursor(0, 1);
  60.   lcd.print(vers);
  61.   lcd.setCursor(0, 0);
  62.   lcd.print(" #ScaleSimTool#   ");
  63.   lcd.setCursor(0, 1);
  64.   lcd.print(vers);
  65.   delay(500);
  66.   lcd.setCursor(0, 0);
  67.   lcd.print(" *ScaleSimTool*   ");
  68.   lcd.setCursor(0, 1);
  69.   lcd.print(vers);
  70.   delay(500);
  71.   lcd.setCursor(0, 0);
  72.   lcd.print(" /ScaleSimTool/   ");
  73.   lcd.setCursor(0, 1);
  74.   lcd.print(vers);
  75.   delay(500);
  76.   lcd.setCursor(0, 0);
  77.   lcd.print(".                 ");
  78.   delay(400);
  79.   lcd.setCursor(0, 0);
  80.   lcd.print("...               ");
  81.   delay(400);
  82.   lcd.setCursor(0, 0);
  83.   lcd.print("........          ");
  84.   delay(400);
  85.   lcd.print("...........       ");
  86.   delay(400);
  87.   lcd.setCursor(0, 0);
  88.   lcd.print("..............   ");
  89.   delay(400);
  90.   lcd.setCursor(0, 0);
  91.   lcd.print("..................");
  92.   delay(400);
  93.   lcd.setCursor(0, 0);
  94.   lcd.print("Starting Up!      ");
  95.   delay(1500);
  96.   lcd.setCursor(0, 0);
  97.   lcd.print("  Hello World :)  ");
  98.   delay(1400);
  99.   }
  100.  
  101. void loop()
  102.     {
  103.   unsigned long ScreenCurrentMls = millis();
  104.   unsigned long CurrentMls = millis();
  105.   unsigned long CurrentRandomMls = millis();
  106.   // Refresh Screen rate to prevent flickering
  107.     if (ScreenCurrentMls - ScreenMls >= ScreenRefreshInterval)
  108.     {
  109.     if (ScreenRefresh == 0)
  110.     {
  111.     ScreenRefresh = 1;
  112.     ScreenMls = ScreenCurrentMls;
  113.     }
  114.     else
  115.     {
  116.     ScreenRefresh = 0;
  117.     }
  118.     }
  119.  
  120.   if (MenuPos == 6) { //checking requirements if it's ok to do random weighings
  121.     if (CurrentRandomMls - PreviousMls >= Interval) { //using an if statement to bypass the delay
  122.     // If Status & Random is on, but zero/max is off
  123.     Serial.println("Generating random values as test"); //using the serial tool as debug
  124.     RandomNr = random(20, 160); // Update the integer with a random number, Min&Max can be user defined
  125.     PreviousMls = CurrentRandomMls; //update Previous Milisecond integer to be used in the timer tool
  126.     Serial.println(RandomNr);
  127.     Servo1.write(RandomNr);
  128.     RandomCalc = RandomNr / 1.8;
  129.     RandCount++;
  130.     lcd.clear();
  131.     lcd.print("Random #");
  132.     lcd.print(RandCount);
  133.     lcd.setCursor(0, 1);
  134.     lcd.print(RandomCalc);
  135.     lcd.print(" % Weight");
  136.     }
  137.    
  138.   }
  139.   // ################################################################################################
  140.   // Button 1 & 2 -> Endless loop setting BState1 high/low during the 'IntervalButton' period
  141.   // LCD&Serial print feedback enabled for debugging / development
  142.  
  143.   if (Status == 1) {
  144.    
  145.         if ((digitalRead(Button1) == LOW) && (BState1 == 0))  // Check if the button is pushed & if it's state is clear
  146.           {    
  147.             BState1 = 1; //set button 'high' so this if is locked
  148.             PreviousMls = CurrentMls; //update timer int
  149.             Serial.println("Button 1 pressed"); //serial print for feedback
  150.               if (MenuPos < 6) {
  151.               MenuPos = MenuPos +1;
  152.               Serial.println(MenuPos);
  153.               }
  154.            }
  155.          if ((CurrentMls - PreviousMls >= IntervalButton) && (BState1 == 1)) // timer loop to prevent double pushing
  156.            {
  157.              BState1 = 0; //unlock again after 'IntervalButton' timer has passed
  158.              Serial.println("Button 1 state release");
  159.            }            
  160.         if ((digitalRead(Button2) == LOW) && (BState2 == 0))
  161.           {
  162.             BState2 = 1;
  163.             PreviousMls = CurrentMls;
  164.             Serial.println("Button 2 pressed");
  165.             if ((MenuPos <= 6) && (MenuPos > 0))
  166.             {
  167.               MenuPos = MenuPos -1;
  168.               Serial.println(MenuPos);
  169.             }
  170.               else {
  171.                 MenuPos = 0;
  172.                 Serial.println(MenuPos);
  173.               }
  174.             }
  175.            
  176.            }
  177.          if ((CurrentMls - PreviousMls >= IntervalButton) && (BState2 == 1))
  178.            {
  179.             BState2 = 0;
  180.             Serial.println("Button 2 state release");
  181.         }
  182.  
  183. //########################################################### End of button loop
  184. //####Menu buildup
  185.  
  186. if (Status == 1)
  187.   {
  188.     if (( MenuPos == 0) && (ScreenRefresh == 1))
  189.   {
  190.     lcd.setCursor(0, 0);
  191.     lcd.print("Welcome To      ");
  192.     lcd.setCursor(0, 1);
  193.     lcd.print("ScaleSimTool    ");
  194.     delay(1500);
  195.     MenuPos = 1;
  196.   }
  197.   {
  198.     if (( MenuPos == 1) && (ScreenRefresh == 1))
  199.   {
  200.     lcd.setCursor(0, 0);
  201.     lcd.print("Use Left & Right");
  202.     lcd.setCursor(0, 1);
  203.     lcd.print("To select please");
  204.   }
  205.     if (( MenuPos == 2) && (ScreenRefresh == 1))
  206.   {
  207.     lcd.setCursor(0, 0);
  208.     lcd.print("1= Set Zero     ");
  209.     lcd.setCursor(0, 1);
  210.     lcd.print("2= Set Max      ");
  211.   }
  212.     if (( MenuPos == 3) && (ScreenRefresh == 1))
  213.   {
  214.     lcd.setCursor(0, 0);
  215.     lcd.print("3= Random Weight");
  216.     lcd.setCursor(0, 1);
  217.     lcd.print("Next Screen is 1");
  218.   }
  219.       if (( MenuPos == 4) && (ScreenRefresh == 1))
  220.   {
  221.     lcd.setCursor(0, 0);
  222.     lcd.print("1 Setting Zero  ");
  223.     lcd.setCursor(0, 1);
  224.     lcd.print(" Calibrate Scale");
  225.     Servo1.write(0);
  226.   }
  227.       if (( MenuPos == 5) && (ScreenRefresh == 1))
  228.   {
  229.     lcd.setCursor(0, 0);
  230.     lcd.print("2 Setting Max   ");
  231.     lcd.setCursor(0, 1);
  232.     lcd.print(" Calibrate Scale");
  233.     Servo1.write(180);
  234.   }
  235.           if (( MenuPos == 6) && (ScreenRefresh == 1) && (BState1 == 1) || (BState2 == 1))
  236.   {
  237.     lcd.setCursor(0, 0);
  238.     lcd.print("3 Random Weights");
  239.     lcd.setCursor(0, 1);
  240.     lcd.print(" Testing Scale  ");
  241.   }
  242.  }
  243.   }
  244. } //end void
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement