Skip to main content

Day 19: Project 2

Our project was a test stand for rocket motors
Using an LCD screen, we could choose a display mode, and it would display the selected value. Unfortunately, we were unable to calibrate the pressure sensor, so all we ended up with was a load cell. We used the HX711 library for this. The LCD screen displayed the maximum force experienced during the test

Code for MotorSensors.h
#ifndef _MOTORSENSORS_
#define _MOTORSENSORS_

#include <LiquidCrystal.h> //Include Liquid Crystal Library
#include <HX711.h> //Include HX711 Load Cell library
#include "HX711.h" //Include Load Cell header file
#include <math.h> //Include math library

#define calibration_factor -8815 //set calibration factor
#define DAT 3 //define DAT pin
#define CLK 2 //define CLK pin
#define pressurePin A1
#define tempPin A2

HX711 scale(DAT,CLK); 
LiquidCrystal lcd(8,9,4,5,6,7); //Define LCD pins

//Initialize variables
float maxkg=0; 
float currentval=0;
float maxforce=0;
char mode;
char lastmode;
float MaxPressure=0;
float maxTemp=0;

void LoadCellSetup()
{
  scale.set_scale(calibration_factor); //Set scale with calibration factor
  scale.tare(); //Tare scale
  //Print menu of options
  Serial.println("Press 0 to begin testing");
  Serial.println("Press 1 to end testing");
  Serial.println("Press t to tare");
  //Print to LCD screen
  lcd.begin(16,2);
  lcd.print("Maximum Force:");
}

void LoadCellLCDSetup()
{
  scale.set_scale(calibration_factor); //Set scale with calibration factor
  scale.tare(); //Tare scale
}

void LoadCellRun()
{
  currentval=scale.get_units(); //Read force
  Serial.print("Reading (kg): ");
  Serial.println(currentval); //Print reading in serial monitor
}

void LivePressure()
{
  float rawReading=analogRead(pressurePin);
  Serial.print("Raw Reading: ");
  Serial.println(rawReading);
  float pressure=400*((5*rawReading/1023)-0.5);
  if(pressure>MaxPressure)
  {
    MaxPressure=pressure;
  }
  Serial.print("Pressure: ");
  Serial.println(pressure);
}

void LoadCell()
{
  
  if(Serial.available())
  {
    mode=Serial.read(); //Read user input
  }
  
  if(mode=='0')
  {
    Serial.print("Reading (kg): ");
    Serial.println(currentval); //Print reading in serial monitor
    lastmode=mode;
    if(currentval>maxkg)
    {
      maxkg=currentval; //Set maximum value
      maxforce=maxkg*9.8; //Convert to Newtons
    }
  }
  if(mode=='1' && mode!=lastmode)
  {
    Serial.print("Max Force Detected: ");
    Serial.print(maxforce); //Print max force in serial monitor
    Serial.println(" N");
    lcd.setCursor(0,1); //Move LCD cursor to first position in second row
    lcd.print(maxforce); //Print max force to LCD screen
    lcd.setCursor(7,1); //Set cursor to 7th position in second row
    lcd.print("N"); //Add units
    lastmode=mode;
  }
  if(mode=='t')
  {
    scale.set_scale(calibration_factor); //Reset and tare scale
    scale.tare();
  }
}

float LoadCellRunLCD()
{
  currentval=scale.get_units();
  if(currentval>maxkg)
  {
    maxkg=currentval; //Set maximum value
    maxforce=maxkg*9.8; //Convert to Newtons
  }
  return maxforce; 
}

float PressureMax()
{
  float rawReading=analogRead(pressurePin);
  float pressure=400*((5*rawReading/1023)-0.5);
  if(pressure>MaxPressure)
  {
    MaxPressure=pressure;
  }
  return MaxPressure;
}

float Thermistor() 
{
  int raw=analogRead(tempPin);
  float resistor = 13800; //value of fixed resistor in Vernier shield
  float resistance; //create local variable for resistance
  float temp; //create local variable for temperature

  resistance=log(resistor*raw/(1023-raw)); //calculate resistance
  temp = 1 / (0.00102119 + (0.000222468 * resistance) + (0.000000133342 * resistance * resistance * resistance)); //calculate temperature using the Steinhart-Hart equation
  temp = temp - 273.15; //Convert Kelvin to Celsius     
  temp = (temp * 1.8) + 32.0; // Celsius to Fahrenheit                  
  if(temp>maxTemp)
  {
    maxTemp=temp;
  }
  return maxTemp; //return the temperature
}

#endif

Code for MotorTesting.h header file
#ifndef _MOTORTESTINGH_
#define _MOTORTESTINGH_

/*---------------------------- LCD main menu ----------------------------*/
#include "MotorSensors.h"

//#include <LiquidCrystal.h>    //Include LCD library
//
// LiquidCrystal lcd (8, 9, 4, 5, 6, 7);  
int menuMode=0;
int lastVal=0;
int currentVal=0;
int sensorValue=0;
int Value=0;
int newReading=0;
float peakForce=0;
float peakPressure=0;
float peakTemp=0;

