0

How do i interpret this code to python for my sensor

    void loop()
{

    volt = 0;
    for(int i=0; i<800; i++)
    {
        volt += ((float)analogRead(sensorPin)/1023)*5;
    }
    volt = volt/800;
    volt = round_to_dp(volt,1);
    if(volt < 2.5){
      ntu = 3000;
    }else{
      ntu = -1120.4*square(volt)+5742.3*volt-4353.8; 
    }
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(volt);
    lcd.print(" V");

    lcd.setCursor(0,1);
    lcd.print(ntu);
    lcd.print(" NTU");
    delay(10);
}
1

Question

Convert Arduino C++ turbidity measurement program to Rpi python

Answer

See Appendix A below.

Appendices

Appendix A - Buggy Answer V0.1

# printTurbidityMeasurement python program V0.1  tlfong01  2019may27hkt0926 
import time.sleep

while True

  totalVoltageReading = 0
  for count in range(800):
     currentVoltageReading = float(anlogRead(rpiGpioSensorPinNum / 1023) * 5)
     totalVoltageReading = totalVoltageReading + currentVoltageReading

  averageVolteageReading = totalVoltageReading / 800

  roundedUpAverageVoltageReading = round(averageVoltageReading)

  if roundedUpAverageVoltageReading < 2.5:
    nephelometricTurbidityUnit = 3000
  elif
    nephelometricTurbidityUnit = -(1120.4 * square(roundUpAverageVoltageReading) + (5742.3 * roundUpAverageVoltageReading - 4353.8) 

  lcd.clear()
  lcd.setCursor(0, 0)
  lcd.print(roundedUpAverageVoltageReading, "V")

  lcd.setCursor(0, 1) 
  lcd.print(nephelometricTurbidityUnit, "NTU")

  sleep(10)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.