/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor

 This example code is in the public domain.
 */


//NOTE: we are using 1.1V VRef, so keep ADC input low.

//current
int ADC_VAL0 = A1;
//
int ADC_VAL1 = A4;



//smoothing
const int numReadings = 100;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int readings2[numReadings];      // the readings from the analog input
int readIndex2 = 0;              // the index of the current reading
double total2 = 0;               // the running total
int average2 = 0;                // the average

int start = 0;
int ADCVAL0_State = 0;
int ADCVAL1_State = 0;
int writeresult = 0;

// the setup routine runs once when you press reset:
void setup() {

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
    readings2[thisReading] = 0;
  }
   

  
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the ADC_VAL0's pin an input:
  //pinMode(ADC_VAL0, INPUT);

 analogReference(INTERNAL);
 //to find where arduino does this, grep -r analogReference in Arduino install folder
 //cryptic shit though
 //so instead, just fprint the register before and after the change. much more intuitive.

  //do it manually...


  
  //this doesn't work
  //ADMUX = (0 << REFS1) | (0 << REFS0) ;

  //this doesn't work
  //ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX0);

  //this doesn't work
  //ADMUX = ADMUX | 0b11001110;

  //this doesn't work
  //ADMUX = (1 << REFS1) | (1 << REFS0) ;

 //this also doesn't work
  //ADMUX |= (1 << REFS0);
  //ADMUX |= (1 << REFS1);
  
  //search engines have been fucked over with endless forum debates, hopeless. a blog post guide would suffice.

  //even this doesn't work. why... it should work. must be wiring code breaking things. give up for now.
  //ADMUX = ADMUX | 0b11000000;


  
  
  
  
}

// the loop routine runs over and over again forever:
void loop() {

  // subtract the last reading:
  total = total - readings[readIndex];
  total2 = total2 - readings2[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(ADC_VAL0);
  readings2[readIndex] = analogRead(ADC_VAL1);
  // add the reading to the total:
  total = total + readings[readIndex];
  total2 = total2 + readings2[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
    start = 1;
    writeresult = 1;
  }

  // calculate the average:
  average = total / numReadings;
  average2 = total2 / numReadings;
  
  // send it to the computer as ASCII digits
  if(start){
  ADCVAL0_State = average;
  ADCVAL1_State = average2;
  }

  
  // read the input pin:
  //int ADCVAL0_State = analogRead(ADC_VAL0);
  if (writeresult){
  //int ADCVAL1_State = analogRead(ADC_VAL1);
  
  // print out the state of the button:
  Serial.print(" ADCVAL0 Current in mA, ");
  Serial.print(ADCVAL0_State);
  Serial.print(",");
  Serial.print(" ADCVAL1 Battery Voltage divby 10, "); 
  Serial.println(ADCVAL1_State);
  delay(5000);        // delay in between reads for stability
  
  writeresult = 0;
  }

  //Trying to debug why I cant change a bit in a register. fucking wiring. fuck search engines.
  //int dog = ADMUX;
  //Serial.println(ADMUX,BIN);
}



