Hello Im trying to converter this C program into python i ha
Hello, Im trying to converter this C# program into python, i have started but am stuck on alot of small syntax! Please help me! Program name: Calibration
def Wired
def \"RTClib.h\"
int PWMSTEP = 2
int PWMSTART = 340
int PWMEND = 1023
int iENDPOINT
int correctionStep = 1
int correctionThreshold = 5
int fadeOutstep = 5
int numReadings = 5
float readings[numReadings]
int readIndex = 0
float total = 0
float average = 0
def SoftwareSerialmySerial = 7 8
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
def RED 0x1
def YELLOW 0x3
def GREEN 0x2
def TEAL 0x6
def BLUE 0x4
def VIOLET 0x5
def WHITE 0x7
int sensorPin = A1
int sensorPin2 = A2
int sensorValue = 0
int sensor2Value = 0
int isetPWM = 0
int usersetMaxFlow = 0
int usersetMinFlow = 0
int setFlow = 0
float fymin
bool DBUG = true
int rawflow = 0
int rawpressure = 0
def sflowrate
int flowrate = 0
int ipwm = 0
int oldipwm = 0
float flowdifference
int actualflowrate = 0
int actualpressure = 0
bool bnewGasIn
bool bovershoot = false
int imode
int led = LED_BUILTIN
int PWMPIN = 10
int PRESSUREPIN = A6
int RESET = 1
int SYSTEM_IDLE = 6
int GASIN = 2
int FADEIN = 13
int FADEOUT = 14
int RAWGASIN = 15
int STEP1 = 100
int STEP0 = 10
int STEP2 = 200
int STEP3 = 300
int STEP4 = 400
int STEP5 = 500
int MMIN = 16
int MMAX = 17
int MRUN = 18
int SYSTEM_STATE
int TEST_STATE
bool FLOW_MODE = true
float newpflow
def previousMillis = 0
int initialflowrate
def interval = 0
int hbcounter = 0
int buttonState = 0
def buf[20]
int incomingLen
int sblen
def c
bool bOkNum
float ymin
float ymax
float xmax
int pressureOffset
int targetpressure
int zeropressure
def startMillis
int address = 0
str flowDataStruct {
int deltaP[51]
int zeropressure;
}
int tdeltaP[51]
def DataStructflowData[3]
int itarget
int iindex
int ncages
void setup() {
int iret = EEPROM.readBlock(address, flowData,3)
def SYSTEM_STATE = SYSTEM_IDLE
def Timer1.initialize(1000); // msec so 2500=400Hz
def pinMode(PWMPIN, OUTPUT);
def Serial.begin(9600);
def mySerial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
EEPROM.readBlock(address, flowData,3);
print \"Prodigy Calibration\"
}
void loop() {
if (Serial.available()) {
c = Serial.read();
print \"incoming char: \"
// println(c);
if (c==63)
{
print \"Prodigy Calibration 001\"
print \"1,2,3 to start\"
print \"W to save to eeprom\"
print \"any char to abort\"
} else
if ((c==87)||(c==119))
{
print \"Calibration data\"
print \"one cage: zeroPressure=\");
print flowData[0].zeropressure
print flowData[0].deltaP[0]);
for (int i=1;i<51;i++)
{
print \",\"
print flowData[0].deltaP[i]
}
print \" \"
print \"two cages: zeroPressure=\"
print (flowData[1].zeropressure)
print (flowData[1].deltaP[0])
for (int i=0;i<51;i++)
{
print \",\"
print (flowData[1].deltaP[i])
}
print \" \"
print \"three cages: zeroPressure=\"
Serial.println(flowData[2].zeropressure);
Serial.print( flowData[2].deltaP[0]);
for (int i=0;i<51;i++)
{
print \",\"
print flowData[2].deltaP[i]
print \" \"
int iret = EEPROM.writeBlock(address, flowData,3);
print \"Saved.\"
} else
if (c == 49)
{
for (int i=0;i<51;i++)
{
def tdeltaP[i] = 0;
}
def itarget = 50;
def iindex = 0;
def iENDPOINT = 1000;
def ipwm = 0; //start pwm
def Timer1.pwm(PWMPIN, ipwm);
print \"Starting one cage calibration.\"
def SYSTEM_STATE = GASIN;
def TEST_STATE = STEP0;
def ncages = 0;
zeropressure = analogRead(PRESSUREPIN);
flowData[ncages].zeropressure = zeropressure;
} else
if (c == 50)
{
for (int i=0;i<51;i++)
{
tdeltaP[i] = 0;
}
itarget = 50;
iindex = 0;
iENDPOINT = 650;
ipwm = 0; //start pwm
Timer1.pwm(PWMPIN, ipwm);
print \"Starting two cage calibration.\"
SYSTEM_STATE = GASIN;
TEST_STATE = STEP0;
ncages = 1;
zeropressure = analogRead(PRESSUREPIN);
flowData[ncages].zeropressure = zeropressure;
} else
if (c == 51)
{
zeropressure = analogRead(PRESSUREPIN);
for (int i=0;i<51;i++)
{
tdeltaP[i] = 0;
}
itarget = 50;
iindex = 0;
iENDPOINT = 425;
ipwm = 0; //start pwm
Timer1.pwm(PWMPIN, ipwm);
print \"Starting three cage calibration.\"
SYSTEM_STATE = GASIN;
TEST_STATE = STEP0;
ncages = 2;
zeropressure = analogRead(PRESSUREPIN);
flowData[ncages].zeropressure = zeropressure;
} else
{
print \"Stopping.\"
SYSTEM_STATE = FADEOUT;
}
}
long currentMillis = millis();
long ltemp = (unsigned long) (currentMillis - previousMillis);
float flowdifference = (float)setFlow - (newpflow*10.0);
float tp;
int itemp;
if (SYSTEM_STATE==FADEOUT)
{
while (1) {
if (ipwm < 250)
ipwm -= 10;
else
if (ipwm > 500)
ipwm -= fadeOutstep*2;
else
ipwm -= 10;
if (ipwm<=0)
{
ipwm=0;
Timer1.pwm(PWMPIN, ipwm); //10bit
SYSTEM_STATE=SYSTEM_IDLE;
break;
}
Timer1.pwm(PWMPIN, ipwm); //10bit
}
} else
if (SYSTEM_STATE==GASIN)
{
switch (TEST_STATE)
{
// set PWM
case STEP0:
tp = analogRead(PRESSUREPIN);
total = 0;
// tp = ((float)tp*(float)5.0)/(float)1023.0;
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = tp;
total+=tp;
}
// itemp = (int)((total / numReadings)*1000.0);
itemp = (int)((total / numReadings));
print \"Start initial p = \"
print itemp
readIndex = 0;
startMillis = millis();
TEST_STATE = STEP1;
break;
// set zero offset
case STEP1:
tp = analogRead(PRESSUREPIN);
// tp = ((float)tp*(float)5.0)/(float)1023.0;
print \"actualpressure=\"
print tp
total = total - readings[readIndex];
readings[readIndex] = tp;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
zeropressure = (int)((total / numReadings));
// zeropressure = (int)((total / numReadings)*1000.0);
print \" p0=\"
print zeropressure
if (((unsigned long) (currentMillis - startMillis))>3000)
{
TEST_STATE = STEP2;
ipwm = PWMSTART;
Timer1.pwm(PWMPIN, ipwm); //10bit
startMillis = millis();
}
break;
// set PWM
case STEP2:
tp = analogRead(PRESSUREPIN);
// tp = ((float)tp*(float)5.0)/(float)1023.0;
total = total - readings[readIndex];
readings[readIndex] = tp;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
actualpressure = (int)((total / numReadings)*1000.0);
// actualpressure = (int)((tp)*1000.0);
actualpressure = (int)((tp));
if (((unsigned long) (currentMillis - startMillis))>7000) //10000)
{
print \"zp=\"
print zeropressure
print \" pressure=\"
print \",\"
print actualpressure
print \" pwm=\"
print \",\"
print ipwm
// tp = analogRead(A5);
// tp = ((float)tp*(float)5.0)/(float)1023.0;
//// Serial.print( \" Fv=\");
print \",\"
mySerial.write(\'F\');
mySerial.write(\'\ \');
delay(500);
incomingLen = mySerial.available();
if (incomingLen > 0) {
// Serial.println(\" \");
print \"newchar: incomingLen=\"
print incomingLen
print \"-\"
sblen=0;
bOkNum = false;
bool bcont = true;
for (int i=0;i {
c = mySerial.read();
if (c==13)
{
bcont = false;
bOkNum = true;
buf[sblen++]=0;
// break;
} else
if (bcont)
{
buf[sblen++]=c;
print \"-\"
print c
}
}
// Serial.println(\"#\");
if (bOkNum)
{
Str stemp = String((char *) buf);
tp = stemp.toFloat();
print \" flow=\"
print tp
print \"#\"
} else tp = -1.00;
print tp
int itemp1 = (int)(tp*100.0);
if (itemp1 >= itarget)
{
print \"target found: target=\"
print itarget
print \" deltaP=\"
print actualpressure
tdeltaP[iindex++] = actualpressure;
if (itarget==iENDPOINT)
{
SYSTEM_STATE=FADEOUT;
print \"\"
print \"Completed. Array is\"
for (int i=0;i<51;i++)
{
flowData[ncages].deltaP[i]=tdeltaP[i];
print tdeltaP[i]
print \",\"
}
print \" \"
}
itarget+=25;
}
}
// if (((long) (currentMillis - startMillis))>1500)
// {
if (itarget>=600)
PWMSTEP=5;
else
PWMSTEP=2;
ipwm+=PWMSTEP;
if (ipwm > PWMEND){ //changed from 255 for 10bit
SYSTEM_STATE=FADEOUT;
print \"\"
print \"Completed. Array is\"
for (int i=0;i<51;i++)
{
flowData[ncages].deltaP[i]=tdeltaP[i];
print tdeltaP[i]
print \",\"
}
print \" \"
}
Timer1.pwm(PWMPIN, ipwm); //10bit
startMillis = millis();
}
break;
}
}
}
Solution
def Wired
def \"RTClib.h\"
int PWMSTEP = 2
int PWMSTART = 340
int PWMEND = 1023
int iENDPOINT
int correctionStep = 1
int correctionThreshold = 5
int fadeOutstep = 5
int numReadings = 5
float readings[numReadings]
int readIndex = 0
float total = 0
float average = 0
def SoftwareSerialmySerial = 7 8
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
def RED 0x1
def YELLOW 0x3
def GREEN 0x2
def TEAL 0x6
def BLUE 0x4
def VIOLET 0x5
def WHITE 0x7
int sensorPin = A1
int sensorPin2 = A2
int sensorValue = 0
int sensor2Value = 0
int isetPWM = 0
int usersetMaxFlow = 0
int usersetMinFlow = 0
int setFlow = 0
float fymin
bool DBUG = true
int rawflow = 0
int rawpressure = 0
def sflowrate
int flowrate = 0
int ipwm = 0
int oldipwm = 0
float flowdifference
int actualflowrate = 0
int actualpressure = 0
bool bnewGasIn
bool bovershoot = false
int imode
int led = LED_BUILTIN
int PWMPIN = 10
int PRESSUREPIN = A6
int RESET = 1
int SYSTEM_IDLE = 6
int GASIN = 2
int FADEIN = 13
int FADEOUT = 14
int RAWGASIN = 15
int STEP1 = 100
int STEP0 = 10
int STEP2 = 200
int STEP3 = 300
int STEP4 = 400
int STEP5 = 500
int MMIN = 16
int MMAX = 17
int MRUN = 18
int SYSTEM_STATE
int TEST_STATE
bool FLOW_MODE = true
float newpflow
def previousMillis = 0
int initialflowrate
def interval = 0
int hbcounter = 0
int buttonState = 0
def buf[20]
int incomingLen
int sblen
def c
bool bOkNum
float ymin
float ymax
float xmax
int pressureOffset
int targetpressure
int zeropressure
def startMillis
int address = 0
str flowDataStruct {
int deltaP[51]
int zeropressure;
}
int tdeltaP[51]
def DataStructflowData[3]
int itarget
int iindex
int ncages
void setup() {
int iret = EEPROM.readBlock(address, flowData,3)
def SYSTEM_STATE = SYSTEM_IDLE
def Timer1.initialize(1000); // msec so 2500=400Hz
def pinMode(PWMPIN, OUTPUT);
def Serial.begin(9600);
def mySerial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
EEPROM.readBlock(address, flowData,3);
print \"Prodigy Calibration\"
}
void loop() {
if (Serial.available()) {
c = Serial.read();
print \"incoming char: \"
// println(c);
if (c==63)
{
print \"Prodigy Calibration 001\"
print \"1,2,3 to start\"
print \"W to save to eeprom\"
print \"any char to abort\"
} else
if ((c==87)||(c==119))
{
print \"Calibration data\"
print \"one cage: zeroPressure=\");
print flowData[0].zeropressure
print flowData[0].deltaP[0]);
for (int i=1;i<51;i++)
{
print \",\"
print flowData[0].deltaP[i]
}
print \" \"
print \"two cages: zeroPressure=\"
print (flowData[1].zeropressure)
print (flowData[1].deltaP[0])
for (int i=0;i<51;i++)
{
print \",\"
print (flowData[1].deltaP[i])
}
print \" \"
print \"three cages: zeroPressure=\"
Serial.println(flowData[2].zeropressure);
Serial.print( flowData[2].deltaP[0]);
for (int i=0;i<51;i++)
{
print \",\"
print flowData[2].deltaP[i]
print \" \"
int iret = EEPROM.writeBlock(address, flowData,3);
print \"Saved.\"
} else
if (c == 49)
{
for (int i=0;i<51;i++)
{
def tdeltaP[i] = 0;
}
def itarget = 50;
def iindex = 0;
def iENDPOINT = 1000;
def ipwm = 0; //start pwm
def Timer1.pwm(PWMPIN, ipwm);
print \"Starting one cage calibration.\"
def SYSTEM_STATE = GASIN;
def TEST_STATE = STEP0;
def ncages = 0;
zeropressure = analogRead(PRESSUREPIN);
flowData[ncages].zeropressure = zeropressure;
} else
if (c == 50)
{
for (int i=0;i<51;i++)
{
tdeltaP[i] = 0;
}
itarget = 50;
iindex = 0;
iENDPOINT = 650;
ipwm = 0; //start pwm
Timer1.pwm(PWMPIN, ipwm);
print \"Starting two cage calibration.\"
SYSTEM_STATE = GASIN;
TEST_STATE = STEP0;
ncages = 1;
zeropressure = analogRead(PRESSUREPIN);
flowData[ncages].zeropressure = zeropressure;
} else
if (c == 51)
{
zeropressure = analogRead(PRESSUREPIN);
for (int i=0;i<51;i++)
{
tdeltaP[i] = 0;
}
itarget = 50;
iindex = 0;
iENDPOINT = 425;
ipwm = 0; //start pwm
Timer1.pwm(PWMPIN, ipwm);
print \"Starting three cage calibration.\"
SYSTEM_STATE = GASIN;
TEST_STATE = STEP0;
ncages = 2;
zeropressure = analogRead(PRESSUREPIN);
flowData[ncages].zeropressure = zeropressure;
} else
{
print \"Stopping.\"
SYSTEM_STATE = FADEOUT;
}
}
long currentMillis = millis();
long ltemp = (unsigned long) (currentMillis - previousMillis);
float flowdifference = (float)setFlow - (newpflow*10.0);
float tp;
int itemp;
if (SYSTEM_STATE==FADEOUT)
{
while (1) {
if (ipwm < 250)
ipwm -= 10;
else
if (ipwm > 500)
ipwm -= fadeOutstep*2;
else
ipwm -= 10;
if (ipwm<=0)
{
ipwm=0;
Timer1.pwm(PWMPIN, ipwm); //10bit
SYSTEM_STATE=SYSTEM_IDLE;
break;
}
Timer1.pwm(PWMPIN, ipwm); //10bit
}
} else
if (SYSTEM_STATE==GASIN)
{
switch (TEST_STATE)
{
// set PWM
case STEP0:
tp = analogRead(PRESSUREPIN);
total = 0;
// tp = ((float)tp*(float)5.0)/(float)1023.0;
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = tp;
total+=tp;
}
// itemp = (int)((total / numReadings)*1000.0);
itemp = (int)((total / numReadings));
print \"Start initial p = \"
print itemp
readIndex = 0;
startMillis = millis();
TEST_STATE = STEP1;
break;
// set zero offset
case STEP1:
tp = analogRead(PRESSUREPIN);
// tp = ((float)tp*(float)5.0)/(float)1023.0;
print \"actualpressure=\"
print tp
total = total - readings[readIndex];
readings[readIndex] = tp;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
zeropressure = (int)((total / numReadings));
// zeropressure = (int)((total / numReadings)*1000.0);
print \" p0=\"
print zeropressure
if (((unsigned long) (currentMillis - startMillis))>3000)
{
TEST_STATE = STEP2;
ipwm = PWMSTART;
Timer1.pwm(PWMPIN, ipwm); //10bit
startMillis = millis();
}
break;
// set PWM
case STEP2:
tp = analogRead(PRESSUREPIN);
// tp = ((float)tp*(float)5.0)/(float)1023.0;
total = total - readings[readIndex];
readings[readIndex] = tp;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
actualpressure = (int)((total / numReadings)*1000.0);
// actualpressure = (int)((tp)*1000.0);
actualpressure = (int)((tp));
if (((unsigned long) (currentMillis - startMillis))>7000) //10000)
{
print \"zp=\"
print zeropressure
print \" pressure=\"
print \",\"
print actualpressure
print \" pwm=\"
print \",\"
print ipwm
// tp = analogRead(A5);
// tp = ((float)tp*(float)5.0)/(float)1023.0;
//// Serial.print( \" Fv=\");
print \",\"
mySerial.write(\'F\');
mySerial.write(\'\ \');
delay(500);
incomingLen = mySerial.available();
if (incomingLen > 0) {
// Serial.println(\" \");
print \"newchar: incomingLen=\"
print incomingLen
print \"-\"
sblen=0;
bOkNum = false;
bool bcont = true;
for (int i=0;i {
c = mySerial.read();
if (c==13)
{
bcont = false;
bOkNum = true;
buf[sblen++]=0;
// break;
} else
if (bcont)
{
buf[sblen++]=c;
print \"-\"
print c
}
}
// Serial.println(\"#\");
if (bOkNum)
{
Str stemp = String((char *) buf);
tp = stemp.toFloat();
print \" flow=\"
print tp
print \"#\"
} else tp = -1.00;
print tp
int itemp1 = (int)(tp*100.0);
if (itemp1 >= itarget)
{
print \"target found: target=\"
print itarget
print \" deltaP=\"
print actualpressure
tdeltaP[iindex++] = actualpressure;
if (itarget==iENDPOINT)
{
SYSTEM_STATE=FADEOUT;
print \"\"
print \"Completed. Array is\"
for (int i=0;i<51;i++)
{
flowData[ncages].deltaP[i]=tdeltaP[i];
print tdeltaP[i]
print \",\"
}
print \" \"
}
itarget+=25;
}
}
// if (((long) (currentMillis - startMillis))>1500)
// {
if (itarget>=600)
PWMSTEP=5;
else
PWMSTEP=2;
ipwm+=PWMSTEP;
if (ipwm > PWMEND){ //changed from 255 for 10bit
SYSTEM_STATE=FADEOUT;
print \"\"
print \"Completed. Array is\"
for (int i=0;i<51;i++)
{
flowData[ncages].deltaP[i]=tdeltaP[i];
print tdeltaP[i]
print \",\"
}
print \" \"
}
Timer1.pwm(PWMPIN, ipwm); //10bit
startMillis = millis();
}
break;
}
}
}














