//Pin A0 - anode of temperature sensing diode, cathode to ground
//Pin A1 - voltage sensor, positive only, maximum 5v, use resistive divider for measuring higher voltages and then multiply resuilt in calculating section of sketch
//pin A2 - tx output power sensor, rectified voltage from antenna connector, positive only, maximum 5v, use resistive divider for measuring higher voltages and then multiply resuilt in calculating section of sketch
//pin 3 - audio output, 64khz PWM, enough to drive headphones through the capacitor 10-100 uF
//pin 2 - transmitter PTT 
//You need a Talkie library for this sketch. In the arduino ide go to menu 'Tools', select 'manage libraries', search for Talkie and install it
#include <Arduino.h>

#include "Talkie.h"
#include "Vocab_US_Large.h" //library provides approx. 1000 words in 5 vocabularies with .c file extensions in folder documents/arduino/libraries/talkie/src, use notepad to read them
#include "Vocab_Special.h"
Talkie voice;

/* Say any number between -999,999 and 999,999 */
void sayNumber(long n) {
  if (n<0) {
    voice.say(sp2_MINUS);
    sayNumber(-n);
  } else if (n==0) {
    voice.say(sp2_ZERO);
  } else {
    if (n>=1000) {
      int thousands = n / 1000;
      sayNumber(thousands);
      voice.say(sp2_THOUSAND);
      n %= 1000;
      if ((n > 0) && (n<100)) voice.say(sp2_AND);
    }
    if (n>=100) {
      int hundreds = n / 100;
      sayNumber(hundreds);
      voice.say(sp2_HUNDRED);
      n %= 100;
      if (n > 0) voice.say(sp2_AND);
    }
    if (n>19) {
      int tens = n / 10;
      switch (tens) {
        case 2: voice.say(sp2_TWENTY); break;
        case 3: voice.say(sp2_THIR_); voice.say(sp2_T); break;
        case 4: voice.say(sp2_FOUR); voice.say(sp2_T);  break;
        case 5: voice.say(sp2_FIF_);  voice.say(sp2_T); break;
        case 6: voice.say(sp2_SIX);  voice.say(sp2_T); break;
        case 7: voice.say(sp2_SEVEN);  voice.say(sp2_T); break;
        case 8: voice.say(sp2_EIGHT);  voice.say(sp2_T); break;
        case 9: voice.say(sp2_NINE);  voice.say(sp2_T); break;
      }
      n %= 10;
    }
    switch(n) {
      case 1: voice.say(sp2_ONE); break;
      case 2: voice.say(sp2_TWO); break;
      case 3: voice.say(sp2_THREE); break;
      case 4: voice.say(sp2_FOUR); break;
      case 5: voice.say(sp2_FIVE); break;
      case 6: voice.say(sp2_SIX); break;
      case 7: voice.say(sp2_SEVEN); break;
      case 8: voice.say(sp2_EIGHT); break;
      case 9: voice.say(sp2_NINE); break;
      case 10: voice.say(sp2_TEN); break;
      case 11: voice.say(sp2_ELEVEN); break;
      case 12: voice.say(sp2_TWELVE); break;
      case 13: voice.say(sp2_THIR_); voice.say(sp2__TEEN); break;
      case 14: voice.say(sp2_FOUR); voice.say(sp2__TEEN);break;
      case 15: voice.say(sp2_FIF_); voice.say(sp2__TEEN); break;
      case 16: voice.say(sp2_SIX); voice.say(sp2__TEEN); break;
      case 17: voice.say(sp2_SEVEN); voice.say(sp2__TEEN); break;
      case 18: voice.say(sp2_EIGHT); voice.say(sp2__TEEN); break;
      case 19: voice.say(sp2_NINE); voice.say(sp2__TEEN); break;
    }
  }
}


//for termometer
int i;
float dtemp, dtemp_avg, t;

//two next values for termometer calibration
//measure temperature in room, voltage on diode and change this values 
const int t0 = 20.3; 
const float vf0 = 549.6; 

void setup() {
  pinMode(A0, INPUT_PULLUP); //turn on bias for temperature sensor diode
}

void loop() { 
   //turn on transmitter for measuring output power and... uhhhmm.. transmit??
 digitalWrite(2, HIGH);
 
//Measuring temp sensor diode voltage and calculating temperature in Celsius
         dtemp_avg = 0;
  for (i = 0; i < 1024; i++) {
    float vf = analogRead(A0) * (4976.30 / 1023.000);
    //Serial.println(vf);
    dtemp = (vf - vf0) * 0.4545454;
    dtemp_avg = dtemp_avg + dtemp;
  }
  t = t0 - dtemp_avg / 1024;

//say temperature
voice.say(sp2_TEMPERATURE);
sayNumber(t);
voice.say(sp2_DEGREES);   
  delay(1000);


//read ADC, calculating voltage and put value in variable
 int voltage = analogRead(A1) * 5.000 / 1.023;
 //say voltage
  sayNumber(voltage);
  voice.say(sp2_MILLI);
  voice.say(sp2_VOLTS);
  delay(1000);

  
//read ADC, calculating power on 50 ohm load and put on other variable
int PowValue = analogRead(A2) * (5.0 / 1.023);
float powerval = pow(PowValue, 2) / 50;
//say power in milliwatts 
voice.say(sp4_MEASURED);
voice.say(sp2_OUT);
voice.say(sp4_POWER);
sayNumber(powerval);
voice.say(sp2_MILLI);
voice.say(sp2_WATTS);

//turn off transmitter
digitalWrite(2, LOW);
//delay to next transmission, 10 seconds
delay(10000);
    }
