Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Bruce Johnson
Published © GPL3+

Arduino Star Trek Tricorder (BETA)

Working Star Trek tricorder, including a temperature sensor, and multiple other show-like scanning features.

IntermediateWork in progress3,874
Arduino Star Trek Tricorder (BETA)

Things used in this project

Story

Read more

Code

Star Trek Tricorder

Arduino
Code to upload to your Arduino Mega.
#include <LiquidCrystal.h>
#include <math.h> 
 
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;


 
void setup() {
   //Set the characters and column numbers.
   lcd.begin(16, 2);
   //Print default title.
   clearPrintTitle();
}
 
double Thermister(int RawADC) {  //Function to perform the fancy math of the Steinhart-Hart equation
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp + 273.15;              // Convert Kelvin to Celsius
 Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
 return Temp;
}
 
void loop() {
  //Call the main menu.
  mainMenu();
}
 
void mainMenu() {
  //State = 0 every loop cycle.
  int state = 0;
  //Refresh the button pressed.
  int x = analogRead (0);
  //Set the Row 0, Col 0 position.
  lcd.setCursor(0,0);
 
  //Check analog values from LCD Keypad Shield
  if (x < 100) {
    //Right
  } else if (x < 200) {
   //Up
    state = 1;
  } else if (x < 400){
   //Down
    state = 2;
  } else if (x < 600){
    //Left
  } else if (x < 800){
    //Select
    state = 3;
  }
 
  //If we are out of bounds on th menu then reset it.
  if (currentMenuItem < 0 || currentMenuItem > 4) {
   currentMenuItem = 0; 
  }
 
   //If we have changed Index, saves re-draws.
   if (state != lastState) {
      if (state == 1) {
         //If Up
          currentMenuItem = currentMenuItem - 1; 
          displayMenu(currentMenuItem);
      } else if (state == 2) {
         //If Down
          currentMenuItem = currentMenuItem + 1;  
          displayMenu(currentMenuItem);
      } else if (state == 3) {
         //If Selected
         selectMenu(currentMenuItem); 
      }
      //Save the last State to compare.
      lastState = state;
   } 
   //Small delay
  delay(5);
}
 
//Display Menu Option based on Index.
void displayMenu(int x) {
     switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print ("Bio Scan");
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("Firmware Scan");
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("Temperature Scan");
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("Geo Scan");
        break;
      case 5:
        clearPrintTitle();
        lcd.print ("General Scan");
        break;  
    }
}
 
//Print a basic header on Row 1.
void clearPrintTitle() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Starfleet");
  lcd.setCursor(0,1); 
}

void scanMode() {
  lcd.clear();
  lcd.setCursor(0,0); 
}

void ledDelay(){
        const int buzzerPin1 = 26;
        //led pins (in order): 22 23 24 25
        digitalWrite(22, HIGH);
        delay(100);
        digitalWrite(22, LOW);
        digitalWrite(23, HIGH);
        delay(100);
        digitalWrite(23, LOW);
        digitalWrite(24, HIGH);
        delay(100);
        digitalWrite(24, LOW);
        digitalWrite(25, HIGH);
        delay(100);
        digitalWrite(25, LOW);
        digitalWrite(22, HIGH);
        delay(100);
        digitalWrite(22, LOW);
        digitalWrite(23, HIGH);
        delay(100);
        digitalWrite(23, LOW);
        digitalWrite(24, HIGH);
        delay(100);
        digitalWrite(24, LOW);
        digitalWrite(25, HIGH);
        delay(100);
        digitalWrite(25, LOW);
        tone(buzzerPin1, 700);
        delay(70);
        noTone(buzzerPin1);
        delay(70);
        tone(buzzerPin1, 700);
        delay(70);
        noTone(buzzerPin1);
}

void ledDelay2(){
  const int buzzerPin1 = 26;
        //led pins (in order): 22 23 24 25
        digitalWrite(23, HIGH);
        digitalWrite(24, HIGH);
        delay(200);
        digitalWrite(23, LOW);
        digitalWrite(24, LOW);
        delay(150);
        digitalWrite(22, HIGH);
        digitalWrite(25, HIGH);
        delay(200);
        digitalWrite(22, LOW);
        digitalWrite(25, LOW);
        delay(150);
        digitalWrite(23, HIGH);
        digitalWrite(24, HIGH);
        delay(200);
        digitalWrite(23, LOW);
        digitalWrite(24, LOW);
        delay(150);
        digitalWrite(22, HIGH);
        digitalWrite(25, HIGH);
        delay(200);
        digitalWrite(22, LOW);
        digitalWrite(25, LOW);
        tone(buzzerPin1, 700);
        delay(70);
        noTone(buzzerPin1);
        delay(70);
        tone(buzzerPin1, 700);
        delay(70);
        noTone(buzzerPin1);
}
 
