Question

In: Computer Science

Hello, Im trying to converter this C# program into python, i have started but am stuck...

Hello, Im trying to converter this C# program into python, i have started but am stuck on alot of small syntax! Please help me!

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('\r');
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<incomingLen;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;

}

}


}

Solutions

Expert Solution

A)

def=input(Prodigy Calibration)
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"
A=input("Prodigy Calibration")
}
void loop() {
if (Serial.available()) {
c = Serial.read();
print "incoming char: "
n=input("income char")
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('\r');
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<incomingLen;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;
}
}

}


Related Solutions

Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
I am trying to make a Risk Management tool in Python. I have it partially started....
I am trying to make a Risk Management tool in Python. I have it partially started. The scenario is that the Project Manager needs to be able to log on and enter information ( the required information is located in the code). I then need to capture that data and store it in an array with the ability to call back and make changes if necessary. Could you please help me out and explain what was done? Current code: Start...
This program needs to be in C++. I have barely gotten anywhere but am already stuck....
This program needs to be in C++. I have barely gotten anywhere but am already stuck. Any help with this one would be greatly appreciated. Write a program that simulates playing a simplified version of the game "Candy Land." In "Candy Land" players take turns drawing cards that have a colored square on them. The player then travels on the board according to the color on the card. The first person to reach the end of the board wins. In...
Hello this is for C++ language. I am currently stuck on creating my api for Day...
Hello this is for C++ language. I am currently stuck on creating my api for Day Trading Stocks. as follows I need an api for *//Function Signature * * parameter: * * Return Value: ** *// Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the...
Hello, I stuck doing c++ program The program will be recieved from the user as input...
Hello, I stuck doing c++ program The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to...
New to C programming and I am stuck. The program will print "no input" if no...
New to C programming and I am stuck. The program will print "no input" if no input is given. If a command line argument is given the program will print "input:" followed by the user input. Below is the code I put together to do as desired but I get errors. What do I need to fix to get my code to compile correctly? #include <stdio.h> #include <stdlib.h> int main () { char input; scanf("%c", &input); if (input == NULL)...
Hello, I have this question that I have been trying to solve for days. I am...
Hello, I have this question that I have been trying to solve for days. I am always able to solve the first part of the question, but I am never able to get the second part. Please help me understand how to set up this problem so I am able to solve it! Q: Let's revisit the banked curves from earlier to see another reason they are useful.We are building a road, and at one place we need to make...
I have been able to determine a. but am stuck on b. and c. for this...
I have been able to determine a. but am stuck on b. and c. for this question... If Wild Widgets, Inc., were an all-equity company, it would have a beta of 0.9. The company has a target debt–equity ratio of .4. The expected return on the market portfolio is 12 percent, and Treasury bills currently yield 4.1 percent. The company has one bond issue outstanding that matures in 20 years and has a coupon rate of 7.2 percent. The bond...
Hello, I have been trying to answer this question for the last hour and I am...
Hello, I have been trying to answer this question for the last hour and I am still struggling could someone help me? The deadline is in 1hour! Perform an analysis of variance on the following data set. Do this by answering the questions below. Group 1 Group 2 Group 3 82 87 97 91 90 99 93 91 104 94 99 105 94 101 106 95 115 109 99 118 110 101 114 103 117 105 121 106 121 106...
Hello everyone! I have been stuck on this problem in my python 3 coding class. Is...
Hello everyone! I have been stuck on this problem in my python 3 coding class. Is there anybody who can see what I am doing wrong? The wings are .50 cents each, If I input sour and want 20 wings it should output a 0.15 discount. I just can't get it to work but I feel like I am really close. Thank you Code: #Variables answer = str() wings = int() rate = float() discount = float() subtotal = float()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT