////////////////////////////////////////////////////////////////////////////////
// Name:       SR-010                                                         //
//             Sonar Range Finder SRF10                                       //
// Platform:   Arduino Uno R3                                                 //
// Created by: HARB, rboek2@gmail.com, May 2016, GPL copyrights               //
// http://robotigs.com/robotigs/includes/parts_header.php?idpart-208          //
// Read the SRF10 with TWI and display the result on the Serial Monitor.      //
// Connect only 1 SRF10 to this sketch at the same time.                      //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************
// Define precompiler settings PINS --------------------------------------------

///Define the needed header files for the precompiler, no charge if not used ---
#include <Wire.h>

// Define variables ------------------------------------------------------------
bool ledBinVal = LOW;         //You can chose HIGH-on or LOW-off for LED_BUILTIN
int  twiAdr = 0xE0;        //This is the new TWI-address of the connected SRF 10 
int  twiSR1 = 112;            //The default address in the datasheet is 224=0xE0
int  Range  = 25;              //255 is default = 65mS. Possible values: 0 - 255
int  Gain   = 16;             //16 is default = No gain. Possible values: 0 - 16
// We seem to must perform a ROR NC on the real address b4 the library reacts ok
                           // but i2c adressing uses the high 7 bits so it's 112
                                       //So 3 flashes means 0xE6 = 230 / 2 = 115
                         //This is a rather bad way to avoid ANDs for read/write
uint8_t  reading = 0;
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  Wire.begin();                         //Start i2c bus, no address to be master
  setTwiAddress();                                  //Reset the address of SRF10
  setSRFrange();                //Set the maximum measuring wait for return time
  setSRFgain();                            //Set the rise time of the microphone
}//--(end setup )---------------------------------------------------------------

void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  proces_SRF();                          //Read and show data Sonar Range Finder
  toggle_ledBin();                  //Toggles the default on-board LED on or off
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898


void setSRFgain(void) { //Set the rise time of the microphone ******************
  Wire.beginTransmission(twiSR1); //Start to occupy twi-bus and to select device
  Wire.write(byte(0x01));      //Sets register pointer to the gain register=0x01
  Wire.write(byte(Gain)); //Gain must be set according table of the manufacturer
  Wire.endTransmission(true);           //Stop transmitting and free the TWI-bus
} //Exit setSRFrange, RSet the maximum measuring wait for return time DONE -----


void setSRFrange(void) { //Set the maximum measuring wait for return time ******
  Wire.beginTransmission(twiSR1); //Start to occupy twi-bus and to select device
  Wire.write(byte(0x02));     //Sets register pointer to the range register=0x02
  Wire.write(byte(Range));                //Range times 43mm is the max distance
  Wire.endTransmission(true);           //Stop transmitting and free the TWI-bus
} //Exit setSRFrange, RSet the maximum measuring wait for return time DONE -----


void proces_SRF(void) { //Read and show data Sonar Range Finder ****************
  //Step 1:       Tell SRF10 to start ranging sequence by emitting a click sound
  Wire.beginTransmission(twiSR1); //Start to occupy twi-bus and to select device
  Wire.write(byte(0x00));   //Sets register pointer to the command register=0x00
  Wire.write(byte(0x51));                 //Command sensor to measure in cm=0x51
  Wire.endTransmission(true);           //Stop transmitting and free the TWI-bus
  //SRF-10 will respond by   a brief flash of the onboard LED and making a click
  
  //Step 2:         Pause until the click sound has echoed back or not even that
  Wire.beginTransmission(twiSR1); //Start to occupy twi-bus and to select device
  Wire.write(byte(0x00));               //Sets register pointer to 0x00, Version
  Wire.endTransmission(false);  //Stop transmitting, but do not free the TWI-bus
  reading = 0xFF;                                            //Set to enter loop
  while (reading == 0xFF) {                //Keep looping while measurement runs
    Wire.requestFrom(twiSR1, 1);              //Request 1 byte from slave device
    reading = Wire.read();       //Read single byte, overwrites previous reading
  } //Exit while (reading == 0xFF),                          see datasheet SRF10
  Serial.print("Version:");                                       //Results in 4
  Serial.print(reading);                        //Show the reading of register 0

  //Step 3:     Tell SRF10 to start to prepare to send me the 2 byte echo result
  Wire.beginTransmission(twiSR1); //Start to occupy twi-bus and to select device
  Wire.write(byte(0x02)); //Sets register pointer to echo #1 register=0x02, HIGH
  Wire.endTransmission(false);  //Stop transmitting, but do not free the TWI-bus

  //Step 4: Read data 2 bytes from sensor
  Wire.requestFrom(twiSR1, 2);               //Request 2 bytes from slave device

  //Step 5: If 2 bytes data are received then          Read data from the buffer
  if (2 <= Wire.available()) {    //If two bytes are received, both are buffered
    reading = Wire.read();         //Read high byte, overwrites previous reading
    reading = reading << 8;                  //Shift high byte to be high 8 bits
    reading |= Wire.read();                   //Receive low byte as lower 8 bits
    Serial.print("  Distance:");                                     //Show text
    Serial.print(reading);                                  // print the reading
    Serial.println(" cm");                                  // print the reading
  } //Exit  if (2 <= Wire.available)              Read data from the buffer done
} //Exit proces_SRF(void) Read and show data Sonar Range Finder ----------------



void toggle_ledBin(void) { //Toggles the default on-board LED on or off ********
  ledBinVal = !ledBinVal;                                         //Toggle value
  digitalWrite(LED_BUILTIN, ledBinVal);         //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------


void setTwiAddress(void) { //Reset the address of SRF10 ************************
  Wire.beginTransmission(twiSR1);
  Wire.write(byte(0x00));
  Wire.write(byte(0xA0));
  Wire.endTransmission();

  Wire.beginTransmission(twiSR1);
  Wire.write(byte(0x00));
  Wire.write(byte(0xAA));
  Wire.endTransmission();

  Wire.beginTransmission(twiSR1);
  Wire.write(byte(0x00));
  Wire.write(byte(0xA5));
  Wire.endTransmission();

  Wire.beginTransmission(twiSR1);
  Wire.write(byte(0x00));
  Wire.write(twiAdr);
  Wire.endTransmission();
} //Exit hangeAddress(void) Reset the address of SRF10 DONE --------------------


////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  - White cable to SRF-10    TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  - Yellow cable to SRF-10   TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - LED_BUILTIN Arduino      SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       -                          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  -                          PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    -                          PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       -                          PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  -                          DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      -                          DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                          PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                          PWM //
// ~4    -   6 - PD4 -   4 - PCINT20/XCK/T0    -                          PWM //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                          INT //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      -                          INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor           TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor           RCD //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -      - VCC              - Output to breadboard     VCC //
// RES   -   1 - RES -      - PCINT14/RESET    -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       -                          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       -                          ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  -                          TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  -                          TWI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898