#include <avr/io.h>
#include <SoftwareSerial.h>


SoftwareSerial srl(PD1,PD0);
/* A simple ADC example that checks the analog reading on ADC0 and turns
 * an LED on if the reading is higher than a threshold value and turns if
 * off if it is under that value. */
#include <avr/io.h>
#include <stdint.h>

/* Which analog pin we want to read from.  The pins are labeled "ADC0"
 * "ADC1" etc on the pinout in the data sheet.  In this case ADC_PIN
 * being 0 means we want to use ADC0.  On the ATmega328P this is also
 * the same as pin PC0 */
#define ADC_PIN1     0
#define ADC_PIN2     PC1

/* Just the pin we are going to be connecting the LED to PB0 is the same
 * as PIN0 but since we will be using PORTB it makes sense to use the 
 * name PB0 */
#define LED_PIN     PB0

/* The ADC value we will consider the cutoff point for turning the LED
 * on or off.  The ADC we are using is 10-bits so can be a value from
 * 0 to 1023.  The value 0 means that there is no voltage on the ADC pin
 * and the value 1023 means the voltage has reached the voltage on the 
 * AREF pin. */
#define ADC_THRESHOLD     600

/* This function just keeps the reading code out of the loop itself.
 * It takes the analog pin number as a parameter and returns the
 * analog reading on that pin as a result.
 *
 * Look for its definition below main. */
uint16_t adc_read(uint8_t adcx);



int main(void) {
srl.begin(9600);
  /* Enable the ADC */
  ADCSRA |= _BV(ADEN);

  /* Set the LED pin as an output. */
  DDRB  |= _BV(LED_PIN);


DDRB = (1 << DDB1) | (1 << DDB2);
    //DDRB |= (1 << DDB0);     //DDRB is port B direction register; set DDB1 to 1
    // PD6 is now an output

    OCR1A = 128;   //A clock
    OCR1B = 128;   //B clock 
    // set PWM for 50% duty cycle

    TCCR1A |= (1 << COM0A1) | (1 << COM0B1); 
    // set none-inverting mode on both A and B ports

    TCCR1A |= (1 << WGM01) | (1 << WGM00);
    // set fast PWM Mode

    TCCR1B |= (1 << CS01);
    // set prescaler to 8 and starts PWM



  ////timer stuff
    // Set the Timer Mode to CTC
    TCCR0A |= (1 << WGM01);
    TCCR0B |= (1 << WGM01);

    // Set the value that you want to count to
    OCR0A = 0xF9;
    OCR0B = 0xF9;

    TIMSK0 |= (1 << OCIE0A);    //Set the ISR COMPA vect

    sei();         //enable interrupts

    TCCR0B |= (1 << CS02);
    // set prescaler to 256 and start the timer



  /* continually check if the ADC value is greater than the
   * defined ADC_THRESHOLD, and turn servos off/on */
  for (;;) {
  int a = adc_read(ADC_PIN1);
  int b = adc_read(ADC_PIN2);


  if(a > ADC_THRESHOLD){
       srl.println('a');   
      OCR1A = 100;   //A clock 
  }

  else{
    srl.println(' ');
        OCR1A = 750;   //A clock
  }



  if(b > ADC_THRESHOLD){
       srl.println('b');   
      OCR1B = 1000;   //B clock 
  }

  else{
    srl.println(' ');
        OCR1B = 750;   //B clock
  }

}
}

uint16_t adc_read(uint8_t adcx) {
  /* adcx is the analog pin we want to use.  ADMUX's first few bits are
   * the binary representations of the numbers of the pins so we can
   * just 'OR' the pin's number with ADMUX to select that pin.
   * We first zero the four bits by setting ADMUX equal to its higher
   * four bits. */
  ADMUX &=  0xf0;
  ADMUX |=  adcx;

  /* This starts the conversion. */
  ADCSRA |= _BV(ADSC);

  /* This is an idle loop that just wait around until the conversion
   * is finished.  It constantly checks ADCSRA's ADSC bit, which we just
   * set above, to see if it is still set.  This bit is automatically
   * reset (zeroed) when the conversion is ready so if we do this in
   * a loop the loop will just go until the conversion is ready. */
  while ( (ADCSRA & _BV(ADSC)) );

  /* Finally, we return the converted value to the calling function. */
  return ADC;
}

