 /*
  Template for Hmi Bluetooth connection with SoftwareSerial library
  
  The circuit: 
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)
 
 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts, 
 so only the following can be used for RX: 
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

*/

#include <hmi.h>

SoftwareSerial mySerial(10, 11); // RX pin, TX pin

int door1Pin= 4, door2Pin = 5, door3Pin = 6, door4Pin= 7;
int enginePin= 2, lightsPin= 3;

//Declaration of global variables here
int engineRPM, fuelLevel, waterTemp;
int lightsIntensity;
boolean turnOnEngine=false, turnOffEngine=false;
boolean lights;
boolean engineStatus=false;

void setup() {
    mySerial.begin(9600);
    
    Hmi.pinMode(door1Pin, INPUT);
    Hmi.pinMode(door2Pin, INPUT);
    Hmi.pinMode(door3Pin, INPUT);
    Hmi.pinMode(door4Pin, INPUT);
    
    Hmi.pinMode(enginePin, OUTPUT);
    Hmi.pinMode(lightsPin, OUTPUT);
    //Attach the global variables with the Hmi system here
    Hmi.attachIntOut(engineRPM,'a');
    Hmi.attachIntOut(fuelLevel,'b');
    Hmi.attachIntOut(waterTemp,'c');
    
    Hmi.attachIntIn(lightsIntensity,'d');
    
    Hmi.attachBooleanIn(turnOnEngine,'A');
    Hmi.attachBooleanIn(turnOffEngine,'B');
    Hmi.attachBooleanIn(lights,'C');
    
  }
  
  
 void loop(){
   //Your code here
   if(lights) analogWrite(lightsPin, lightsIntensity);
   else analogWrite(lightsPin,0);
   
   if(turnOnEngine){
     digitalWrite(enginePin,HIGH);
     engineStatus=true; 
   }
   
   if(turnOffEngine){
     digitalWrite(enginePin,LOW);
     engineStatus=false;
   }
   
   if(engineStatus)engineRPM = analogRead(0);
   else engineRPM= 0;

   waterTemp= analogRead(1);
   fuelLevel= analogRead(2);
   
   if(waterTemp>300){
     digitalWrite(enginePin,LOW);
     engineStatus=false;
   }
   
    Hmi.softSerial(mySerial); 
  }

