Devices & Components
Arduino Uno Rev3
16x2 LCD display with I²C interface
Push Button
Breadboard Jumper Wire Pack (200mm&100mm)
Bread board
TMP36- Analog Temperature sensor
Software & Tools
Arduino IDE
Project description
Code
MMTC v1 Code
cpp
Do with this as you please.
1#include <LiquidCrystal.h> 2#include <math.h> 3 4// ─── LCD & BACKLIGHT ───────────────────────────────────────── 5const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7; 6LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 7 8const int cs = 9, bl = 10; 9const int CONTRAST = 1, BACKLIGHT = 50; 10 11// ─── BUTTONS & SCREENS ─────────────────────────────────────── 12const int HS_PIN = 0; // H button 13const int MS_PIN = 1; // M button 14const int BTN_PIN = 8; // page toggle 15const uint8_t MAX_SCREENS = 3; 16uint8_t currentScreen = 0; 17 18// ─── CLOCK VARIABLES ───────────────────────────────────────── 19int h = 9, m = 0, s = 8, flag = 1; 20uint32_t lastSec = 0; 21uint32_t lastUpdate = 0; 22 23// ─── DEGREE CHAR ────────────────────────────────────────────── 24byte degreeChar[8] = { 25 B00110, B01001, B00110, B00000, 26 B00000, B00000, B00000, B00000 27}; 28 29// ─── CALCULATOR (PAGE B) ───────────────────────────────────── 30const char calcMenu[] = { 31 '0','1','2','3','4','5','6','7','8','9', 32 '+','-','*','/','=','C' 33}; 34const int CALC_MENU_LEN = sizeof(calcMenu); 35int calcIdx = 0; 36String calcExp; 37bool prevHs_Calc = HIGH, prevMs_Calc = HIGH; 38 39// ─── PERFORMANCE DASHBOARD (PAGE C) ────────────────────────── 40unsigned long lastPerfUpdate = 0; 41 42// ────────────────────────────────────────────────────────────── 43void setup() { 44 pinMode(HS_PIN, INPUT_PULLUP); 45 pinMode(MS_PIN, INPUT_PULLUP); 46 pinMode(BTN_PIN, INPUT_PULLUP); 47 48 analogWrite(cs, CONTRAST); 49 analogWrite(bl, BACKLIGHT); 50 51 lcd.begin(16,2); 52 lcd.noBlink(); 53 lcd.noCursor(); 54 lcd.createChar(0, degreeChar); 55 56 drawPageA(); 57} 58 59void loop() { 60 uint32_t now = millis(); 61 62 // ─── clock tick ─────────────────────────────────────── 63 if (now - lastSec >= 1000) { 64 lastSec += 1000; 65 tickClock(); 66 if (currentScreen == 0) redrawTime(); 67 } 68 69 // ─── page toggle ────────────────────────────────────── 70 if (digitalRead(BTN_PIN) == LOW) { 71 currentScreen = (currentScreen + 1) % MAX_SCREENS; 72 delay(300); 73 lcd.clear(); 74 prevHs_Calc = prevMs_Calc = HIGH; 75 76 if (currentScreen == 0) drawPageA(); 77 else if (currentScreen == 1) drawPageB(); 78 else drawPageC(); 79 } 80 81 // ─── PAGE A: clock + temp ───────────────────────────── 82 if (currentScreen == 0) { 83 if (digitalRead(HS_PIN) == LOW) setHour(); 84 if (digitalRead(MS_PIN) == LOW) setMin(); 85 86 if (now - lastUpdate >= 1000) { 87 lastUpdate = now; 88 drawTemp(); 89 } 90 } 91 92 // ─── PAGE B: calculator ─────────────────────────────── 93 else if (currentScreen == 1) { 94 bool hsNow = digitalRead(HS_PIN); 95 if (prevHs_Calc == HIGH && hsNow == LOW) { 96 calcIdx = (calcIdx + 1) % CALC_MENU_LEN; 97 lcd.setCursor(2,1); 98 lcd.print(calcMenu[calcIdx]); 99 delay(200); 100 } 101 prevHs_Calc = hsNow; 102 103 bool msNow = digitalRead(MS_PIN); 104 if (prevMs_Calc == HIGH && msNow == LOW) { 105 handleCalcSelect(calcMenu[calcIdx]); 106 delay(200); 107 } 108 prevMs_Calc = msNow; 109 } 110 111 // ─── PAGE C: performance dashboard ──────────────────── 112 else { 113 if (now - lastPerfUpdate >= 1000) { 114 lastPerfUpdate = now; 115 drawPageC(); 116 } 117 } 118} 119 120// ─── DRAW STATIC PAGES ───────────────────────────────── 121void drawPageA() { 122 lcd.setCursor(0,0); lcd.print("Time 00:00:00 AM"); 123 lcd.setCursor(0,1); lcd.print("Temp: "); lcd.write((byte)8); lcd.print("F"); 124 lastSec = millis(); 125 lastUpdate = millis(); 126 redrawTime(); 127 drawTemp(); 128} 129 130void drawPageB() { 131 calcExp = ""; 132 calcIdx = 0; 133 lcd.setCursor(0,0); lcd.print("Calc:"); 134 lcd.setCursor(0,1); lcd.print("> "); 135 lcd.setCursor(2,1); lcd.print(calcMenu[calcIdx]); 136} 137 138void drawPageC() { 139 lcd.clear(); 140 141 // Free RAM display 142 int ram = freeRam(); 143 float ramKB = ram / 1024.0; 144 lcd.setCursor(0,0); 145 lcd.print("Free RAM:"); 146 lcd.setCursor(10,0); 147 lcd.print(ramKB, 1); lcd.print("KB"); 148 149 // Simulated CPU load (Technically The Arduino UNO R3's CPU can't multitask so it is always running) 150 int load = ((analogRead(A0) + analogRead(9) + analogRead(10) + analogRead(4) + analogRead(5) + analogRead(6) + analogRead(7) + (2048 - ram)) / 2.25); 151 int percent = map(load, 0, 1023, 0, 100); 152 lcd.setCursor(0,1); 153 lcd.print("CPU Load:"); 154 lcd.print(percent); lcd.print("% "); 155} 156 157// ─── REDRAW HELPERS ──────────────────────────────────── 158void redrawTime() { 159 char b[3]; 160 sprintf(b,"%02d", h); lcd.setCursor(5,0); lcd.print(b); 161 sprintf(b,"%02d", m); lcd.setCursor(8,0); lcd.print(b); 162 sprintf(b,"%02d", s); lcd.setCursor(11,0); lcd.print(b); 163 lcd.setCursor(14,0); lcd.print(flag ? "PM" : "AM"); 164} 165 166// ─── DRAW TEMP (UPDATED TO AVERAGE MULTIPLE READINGS) ───────────────────────────────── 167void drawTemp() { 168 // Take 10 quick readings and average them 169 long sum = 0; 170 for (int i = 0; i < 10; i++) { 171 sum += analogRead(A0); 172 delay(5); 173 } 174 int avgReading = sum / 10; 175 176 // Convert average reading to Fahrenheit 177 int t = (int)(avgReading * 0.48828125); 178 char b[3]; 179 sprintf(b, "%2d", t); 180 lcd.setCursor(7,1); 181 lcd.print(b); 182} 183 184// ─── CLOCK HELPERS ───────────────────────────────────── 185void tickClock() { 186 s++; if (s>=60){ s=0; m++; } 187 if (m>=60){ m=0; h++; } 188 if (h>=13){ h=1; flag^=1; } 189} 190 191void setHour() { 192 h++; if (h>=13){ h=1; flag^=1; } 193 redrawTime(); 194 delay(200); 195} 196 197void setMin() { 198 s = 0; m++; 199 if (m>=60){ m=0; h++; } 200 if (h>=13){ h=1; flag^=1; } 201 redrawTime(); 202 delay(200); 203} 204 205// ─── CALCULATOR HELPERS ──────────────────────────────── 206void handleCalcSelect(char c) { 207 if (c == 'C') { 208 calcExp = ""; 209 lcd.setCursor(2,1); 210 lcd.print(" "); // Clear line 211 lcd.setCursor(2,1); 212 } 213 else if (c == '=') { 214 double val = evalExp(calcExp); 215 calcExp = String(val, 4); 216 // Trim trailing zeros 217 while(calcExp.endsWith("0")) calcExp.remove(calcExp.length() - 1); 218 if(calcExp.endsWith(".")) calcExp.remove(calcExp.length() - 1); 219 220 lcd.setCursor(6,0); 221 lcd.print(" "); 222 lcd.setCursor(6,0); 223 lcd.print(calcExp); 224 } 225 else { 226 calcExp += c; 227 lcd.setCursor(6,0); 228 lcd.print(calcExp); 229 } 230} 231 232// Basic expression evaluator for +, -, *, / with no parentheses or operator precedence 233double evalExp(String expr) { 234 double result = 0; 235 char op = '+'; 236 String number = ""; 237 238 for (unsigned int i = 0; i < expr.length(); i++) { 239 char c = expr.charAt(i); 240 if ((c >= '0' && c <= '9') || c == '.') { 241 number += c; 242 } else if (c == '+' || c == '-' || c == '*' || c == '/') { 243 double numVal = number.toFloat(); 244 if (op == '+') result += numVal; 245 else if (op == '-') result -= numVal; 246 else if (op == '*') result *= numVal; 247 else if (op == '/') { 248 if (numVal != 0) result /= numVal; 249 else return 0; 250 } 251 op = c; 252 number = ""; 253 } 254 } 255 // Last number 256 double numVal = number.toFloat(); 257 if (op == '+') result += numVal; 258 else if (op == '-') result -= numVal; 259 else if (op == '*') result *= numVal; 260 else if (op == '/') { 261 if (numVal != 0) result /= numVal; 262 else return 0; 263 } 264 265 return result; 266} 267 268// ─── MEMORY HELPER ───────────────────────────────────── 269#ifdef __arm__ 270int freeRam() { 271 char top; 272 return &top - reinterpret_cast<char*>(sbrk(0)); 273} 274#else 275extern unsigned int __heap_start; 276extern void *__brkval; 277int freeRam() { 278 int v; 279 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval); 280} 281#endif
Comments
Only logged in users can leave comments