/*
  when pin 5 is low the output on the DAC increases
  when pin 4 is low the output on the DAC decreases
  DAC je pripojen na pin 11 a 13. CS je na pin 10.
*/
// pouzite knihovny
#include <SPI.h>
#include <MCP48x2.h>

#define MAX_VALUE 255                               // MCP4802 je 8-bit prevodnik, => 255

#define PIN_INC 3                                   // defininovani rust

#define PIN_DEC 2                                    // pokles

#define CSPIN 10                                    // datasheet

byte channelA = CHANNEL_A | GAIN_1 | MODE_ACTIVE;   // definovani pro kanal A;  gain_1 = 2,07V a gain_2= 4V

float i;
float outputValue = i;                              // hodnota výstupu


//create the DAC object, this example uses the MCP4802 8 bit DAC
MCP48x2 dac(MCP4802, CSPIN);                        // ???


void setup() {

  Serial.begin(9600);
  SPI.begin();
  //we only use channel A in this exampe so lets shutdown channel B
  dac.send(CHANNEL_B | MODE_SHUTDOWN, 0);
  //set the output on channel A to zero volts
  dac.send(channelA, 0);
}

void loop() {

  for (i = 0; i < 255; i) {                         //cyklus for pro jendotlive hodnoty
    i = i + 25.5;                                   // krok mezi jednolivyma hodnotama


    if (digitalRead(PIN_DEC) == LOW) {                //nastaveni, kdyz i<0, tak i=0 a posle na CH_A
      outputValue -= 1;
      if (i < 0) i = 0;
      dac.send(channelA, i);

    }

    if (digitalRead(PIN_INC) == LOW) {                //nastaveni, kdyz i>maximalni hodnota, tak i=max a posle na CH_A
      outputValue += 1;
      if (i > MAX_VALUE) i = MAX_VALUE;
      dac.send(channelA, i);
    }


    Serial.print("DAC_hodnota= ");                    // vypis na serial monitor
    Serial.println(outputValue);
    Serial.println(i);


    delay(1000);                                      // zpozdeni 5s

  }

}


//**********************************************************************************************************************************
