mittalaite_v4.ino (3707B)
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 = millis(); 39 unsigned long lcdrefresh = millis(); 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 > 500) { 62 lcdrefresh = millis(); 63 if (newlenght == ""){ 64 lcd.clear(); 65 lcd.setCursor(0, 0); 66 lcd.print("L:" + String(int(countcm)) + "/" + String(int(langd)) + " cm"); 67 lcd.setCursor(0, 1); 68 lcd.print("T:" + String(int(total_langd)) + " cm"); 69 } 70 else{ 71 lcd.clear(); 72 lcd.setCursor(0, 0); 73 lcd.print("New lenght:"); 74 lcd.setCursor(0, 1); 75 lcd.print(String(newlenght)); 76 } 77 } 78 char key = keypad.getKey(); 79 if (key){ 80 newlenght += key; 81 } 82 if (key == '#'){ 83 if (newlenght != ""){ 84 langd = newlenght.toInt(); 85 newlenght = ""; 86 } 87 else{ 88 newlenght = ""; 89 } 90 } 91 if (key == '*'){ 92 newlenght = ""; 93 } 94 } 95 if (countcm >= (langd - 13)){ 96 digitalWrite(1, HIGH); //relay 97 } 98 else{ 99 digitalWrite(1, LOW); //relay 100 } 101 if (digitalRead(7) == LOW){ 102 if (millis() - impulstid < 10000){ 103 total_langd += countcm; 104 } 105 countcm = 0; 106 steps = 0; 107 impulstid = millis(); 108 delay(100); 109 lcd.clear(); 110 } 111 if (digitalRead(9) == LOW){ 112 if (millis() - impulstid < 10000){ 113 total_langd += countcm; 114 } 115 countcm = 0; 116 steps = 0; 117 impulstid = millis(); 118 delay(100); 119 lcd.clear(); 120 } 121 if (digitalRead(13) == LOW){ 122 countstep1 = false; 123 } 124 if (digitalRead(13) == HIGH){ 125 countstep1 = true; 126 } 127 if (digitalRead(6) == LOW){ 128 countstep2 = false; 129 } 130 if (digitalRead(6) == HIGH){ 131 countstep2 = true; 132 } 133 if (countstep1 == true){ 134 if (countstep2 == false){ 135 if (counted == false){ 136 countcm = countcm + calib; 137 steps = steps + 1; 138 impulstid = millis(); 139 counted = true; 140 } 141 } 142 } 143 if (countstep1 == false){ 144 if (countstep2 == true){ 145 if (counted == false){ 146 countcm = countcm - calib; 147 steps = steps - 1; 148 impulstid = millis(); 149 counted = true; 150 } 151 } 152 } 153 if (countstep1 == true){ 154 if (countstep2 == true){ 155 counted = false; 156 } 157 } 158 } 159 160