//Show the selection on Screen.
void selectMenu(int x) {

  

randomSeed(analogRead(14));
int scanResults = random(6, 1000);
int a = 0;
int scanResults2 = random(0, 3);
 
   switch (x) {
      case 1:
        scanMode();
        lcd.print ("Bio scan ");
        lcd.setCursor(0,1);
        lcd.print("initiated");
        ledDelay();
        lcd.print(".");
        ledDelay();
        lcd.print(".");
        ledDelay();
        lcd.print(".");
        ledDelay();
        scanMode();
        lcd.print ("Bio scan ");
        lcd.setCursor(0,1);
        lcd.print("completed");
        ledDelay();
        scanMode();
        lcd.print ("Life signs: ");
        lcd.setCursor(0,1);
        lcd.print(scanResults);
        lcd.setCursor(1,1);
        lcd.print(".");
        lcd.setCursor(2,1);
        lcd.print(scanResults);
        //Call the function that belongs to Option 1
        break;
      case 2:
        while (a == 0){
        /*scanMode();
        lcd.print ("Firmware scan ");
        lcd.setCursor(0,1);
        lcd.print("initiated");
        ledDelay2();
        lcd.print(".");
        ledDelay2();
        lcd.print(".");
        ledDelay2();
        lcd.print(".");
        ledDelay2();
        scanMode();
        lcd.print ("Firmware scan ");
        lcd.setCursor(0,1);
        lcd.print("completed");
        ledDelay2();*/
        ledDelay2();
        scanMode();
        lcd.print ("Teraquads: ");
        lcd.setCursor(0,1);
        lcd.print(scanResults);
        lcd.setCursor(1,1);
        lcd.print(".");
        lcd.setCursor(2,1);
        lcd.print(scanResults);

        }
        //Call the function that belongs to Option 2
        break;
       case 3:
        while (a == 0) {
          digitalWrite(22, HIGH);
        delay(100);
        digitalWrite(22, LOW);
        digitalWrite(23, HIGH);
        delay(100);
        digitalWrite(23, LOW);
        digitalWrite(24, HIGH);
        delay(100);
        digitalWrite(24, LOW);
        digitalWrite(25, HIGH);
        delay(100);
        digitalWrite(25, LOW);
        digitalWrite(22, HIGH);
        delay(100);
        digitalWrite(22, LOW);
        digitalWrite(23, HIGH);
        delay(100);
        digitalWrite(23, LOW);
        digitalWrite(24, HIGH);
        delay(100);
        digitalWrite(24, LOW);
        digitalWrite(25, HIGH);
        delay(100);
        digitalWrite(25, LOW);
        scanMode();
        int val;                //Create an integer variable
        double temp;            //Variable to hold a temperature value
        val=analogRead(15);      //Read the analog port 0 and store the value in val
        temp=Thermister(val);   //Runs the fancy math on the raw analog value 
        lcd.setCursor(0,0);
        lcd.print("Temperature:");
        lcd.setCursor(0,1);
        lcd.print(temp - 900);   //Print the value to the serial port
        lcd.setCursor(6,1);
        lcd.print("(F)");
        delay(100);
        }
        break;
      case 4:
        scanMode();
        lcd.print ("Geological scan ");
        lcd.setCursor(0,1);
        lcd.print("initiated");
        ledDelay();
        lcd.print(".");
        ledDelay();
        lcd.print(".");
        ledDelay();
        lcd.print(".");
        ledDelay();
        scanMode();
        lcd.print ("Geological scan ");
        lcd.setCursor(0,1);
        lcd.print("completed");
        ledDelay();
        scanMode();
        lcd.print ("Dilithium: ");
        lcd.setCursor(0,1);
        lcd.print(scanResults);
        lcd.setCursor(1,1);
        lcd.print(".");
        lcd.setCursor(2,1);
        lcd.print(scanResults);
        
        //Call the function that belongs to Option 4
        break;
      case 5:
        scanMode();
        lcd.print("Location");
        
        //Call the function that belongs to Option 4
        break;
    }
}

Credits

Bruce Johnson
1 project • 1 follower
In 2015, I began teaching a class called the Pi Crafters. This class teaches kids how to program in HTML, CSS, Arduino, and Python.

Comments