Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Load and use pre-defined LCD system
- #include <LiquidCrystal.h>
- #include <Servo.h>
- LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
- //create integers used
- int ServoPin = 5;
- int Status = 1;
- int Refresh = 1;
- int RandomNr;
- int Position;
- int Move;
- int RedLed = 12;
- int GreenLed = 11;
- int Button1 = 2;
- int Button2 = 3;
- int ZeroSetting = 0;
- int LCDLight = 6;
- int MaxSetting = 0;
- int RandomMode = 0;
- int BState1 = 0;
- int BState2 = 0;
- int MenuPos = 0;
- int ScreenMls;
- int ScreenRefresh = 0;
- int RandCount = 0;
- int RandomCalc;
- const int ScreenRefreshInterval = 70;
- unsigned long PreviousMls = 0;
- const long Interval = 5000;
- const long IntervalButton = 600;
- String vers = " Version 0.23 ";
- Servo Servo1;
- void setup() {
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- Serial.begin(9600);
- // Declare pinmode's
- Servo1.attach(ServoPin);
- pinMode(Button1, INPUT_PULLUP);
- pinMode(Button2, INPUT_PULLUP);
- pinMode(RedLed, OUTPUT);
- pinMode(GreenLed, OUTPUT);
- pinMode(LCDLight, OUTPUT);
- analogWrite(LCDLight, 140); // brightness setting for LCD, 0-255 (0-5v)
- // Welcome user on the LCD
- lcd.setCursor(0, 0);
- lcd.print(" #ScaleSimTool# ");
- lcd.setCursor(0, 1);
- lcd.print(vers);
- delay(500);
- lcd.setCursor(0, 0);
- lcd.print(" *ScaleSimTool* ");
- lcd.setCursor(0, 1);
- lcd.print(vers);
- delay(500);
- lcd.setCursor(0, 0);
- lcd.print(" /ScaleSimTool/ ");
- lcd.setCursor(0, 1);
- lcd.print(vers);
- lcd.setCursor(0, 0);
- lcd.print(" #ScaleSimTool# ");
- lcd.setCursor(0, 1);
- lcd.print(vers);
- delay(500);
- lcd.setCursor(0, 0);
- lcd.print(" *ScaleSimTool* ");
- lcd.setCursor(0, 1);
- lcd.print(vers);
- delay(500);
- lcd.setCursor(0, 0);
- lcd.print(" /ScaleSimTool/ ");
- lcd.setCursor(0, 1);
- lcd.print(vers);
- delay(500);
- lcd.setCursor(0, 0);
- lcd.print(". ");
- delay(400);
- lcd.setCursor(0, 0);
- lcd.print("... ");
- delay(400);
- lcd.setCursor(0, 0);
- lcd.print("........ ");
- delay(400);
- lcd.print("........... ");
- delay(400);
- lcd.setCursor(0, 0);
- lcd.print(".............. ");
- delay(400);
- lcd.setCursor(0, 0);
- lcd.print("..................");
- delay(400);
- lcd.setCursor(0, 0);
- lcd.print("Starting Up! ");
- delay(1500);
- lcd.setCursor(0, 0);
- lcd.print(" Hello World :) ");
- delay(1400);
- }
- void loop()
- {
- unsigned long ScreenCurrentMls = millis();
- unsigned long CurrentMls = millis();
- unsigned long CurrentRandomMls = millis();
- // Refresh Screen rate to prevent flickering
- if (ScreenCurrentMls - ScreenMls >= ScreenRefreshInterval)
- {
- if (ScreenRefresh == 0)
- {
- ScreenRefresh = 1;
- ScreenMls = ScreenCurrentMls;
- }
- else
- {
- ScreenRefresh = 0;
- }
- }
- if (MenuPos == 6) { //checking requirements if it's ok to do random weighings
- if (CurrentRandomMls - PreviousMls >= Interval) { //using an if statement to bypass the delay
- // If Status & Random is on, but zero/max is off
- Serial.println("Generating random values as test"); //using the serial tool as debug
- RandomNr = random(20, 160); // Update the integer with a random number, Min&Max can be user defined
- PreviousMls = CurrentRandomMls; //update Previous Milisecond integer to be used in the timer tool
- Serial.println(RandomNr);
- Servo1.write(RandomNr);
- RandomCalc = RandomNr / 1.8;
- RandCount++;
- lcd.clear();
- lcd.print("Random #");
- lcd.print(RandCount);
- lcd.setCursor(0, 1);
- lcd.print(RandomCalc);
- lcd.print(" % Weight");
- }
- }
- // ################################################################################################
- // Button 1 & 2 -> Endless loop setting BState1 high/low during the 'IntervalButton' period
- // LCD&Serial print feedback enabled for debugging / development
- if (Status == 1) {
- if ((digitalRead(Button1) == LOW) && (BState1 == 0)) // Check if the button is pushed & if it's state is clear
- {
- BState1 = 1; //set button 'high' so this if is locked
- PreviousMls = CurrentMls; //update timer int
- Serial.println("Button 1 pressed"); //serial print for feedback
- if (MenuPos < 6) {
- MenuPos = MenuPos +1;
- Serial.println(MenuPos);
- }
- }
- if ((CurrentMls - PreviousMls >= IntervalButton) && (BState1 == 1)) // timer loop to prevent double pushing
- {
- BState1 = 0; //unlock again after 'IntervalButton' timer has passed
- Serial.println("Button 1 state release");
- }
- if ((digitalRead(Button2) == LOW) && (BState2 == 0))
- {
- BState2 = 1;
- PreviousMls = CurrentMls;
- Serial.println("Button 2 pressed");
- if ((MenuPos <= 6) && (MenuPos > 0))
- {
- MenuPos = MenuPos -1;
- Serial.println(MenuPos);
- }
- else {
- MenuPos = 0;
- Serial.println(MenuPos);
- }
- }
- }
- if ((CurrentMls - PreviousMls >= IntervalButton) && (BState2 == 1))
- {
- BState2 = 0;
- Serial.println("Button 2 state release");
- }
- //########################################################### End of button loop
- //####Menu buildup
- if (Status == 1)
- {
- if (( MenuPos == 0) && (ScreenRefresh == 1))
- {
- lcd.setCursor(0, 0);
- lcd.print("Welcome To ");
- lcd.setCursor(0, 1);
- lcd.print("ScaleSimTool ");
- delay(1500);
- MenuPos = 1;
- }
- {
- if (( MenuPos == 1) && (ScreenRefresh == 1))
- {
- lcd.setCursor(0, 0);
- lcd.print("Use Left & Right");
- lcd.setCursor(0, 1);
- lcd.print("To select please");
- }
- if (( MenuPos == 2) && (ScreenRefresh == 1))
- {
- lcd.setCursor(0, 0);
- lcd.print("1= Set Zero ");
- lcd.setCursor(0, 1);
- lcd.print("2= Set Max ");
- }
- if (( MenuPos == 3) && (ScreenRefresh == 1))
- {
- lcd.setCursor(0, 0);
- lcd.print("3= Random Weight");
- lcd.setCursor(0, 1);
- lcd.print("Next Screen is 1");
- }
- if (( MenuPos == 4) && (ScreenRefresh == 1))
- {
- lcd.setCursor(0, 0);
- lcd.print("1 Setting Zero ");
- lcd.setCursor(0, 1);
- lcd.print(" Calibrate Scale");
- Servo1.write(0);
- }
- if (( MenuPos == 5) && (ScreenRefresh == 1))
- {
- lcd.setCursor(0, 0);
- lcd.print("2 Setting Max ");
- lcd.setCursor(0, 1);
- lcd.print(" Calibrate Scale");
- Servo1.write(180);
- }
- if (( MenuPos == 6) && (ScreenRefresh == 1) && (BState1 == 1) || (BState2 == 1))
- {
- lcd.setCursor(0, 0);
- lcd.print("3 Random Weights");
- lcd.setCursor(0, 1);
- lcd.print(" Testing Scale ");
- }
- }
- }
- } //end void
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement