/*

  Ocitavanje stanja tastera i prekidaca. Signalizacija pomocu dvije LED.
  
  created 2 Feb 2017
  by Nedjeljko lekic
  
*/
#include <SoftwareSerial.h>

#define CRVENA_LED 19  
#define ZUTA_LED 20    
#define ZELENA_LED 0  
#define PLAVA_LED 1   

#define TASTER 11  //Cetvrti bit PORTC
#define PREKIDAC 18  //Peti bit PORTD

SoftwareSerial Maketa(15, 16); // RX, TX

bool taster,prekidac;

//Postavljanje stanja dioda 
void SetLED(uint8_t Value)
{
  if(Value & 1) digitalWrite(CRVENA_LED, HIGH);
  else digitalWrite(CRVENA_LED, LOW);

  if(Value & 2) digitalWrite(ZUTA_LED, HIGH);
  else digitalWrite(ZUTA_LED, LOW);

  if(Value & 4) digitalWrite(ZELENA_LED, HIGH);
  else digitalWrite(ZELENA_LED, LOW);

  if(Value & 8) digitalWrite(PLAVA_LED, HIGH);
  else digitalWrite(PLAVA_LED, LOW);
}


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pinova na koje su prikljucene LED DIODE.
  pinMode(CRVENA_LED, OUTPUT);
  pinMode(ZUTA_LED, OUTPUT);
  pinMode(ZELENA_LED, OUTPUT);
  pinMode(PLAVA_LED, OUTPUT);  

  //Iskljucivanje dioda
  digitalWrite(CRVENA_LED, LOW);
  digitalWrite(ZUTA_LED, LOW);
  digitalWrite(ZELENA_LED, LOW);
  digitalWrite(PLAVA_LED, LOW);  

  // initialize digital pinova na koje su prikljuceni TASTER i PREKIDAC.
  pinMode(TASTER, INPUT_PULLUP);
  pinMode(PREKIDAC, INPUT_PULLUP);

  //Inicijalizacija komunikacije sa serijskim monitorom
  Serial.begin(9600);
  Maketa.begin(9600);
}

        
// The loop function runs over and over again forever
void loop() {

   taster=digitalRead(TASTER);
   prekidac=digitalRead(PREKIDAC);

   if (taster){Serial.print("Taster otpusten. ");Maketa.print("Taster otpusten. ");}
   else {Serial.print("Taster pritisnut. ");Maketa.print("Taster otpusten. ");}

   if (prekidac){Serial.println("Prekidac otvoren. ");Maketa.print("Taster otpusten. ");}
   else {Serial.println("Prekidac zatvoren. "); Maketa.print("Taster otpusten. ");}

   // Signalizacija stanja taster i prekidaca pomocu zute i crvene LED
   SetLED(2*taster+prekidac);
}
