stockmetare

git clone https://git.tarina.org/stockmetare
Log | Files | Refs

mittalaite_v3.ino (3743B)


      1 
      2 
      3 /*
      4  Hansson Service
      5  Mittalaite v.4
      6 */
      7 
      8 // include the library code:
      9 #include <LiquidCrystal.h>
     10 #include <Keypad.h>
     11 
     12 // initialize keypad
     13 String newlenght = "";
     14 const byte ROWS = 4; //four rows
     15 const byte COLS = 3; //three columns
     16 char keys[ROWS][COLS] = {
     17   {'1','2','3'},
     18   {'4','5','6'},
     19   {'7','8','9'},
     20   {'*','0','#'}
     21 };
     22 byte rowPins[ROWS] = {A3, A2, A1, A0}; //connect to the row pinouts of the keypad
     23 byte colPins[COLS] = {8, A5, A4}; //connect to the column pinouts of the keypad
     24 Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
     25 
     26 // initialize the library by associating any needed LCD interface pin
     27 // with the arduino pin number it is connected to
     28 const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
     29 float langd = 300;
     30 float total_langd = 0;
     31 float countcm = 0;
     32 int steps1 = 0;
     33 int steps2 = 0;
     34 int steps = 0;
     35 float calib = 2.00;
     36 int optsensor1;
     37 int optsensor2;
     38 unsigned long impulstid = 0;
     39 unsigned long lcdrefresh = 0;
     40 boolean counted = false;
     41 boolean countstep1 = false;
     42 boolean countstep2 = false;
     43 LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
     44 
     45 void setup() {
     46   // set up the LCD's number of columns and rows:
     47   lcd.begin(16, 2);
     48   pinMode(1, OUTPUT);
     49   pinMode(7, INPUT_PULLUP); //knapp 1
     50   pinMode(9, INPUT_PULLUP); //knapp 3  
     51   pinMode(10, INPUT_PULLUP); //knapp 4
     52   pinMode(6, INPUT); //optisksensor 1
     53   pinMode(13, INPUT); //optisksensor 2
     54   digitalWrite(1, HIGH); //relay
     55   }
     56 
     57 void loop() {
     58   if ((millis() - impulstid >= 1500)) {
     59     // set the cursor to column 0, line 1
     60     // (note: line 1 is the second row, since counting begins with 0):
     61     if (millis() - lcdrefresh > 50) {
     62       if (newlenght == ""){
     63         lcdrefresh = millis();
     64         lcd.clear();
     65         lcd.setCursor(0, 0);
     66         lcd.print("L:" + String(int(langd)) + "/" + String(int(countcm)) + " cm");
     67         lcd.setCursor(0, 1);
     68         lcd.print("T:" + String(int(total_langd)) + String(int(countcm)) + " cm");
     69         delay(200);
     70       }
     71       else{
     72         lcd.clear();
     73         lcd.setCursor(0, 0);
     74         lcd.print("New lenght:");
     75         lcd.setCursor(0, 1);
     76         lcd.print(String(newlenght));
     77         delay(200);
     78       }
     79     }
     80     char key = keypad.getKey();
     81     if (key){
     82       newlenght += key;
     83     }
     84     if (key == '#'){
     85       if (newlenght != ""){
     86         langd = newlenght.toInt();
     87         newlenght = "";
     88       }
     89       else{
     90         newlenght = "";
     91       }
     92     }
     93     if (key == '*'){
     94       newlenght = "";
     95       }
     96     }
     97   if (countcm >= (langd - 13)){
     98     digitalWrite(1, HIGH); //relay
     99   }
    100   else{
    101     digitalWrite(1, LOW); //relay
    102   }
    103   if (digitalRead(7) == LOW){
    104     langd = langd + 5;
    105     impulstid = millis();
    106     delay(300);
    107     lcd.clear();
    108   }
    109   if (digitalRead(8) == LOW){
    110     langd = langd - 5;
    111     impulstid = millis();
    112     delay(300);
    113     lcd.clear();
    114   }
    115   if (digitalRead(9) == LOW){
    116     total_langd += countcm;
    117     countcm = 0;
    118     steps = 0;
    119     impulstid = millis();
    120     delay(100);
    121     lcd.clear();
    122   }
    123   if (digitalRead(13) == LOW){
    124     countstep1 = false;
    125   }
    126   if (digitalRead(13) == HIGH){
    127     countstep1 = true;
    128     }
    129   if (digitalRead(6) == LOW){
    130     countstep2 = false;
    131     }
    132   if (digitalRead(6) == HIGH){
    133     countstep2 = true;
    134     }
    135   if (countstep1 == true){
    136     if (countstep2 == false){
    137       if (counted == false){
    138         countcm = countcm + calib;
    139         steps = steps + 1;
    140         impulstid = millis();
    141         counted = true;
    142         }
    143       }
    144     }
    145   if (countstep1 == false){
    146     if (countstep2 == true){
    147       if (counted == false){
    148         countcm = countcm - calib;
    149         steps = steps - 1;
    150         impulstid = millis();
    151         counted = true;
    152       }
    153     }
    154   }
    155   if (countstep1 == true){
    156     if (countstep2 == true){
    157       counted = false;
    158     }
    159   }
    160 }
    161 
    162