Adapt code to particle photon

hi guys, i need to adapt this code to my photon. I am a very newbie, but your help would be greatly appreciated… :slightly_smiling_face:
I am using this sensor in my project, but I only find the firware for use on arduino.I am trying to adjust it to my particle photon
Sensor : https://wiki.dfrobot.com/Analog_ORP_Meter_SKU_SEN0165_

@angeldb, the output voltage range of the board is not described so you have to assume that the voltage will not exceed 3.3v (or be less than zero volts) otherwise you will damage the Photon’s analog input pin circuitry.

As for the code, you need to keep these arduino/photon differences in mind:

  • The ADC on the arduino is 10 bits (0-1024) vs the 12 bits (0-4096) of the Photon

  • The Photon does not provide an accurate 5v supply. Instead, the Vin pin can be used to supply the USB voltage minus a diode voltage drop of about 0.3v. That diode is used for protection should you connect both the USB and an external voltage on Vin. This may affect accuracy as the ORP board generates its own -5v voltage. For best accuracy, you could supply the board with a separate 5v supply, connecting all grounds (5v supply, ORP board and Photon) together as a “common” ground.

  • As mentioned previously, analog input pins on the Photon cannot tolerate voltages higher than 3.3v whereas the arduino can handle 5v.

Given this, the code can be adapted. See comments where code was changed.

/*
# This sample codes is for testing the ORP meter V1.0.
 # Editor : YouYou
 # Date   : 2013.11.26
 # Product: ORP meter
 # SKU    : SEN0165
*/
#define VOLTAGE 3.30		//system voltage					<-- 3.30 for Photon, 5.00 for Arduino
#define OFFSET 0				//zero drift voltage
#define LED D7					//operating instructions	<-- D7 is the Photon onboard LED, pin 12 for the Arduino

double orpValue;

#define ArrayLenth  40		//times of collection
#define orpPin A1					//orp meter output,connect to Arduino controller ADC pin  <-- pin A1 for Photon, pin 1 for Arduino

int orpArray[ArrayLenth];
int orpArrayIndex=0;

double avergearray(int* arr, int number){
  int i;
  int max,min;
  double avg;
  long amount=0;
  if(number<=0){
    Serial.print("Error number for the array to average!/n");	// printf must be specified with a "stream", in this case Serial
    return 0;
  }
  if(number<5){   //less than 5, calculated directly statistics
    for(i=0;i<number;i++){
      amount+=arr[i];
    }
    avg = amount/number;
    return avg;
  }else{
    if(arr[0]<arr[1]){
      min = arr[0];max=arr[1];
    }
    else{
      min=arr[1];max=arr[0];
    }
    for(i=2;i<number;i++){
      if(arr[i]<min){
        amount+=min;        //arr<min
        min=arr[i];
      }else {
        if(arr[i]>max){
          amount+=max;    //arr>max
          max=arr[i];
        }else{
          amount+=arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount/(number-2);
  }//if
  return avg;
}


void setup(void) {
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
}

void loop(void) {
  static unsigned long orpTimer=millis();   //analog sampling interval
  static unsigned long printTime=millis();
  if(millis() >= orpTimer)
  {
    orpTimer=millis()+20;
    orpArray[orpArrayIndex++]=analogRead(orpPin);    //read an analog value every 20ms
    if (orpArrayIndex==ArrayLenth) {
      orpArrayIndex=0;
    }
    //orpValue=((30*(double)VOLTAGE*1000)-(75*avergearray(orpArray, ArrayLenth)*VOLTAGE*1000/1024))/75-OFFSET;	// Arduino
    orpValue=((30*(double)VOLTAGE*1000)-(75*avergearray(orpArray, ArrayLenth)*VOLTAGE*1000/4096))/75-OFFSET;		// Photon

    //convert the analog value to orp according the circuit
  }
  if(millis() >= printTime)   //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
    printTime=millis()+800;
    Serial.print("ORP: ");
    Serial.print((int)orpValue);
        Serial.println("mV");
        digitalWrite(LED,1-digitalRead(LED));
  }
}

I test compiled for DeviceOS 2.0.0 on a Photon without errors.