/*
RF02 TX duckweed collector

in arduino version 022

tested with MLAB modules with ATmega 328 at 16MHz
see RF02 library for a Band and Frequency
*/

//connections RF02:  (TX)
// SDI,  -> RF02   Atmega PB0 Arduino 8  cannot be changed
// SCK,  -> RF02   Atmega PB1 Arduino 9  cannot be changed
// nSEL, -> RF02   Atmega PB2 Arduino 10 cannot be changed
// nIRQ, <- RF02   Atmega PB4 Arduino 12 cannot be changed
// FSK: Pullupto VCC

#include <RF02.h>

unsigned int MotorOffset;
unsigned int RudderOffset;

void setup() 
{
  Serial.begin(9600);
  Serial.println("\nSTM02 init" );

  MotorOffset = analogRead(A0);            
  RudderOffset = analogRead(A1);            
 
  rf02_prepAll();

  Serial.println("done" );
}

void loop() 
{
  unsigned char _tx_buffer[]="mrc";
  unsigned int Motor;
  unsigned int Rudder;
  unsigned char Check;
  signed int ble;
  
  Motor = analogRead(A0);            
  Rudder = analogRead(A1);            

//!!!
  Serial.print(Motor, DEC);
  Serial.print(" * ");
  Serial.println(Rudder, DEC);

  Motor = MotorOffset - Motor;            
  Rudder=1023-Rudder;
  Rudder = 512 - ((RudderOffset - Rudder)<<2);
  Motor>>=2;
  Rudder>>=2;
  if (Motor>200) Motor=0;  // Odstranit pro programovani regulatoru
  if (Rudder>250) Rudder=0;  
//if (Rudder<71) Rudder=71;  

  Check=Motor+Rudder;
 
  _tx_buffer[0]=(unsigned char)Motor;
  _tx_buffer[1]=(unsigned char)Rudder;
  _tx_buffer[2]=Check;
  
  rf02_txdata( _tx_buffer, sizeof(_tx_buffer));
}