#include "sim.h"
#include <LiquidCrystal_I2C.h>
//******************Screen Seting
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
//******************Screen Seting************
unsigned long msStar =0;
// unsigned long msPrev1 = 0;
// unsigned long msPrev2 = 0;
unsigned long msBS =0; //millisec start
unsigned long msPB = 0; //button previous
unsigned long LdP1 = 0; //Led 1 milli previous
unsigned long LdP2 = 0; //Led 2 milli previous
unsigned long bDB=255;
unsigned long LEDi1=1000;
unsigned long LEDi2=550;
int LED1_state=0;
int LED2_state=0;
int btnA = 13; //button
bool bSA = 0;// Button state need to start at ZERO!
#define LED1 6
#define LED2 2
him Tst(0);
int pt1 = A1;int pt2 = A2;
int Pr1 = 0;int Pr2 = 0;
////*****************SCILAB VARIABLES*************//////
const float pi = 3.14;
//state 1 variables:assumed values
int p1=10132.089; // PRESSURE IN Pa
int tc1=326.85;//INITIAL degrees in Celcius
int tc2=1300;//FINAL degrees in Celcius
int t1=tc1+273.15;
//engine characteristics:assumed values
int bore=99.3;
int stroke=84.1;
int cr=9.1;
int con_rod=155.52;
int A=(2*pi*stroke*bore+2*pi*sq(bore)*pow(10,-6));
//since used air standard assumption,we took air(diatomic gas) as working fluid
int gamma=1.4;
//maximum temperature of the cycle after constant volume heat
//addition:assumed vaue
int t3=tc2+273.15;
//Using the formulas mentioned above
int vswept=(pi/4)*sq(bore)*stroke;
int vclearance=vswept/(cr-1);
//engine characteristics
int v2=vclearance;
int v1=vswept+vclearance;
//using adiabatic relation for isentropic compression
int p2=p1*pow((cr),gamma);
int t2=t1*pow((cr),(gamma-1));
//constant volume heat addition and rejection
int v3=v2;
int v4=v1;
int p3=p2*(t3/t2);
//using adiabatic relation for isentropic expansion
int p4=p3/pow((cr),gamma);
int t4=t3/pow((cr),(gamma-1));
//efficiency of an otto cycle
int eff=1-(t1/t2);
// mprintf("efficiency of an otto cycle= %f \n\n",eff)
// Serial.println("efficiency of an otto cycle= ");
int psi=0.000145038;// convert to psi
//BAR=0.00001// convert to psi
int P1=p1*psi;
int P2=p2*psi;
int P3=p3*psi;
int P4=p4*psi;
//usiing adiabatic relation at state 3
int constant_e=p3*pow(v3,gamma);
////FUNCTION may not be neess
// void benz(int _bore,int _stroke,int _con_rod,int _cr,int _v2,int start_ang,int end_ang)
// {
// int a=_stroke/2;
// int r=_con_rod/a;
// //theta is an array
// // int theta=linspace(start_ang,end_ang,180);
// //DIVIDING THE PISTON KINEMATICS FORMULA INTO term1,term2,term3
// // int term1=0.5*(cr-1);
// // int term2=r+1-cosd(theta);
// // //since theta is an array using . operator while using power operator(^)
// // int term3=((r^2)-(sind(theta).^2)).^(0.5);
// // //now combining term1,term2,term2,term3 into final formula
// // //since term3 is an array using . operator before * operator
// // int v=(1+term1.*(term2-term3)).*v2;
// //the result of this function v is also an array
// }
////FUNCTION may not be neess
void vcomp(int _stroke,int _con_rod,int _cr,int _theta, int v2) //v2 is Clearance volume
{
int a=_stroke/2;
int r=_con_rod/a;
int term1=0.5*(_cr-1);
int term2=r+1-cos(_theta);
int term3=pow((sq(r)-pow(sin(_theta),2)),(0.5));
int v=(1+term1*(term2-term3))*v2;
Serial.println(v);
}
//
// int a=_stroke/2;
// int r=_con_rod/a;
// //theta is an array
// // int theta=linspace(start_ang,end_ang,180);
// //DIVIDING THE PISTON KINEMATICS FORMULA INTO term1,term2,term3
// int term1=0.5*(cr-1);
// int term2=r+1-cosd(theta);
// // //since theta is an array using . operator while using power operator(^)
// int term3=((r^2)-(sind(theta).^2)).^(0.5);
// // //now combining term1,term2,term2,term3 into final formula
// // //since term3 is an array using . operator before * operator
// int v=(1+term1.*(term2-term3)).*v2;
// the result of this function v is also an array
//
// int vcomprress = vcomp(stroke,con_rod,cr,180, vclearance);
int c1 = p1*pow(v1,gamma);
// int pcompress =c1/pow(vcomprress,gamma)
// p_compression = c1./v_compression.^gamma;
////*****************SCILAB VARIABLES*************//////
void jigg1()
{
int btnStateA = digitalRead(btnA);
msStar = millis();
if (msStar-msPB>=bDB)
{
msPB=msStar;
if (btnStateA ==LOW)
{
bSA=!bSA;Serial.print("btnStateA Pressed ");Serial.println(bSA);
}
if (bSA==1) ///IF ON
{
// digitalWrite(LED1,1);
lcd.setCursor(10, 0);lcd.print("ON!");
lcd.setCursor(13, 0);lcd.print(" ");
////LED CONTROL************
msStar = millis();
if (msStar-LdP1>=LEDi1)
{
LdP1=msStar;
if (LED1_state==0) LED1_state=1; else LED1_state=0;
digitalWrite(LED1,LED1_state);Serial.print("on ");Serial.println(millis());
}
////LED CONTROL************
}else{ ///IF OFF?
lcd.setCursor(10, 0);lcd.print(" ");
lcd.setCursor(13, 0);lcd.print("OFF");
////LED CONTROL************
// msStar = millis();
// Tst.mMov(msStar,LdP1,200);
// if (msStar-LdP1>=200)
// {
// LdP1=msStar;
// if (LED1_state==0)
// {
// LED1_state =1;
// Serial.println("how");
// }else{
// Serial.println("off");
// LED1_state=0;
// }
// }
////LED CONTROL************
}
}
}
void setup() {
// put your setup code here, to run once:
// Tst.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Jig State");
// lcd.setCursor(0, 1);
// lcd.print("Sensor 2");
pinMode(btnA, INPUT_PULLUP);
pinMode(pt1, INPUT); pinMode(pt2, INPUT);
pinMode(LED1, OUTPUT);
Serial.begin(9600);
Serial.println("Hello!");
Tst.begin();
}
void loop() {
// LLop();
jigg1();
// Serial.println(c1);
vcomp(stroke,con_rod,cr,180, vclearance);
delay(600);
// Serial.println(Pr1*Pr2);
// Tst.on();
// delay(aV1);
// Tst.off();
// delay(aV1);
}
//**********************************
// put your main code here, to run repeatedly:
// Pr1 = map(analogRead(pt1),0,1023,0,33);Pr2 = map(analogRead(pt2),0,1023,0,5);
// int aV1 = myArray[Pr1][Pr2];Serial.print("printed value ");Serial.println(aV1);
// Serial.print("Sense 1 ");Serial.print(Pr1);
// Serial.print(" Sense 2 ");Serial.println(Pr2);
//if (LED1_state==0) LED1_state=1; else LED1_state=0;
//digitalWrite(LED1,LED1_state);Serial.print("off ");Serial.println(millis());