int btn=A0;

void InitializeMotorTest()
 {  
 /*----------------Start up screen----------------*/
  lcd.begin (16, 2);          //Number of columns and rows
  lcd.setCursor (0, 0);       //Print message at 0, 0 position of LCD
  lcd.print ("Project 2:");   //Print Menu message onto LCD
  lcd.setCursor (0, 1);       // Set cursor to 0 column, 1st row
  lcd.print ("Rocket Stand"); //Print message: Rocket test stand 
  delay (5000);               //Delay for 5 seconds before going 
  /*-----------------------------------------------*/
  lcd.clear ();               // Clear LCD before displaying menu options

  lcd.setCursor (0, 0);
  lcd.print("Choose mode with");
  lcd.setCursor(0,1);
  lcd.print("select button");
  LoadCellLCDSetup();
  Serial.begin(9600);
  pinMode (btn, INPUT);   //Reading voltage values corresponding from the buttons on LCD shield
 }

/*
Select: 720
Left: 480
Down: 307
Up: 131
Right: 0
*/

void MotorTest()
 {
  sensorValue=analogRead(btn);
  
  if(sensorValue==720 && lastVal!=9)
  {
    menuMode++;
    lastVal=9;
  }
  
  if(sensorValue>721 || sensorValue<720)
  {
    lastVal=0;
  }
  
  switch(menuMode)
  {
    case 0:
    //lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Choose mode with");
    lcd.setCursor(0,1);
    lcd.print("select button");
    break;
    
    case 1:
    lcd.setCursor(0,0);
    lcd.print("Load Cell Cal   ");
    lcd.setCursor(0,1);
    lcd.print("Use right 2 tare");
    newReading=analogRead(btn);
//    Serial.print("New Reading: ");
//    Serial.println(newReading);
    if(newReading==0 && Value!=13)
    {
      menuMode=11;
      Value=13;
    }
    break;

    case 2:
    lcd.setCursor(0,0);
    lcd.print("Max Force       ");
    lcd.setCursor(0,1);
    lcd.print(peakForce);
    lcd.setCursor(5,1);
    lcd.print("         ");
    break;

    case 11:
    scale.set_scale(calibration_factor);
    scale.tare();
    lcd.setCursor(0,1);
    lcd.print("                ");
    lcd.setCursor(0,0);
    lcd.print("Tare Successful ");
    lcd.setCursor(0,1);
    lcd.print("Return with left");
    //Value=8;
    newReading=analogRead(btn);
    LoadCellRun();
    if(newReading==480 && Value!=9)
    {
      menuMode=1;
      Value=9;
    }
    break; 
    
    case 3:
    lcd.setCursor(0,0);
    lcd.print("Thermistor      ");
    lcd.setCursor(0,1);
    lcd.print(peakTemp);
    lcd.setCursor(5,1);
    lcd.print("         ");
    break;

    case 4:
    lcd.setCursor(0,0);
    lcd.print("Pressure        ");
    lcd.setCursor(0,1);
    lcd.print(peakPressure);
    lcd.setCursor(5,1);
    lcd.print("         ");
    break;

    case 5:
    lcd.setCursor(0,0);
    lcd.print("Up to begin test");
    newReading=analogRead(btn);
    if(newReading==131 && Value!=11)
    {
      menuMode=7;
      Value=11;
    }
    break;

    case 7:
    LoadCellRun();
    LivePressure();
    lcd.setCursor(0,0);
    lcd.print("Test in progress");
    lcd.setCursor(0,1);
    lcd.print("Down to end test");
    newReading=analogRead(btn);
    peakForce=LoadCellRunLCD();
    peakPressure=PressureMax();
    peakTemp=Thermistor();
    if(newReading==307 && Value!=10)
    {
      menuMode=10;
      Value=10;
    }
    break;

    case 10:
    lcd.setCursor(0,0);
    lcd.print("Testing Complete");
    lcd.setCursor(0,1);
    lcd.print("                ");
    delay(5000);
    menuMode=2;
    break;
    
    default:
    menuMode=0;
    break;
  }
}


#endif

Main Code
#include "MotorTesting.h"

void setup() 
{
 InitializeMotorTest();
}

void loop() {
  MotorTest();

}

Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight const int red=11; const int green=10; const int blue=9; const int button=4; boolean lastbutton=LOW; boolean currentbutton=LOW; int ledMode=0; void setup() {   // put your setup code here, to run once:   pinMode(button,INPUT);   pinMode(red,OUTPUT);   pinMode(green,OUTPUT);   pinMode(blue,OUTPUT); } boolean debounce(boolean last) {   boolean current=digitalRead(button);   if(last!=current)   {     delay(5);     current=digitalRead(button);   }   return current; } void setMode(int mode) {   //RED   if(mode==1)   {     digitalWrite(red,HIGH);     digitalWrite(green,LOW);     digitalWrite(blue,LOW);   }   if(mode==2)   {     //orange     digitalWrite(red,HIGH);     analogWrite(green,40);     digitalWrite(blue,LOW);...