Components and supplies
LED (generic)
Arduino Mega 2560
Buzzer
Standard LCD - 16x2 White on Blue
Resistor 10k ohm
Thermistor
Tools and machines
Soldering iron (generic)
Project description
Code
Star Trek Tricorder
arduino
Code to upload to your Arduino Mega.
1 2#include <LiquidCrystal.h> 3#include <math.h> 4 5// Initialize the library with the numbers of the interface pins 6LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 7 8//States for the menu. 9int currentMenuItem = 0; 10int lastState = 0; 11 12 13 14void setup() { 15 //Set the characters and column numbers. 16 lcd.begin(16, 2); 17 //Print default title. 18 clearPrintTitle(); 19} 20 21double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation 22 double Temp; 23 Temp = log(((10240000/RawADC) - 10000)); 24 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); 25 Temp = Temp + 273.15; // Convert Kelvin to Celsius 26 Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius 27 return Temp; 28} 29 30void loop() { 31 //Call the main menu. 32 mainMenu(); 33} 34 35void mainMenu() { 36 //State = 0 every loop cycle. 37 int state = 0; 38 //Refresh the button pressed. 39 int x = analogRead (0); 40 //Set the Row 0, Col 0 position. 41 lcd.setCursor(0,0); 42 43 //Check analog values from LCD Keypad Shield 44 if (x < 100) { 45 //Right 46 } else if (x < 200) { 47 //Up 48 state = 1; 49 } else if (x < 400){ 50 //Down 51 state = 2; 52 } else if (x < 600){ 53 //Left 54 } else if (x < 800){ 55 //Select 56 state = 3; 57 } 58 59 //If we are out of bounds on th menu then reset it. 60 if (currentMenuItem < 0 || currentMenuItem > 4) { 61 currentMenuItem = 0; 62 } 63 64 //If we have changed Index, saves re-draws. 65 if (state != lastState) { 66 if (state == 1) { 67 //If Up 68 currentMenuItem = currentMenuItem - 1; 69 displayMenu(currentMenuItem); 70 } else if (state == 2) { 71 //If Down 72 currentMenuItem = currentMenuItem + 1; 73 displayMenu(currentMenuItem); 74 } else if (state == 3) { 75 //If Selected 76 selectMenu(currentMenuItem); 77 } 78 //Save the last State to compare. 79 lastState = state; 80 } 81 //Small delay 82 delay(5); 83} 84 85//Display Menu Option based on Index. 86void displayMenu(int x) { 87 switch (x) { 88 case 1: 89 clearPrintTitle(); 90 lcd.print ("Bio Scan"); 91 break; 92 case 2: 93 clearPrintTitle(); 94 lcd.print ("Firmware Scan"); 95 break; 96 case 3: 97 clearPrintTitle(); 98 lcd.print ("Temperature Scan"); 99 break; 100 case 4: 101 clearPrintTitle(); 102 lcd.print ("Geo Scan"); 103 break; 104 case 5: 105 clearPrintTitle(); 106 lcd.print ("General Scan"); 107 break; 108 } 109} 110 111//Print a basic header on Row 1. 112void clearPrintTitle() { 113 lcd.clear(); 114 lcd.setCursor(0,0); 115 lcd.print("Starfleet"); 116 lcd.setCursor(0,1); 117} 118 119void scanMode() { 120 lcd.clear(); 121 lcd.setCursor(0,0); 122} 123 124void ledDelay(){ 125 const int buzzerPin1 = 26; 126 //led pins (in order): 22 23 24 25 127 digitalWrite(22, HIGH); 128 delay(100); 129 digitalWrite(22, LOW); 130 digitalWrite(23, HIGH); 131 delay(100); 132 digitalWrite(23, LOW); 133 digitalWrite(24, HIGH); 134 delay(100); 135 digitalWrite(24, LOW); 136 digitalWrite(25, HIGH); 137 delay(100); 138 digitalWrite(25, LOW); 139 digitalWrite(22, HIGH); 140 delay(100); 141 digitalWrite(22, LOW); 142 digitalWrite(23, HIGH); 143 delay(100); 144 digitalWrite(23, LOW); 145 digitalWrite(24, HIGH); 146 delay(100); 147 digitalWrite(24, LOW); 148 digitalWrite(25, HIGH); 149 delay(100); 150 digitalWrite(25, LOW); 151 tone(buzzerPin1, 700); 152 delay(70); 153 noTone(buzzerPin1); 154 delay(70); 155 tone(buzzerPin1, 700); 156 delay(70); 157 noTone(buzzerPin1); 158} 159 160void ledDelay2(){ 161 const int buzzerPin1 = 26; 162 //led pins (in order): 22 23 24 25 163 digitalWrite(23, HIGH); 164 digitalWrite(24, HIGH); 165 delay(200); 166 digitalWrite(23, LOW); 167 digitalWrite(24, LOW); 168 delay(150); 169 digitalWrite(22, HIGH); 170 digitalWrite(25, HIGH); 171 delay(200); 172 digitalWrite(22, LOW); 173 digitalWrite(25, LOW); 174 delay(150); 175 digitalWrite(23, HIGH); 176 digitalWrite(24, HIGH); 177 delay(200); 178 digitalWrite(23, LOW); 179 digitalWrite(24, LOW); 180 delay(150); 181 digitalWrite(22, HIGH); 182 digitalWrite(25, HIGH); 183 delay(200); 184 digitalWrite(22, LOW); 185 digitalWrite(25, LOW); 186 tone(buzzerPin1, 700); 187 delay(70); 188 noTone(buzzerPin1); 189 delay(70); 190 tone(buzzerPin1, 700); 191 delay(70); 192 noTone(buzzerPin1); 193} 194 195//Show the selection on Screen. 196void selectMenu(int x) { 197 198 199 200randomSeed(analogRead(14)); 201int scanResults = random(6, 1000); 202int a = 0; 203int scanResults2 = random(0, 3); 204 205 switch (x) { 206 case 1: 207 scanMode(); 208 lcd.print ("Bio scan "); 209 lcd.setCursor(0,1); 210 lcd.print("initiated"); 211 ledDelay(); 212 lcd.print("."); 213 ledDelay(); 214 lcd.print("."); 215 ledDelay(); 216 lcd.print("."); 217 ledDelay(); 218 scanMode(); 219 lcd.print ("Bio scan "); 220 lcd.setCursor(0,1); 221 lcd.print("completed"); 222 ledDelay(); 223 scanMode(); 224 lcd.print ("Life signs: "); 225 lcd.setCursor(0,1); 226 lcd.print(scanResults); 227 lcd.setCursor(1,1); 228 lcd.print("."); 229 lcd.setCursor(2,1); 230 lcd.print(scanResults); 231 //Call the function that belongs to Option 1 232 break; 233 case 2: 234 while (a == 0){ 235 /*scanMode(); 236 lcd.print ("Firmware scan "); 237 lcd.setCursor(0,1); 238 lcd.print("initiated"); 239 ledDelay2(); 240 lcd.print("."); 241 ledDelay2(); 242 lcd.print("."); 243 ledDelay2(); 244 lcd.print("."); 245 ledDelay2(); 246 scanMode(); 247 lcd.print ("Firmware scan "); 248 lcd.setCursor(0,1); 249 lcd.print("completed"); 250 ledDelay2();*/ 251 ledDelay2(); 252 scanMode(); 253 lcd.print ("Teraquads: "); 254 lcd.setCursor(0,1); 255 lcd.print(scanResults); 256 lcd.setCursor(1,1); 257 lcd.print("."); 258 lcd.setCursor(2,1); 259 lcd.print(scanResults); 260 261 } 262 //Call the function that belongs to Option 2 263 break; 264 case 3: 265 while (a == 0) { 266 digitalWrite(22, HIGH); 267 delay(100); 268 digitalWrite(22, LOW); 269 digitalWrite(23, HIGH); 270 delay(100); 271 digitalWrite(23, LOW); 272 digitalWrite(24, HIGH); 273 delay(100); 274 digitalWrite(24, LOW); 275 digitalWrite(25, HIGH); 276 delay(100); 277 digitalWrite(25, LOW); 278 digitalWrite(22, HIGH); 279 delay(100); 280 digitalWrite(22, LOW); 281 digitalWrite(23, HIGH); 282 delay(100); 283 digitalWrite(23, LOW); 284 digitalWrite(24, HIGH); 285 delay(100); 286 digitalWrite(24, LOW); 287 digitalWrite(25, HIGH); 288 delay(100); 289 digitalWrite(25, LOW); 290 scanMode(); 291 int val; //Create an integer variable 292 double temp; //Variable to hold a temperature value 293 val=analogRead(15); //Read the analog port 0 and store the value in val 294 temp=Thermister(val); //Runs the fancy math on the raw analog value 295 lcd.setCursor(0,0); 296 lcd.print("Temperature:"); 297 lcd.setCursor(0,1); 298 lcd.print(temp - 900); //Print the value to the serial port 299 lcd.setCursor(6,1); 300 lcd.print("(F)"); 301 delay(100); 302 } 303 break; 304 case 4: 305 scanMode(); 306 lcd.print ("Geological scan "); 307 lcd.setCursor(0,1); 308 lcd.print("initiated"); 309 ledDelay(); 310 lcd.print("."); 311 ledDelay(); 312 lcd.print("."); 313 ledDelay(); 314 lcd.print("."); 315 ledDelay(); 316 scanMode(); 317 lcd.print ("Geological scan "); 318 lcd.setCursor(0,1); 319 lcd.print("completed"); 320 ledDelay(); 321 scanMode(); 322 lcd.print ("Dilithium: "); 323 lcd.setCursor(0,1); 324 lcd.print(scanResults); 325 lcd.setCursor(1,1); 326 lcd.print("."); 327 lcd.setCursor(2,1); 328 lcd.print(scanResults); 329 330 //Call the function that belongs to Option 4 331 break; 332 case 5: 333 scanMode(); 334 lcd.print("Location"); 335 336 //Call the function that belongs to Option 4 337 break; 338 } 339} 340
Comments
Only logged in users can leave comments
Doctorcraft773
0 Followers
•0 Projects
+1
Work attribution
0