String inputString = "";
bool stringComplete = false;
#define MR 6
#define MC 3
#define JS 2
void printArray ( const int [][ MC ] );
float arIn[ MR][ MC] = {{1, 1,0},{ 2, 1,0},{3, 1,1},{1, 2,0},{2, 2,0},{3, 2,1}};
int jON=0;
int jOFF=0;
//int NB(int sensor,int kolom) {
int * NB(int sensor,int kolom) {
jON=0;
jOFF=0;
int jONkolom=0;
int jOFFkolom=0;
for (int i = 0; i < MR; i++) {
if(arIn[i][2]==1){jON++;}
else if(arIn[i][2]==0){jOFF++;}
//--------------------------
if(arIn[i][kolom]==sensor && arIn[i][2]==1){jONkolom++;}
else if(arIn[i][kolom]==sensor && arIn[i][2]==0){jOFFkolom++;}
}//i
int arout[JS];
arout[0]=jOFFkolom;
arout[1]=jONkolom;
return arout;
}
void setup() {
Serial.begin(115200);
}//setup
String s(long var){
return String(var);
}
void loop() {
if (stringComplete) {
Serial.println(inputString);
inputString = "";
stringComplete = false;
}
cetak("Baca Sensor Loop");
//POMPA= OFF=0, ON=1
//1 Basah <=300
//2 Lembab >301
//3 Kering <=701
long sensor1=300;
int sen1=3;
if(sensor1<=300){sen1=1;}
else if(sensor1<701){sen1=2;}
//1 Dingin <=25 C
//2 Panas >25
long sensor2=25;
int sen2=2;
if(sensor2<25){sen1=1;}
//============================================
int io1=0;
int io2=1;
//int arx[JS];
int *arx=NB(sen1,io1);
//int ary[JS];
int *ary=NB(sen2,io2);
cetak("Jika Sensor1: "+s(sensor1)+" ["+s(sen1)+"] dan Sensor2:"+s(sensor2)+" ["+s(sen2)+"]");
cetak("Jumlah ON:"+s(jON)+" |Jumlah OFF:"+s(jOFF));
cetak("Jumlah :"+s(sen1)+"-ON="+s(arx[0]));
cetak("Jumlah :"+s(sen1)+"-OFF="+s(arx[1]));
cetak("Jumlah :"+s(sen2)+"-ON="+s(ary[0]));
cetak("Jumlah :"+s(sen2)+"-OFF="+s(ary[1]));
int total=jON+jOFF;
float probON=(jON/total) * (arx[0]/jON) * (ary[0]/jON);
float probOFF=(jOFF/total) * (arx[1]/jOFF) * (ary[1]/jOFF);
cetak("Probablitias ON: ("+s(jON)+"/"+s(total)+") * ("+s(arx[0])+"/"+s(jON)+") * ("+s(ary[0])+"/"+s(jON)+")");
cetak("Probablitias OFF: ("+s(jOFF)+"/"+s(total)+") * ("+s(arx[1])+"/"+s(jOFF)+") * ("+s(ary[1])+"/"+s(jOFF)+")");
String dom="ON";
int hasil=1;
if(probOFF > probON){
dom="OFF";hasil=0;
cetak("Karena Probablitias OFF Lebih tinggi "+s(probOFF)+" >"+s(probON)+" => Sistem akan:"+dom);
}
else{
dom="ON";hasil=1;
cetak("Karena Probablitias ON Lebih tinggi "+s(probON)+" >"+s(probOFF)+" => Sistem akan:"+dom);
}
//============================================
delay(5000);
}
void cetak(String lap) {
Serial.println(lap);
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
void printArray( float a[][ MC ] ) {
for ( int i = 0; i < MR; ++i ) {
for ( int j = 0; j < MC; ++j )
Serial.print (String(a[ i ][ j ]) + " " );
Serial.print ("\n" ) ;
}
}
//=======================================================
REFF ARRAY FUNGSI:
int *array(int nbelt)
{
int *toret=0L, *ptr; // we must get 2 pointers
toret = (int *) malloc (nbelt*sizeof(int)); // try to allocate memory
if (toret) // toret != null so memory allocation success
{
ptr = toret; // ptr is pointing to toret
for (int i=0; i<nbelt; i++)
*ptr++ = 2*i; // assign a value to ptr and move the pointer
return toret; // return the pointer for address allocation
}
else // memory allocation failed, return 0L
return 0L;
}
void setup(void)
{
int *p=0L;
p = array(10); // Function returning an array of 10 int
if (p) // we have a result (p != OL)
{
for (int i=0; i<10; i++)
{
Seria.println(*(p+i), DEC); // print each elements
free(p); // don't forget to free Ram !
}
}
void loop(void)
{
}
Tidak ada komentar:
Posting Komentar