Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to convert this Ardunio code to python! Please help!!! I needed this done yesterday!!!!!!!! Thank YOU! #include #include RTClib.h #include #include #include #include

I need to convert this Ardunio code to python! Please help!!! I needed this done yesterday!!!!!!!! Thank YOU!

#include #include "RTClib.h" #include #include #include #include #include int PWMSTEP = 2; const int PWMSTART = 340; const int PWMEND = 1023; int iENDPOINT;

const int correctionStep = 1; const int correctionThreshold = 5; const long monitorTime = 500; const int fadeOutstep = 5;

const int numReadings = 5; float readings[numReadings]; int readIndex = 0; float total = 0; float average = 0;

SoftwareSerial mySerial(7,8); // RX, TX

// The shield uses the I2C SCL and SDA pins. On classic Arduinos // this is Analog 4 and 5 so you can't use those for analogRead() anymore // However, you can connect other I2C sensors to the I2C bus and share // the I2C bus. Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// These #defines make it easy to set the backlight color #define RED 0x1 #define YELLOW 0x3 #define GREEN 0x2 #define TEAL 0x6 #define BLUE 0x4 #define VIOLET 0x5 #define WHITE 0x7 const int sensorPin = A1; const int sensorPin2 = A2; int sensorValue = 0; int sensor2Value = 0;

int isetPWM = 0; int usersetMaxFlow = 0; int usersetMinFlow = 0; int setFlow = 0; float fymin; const bool DBUG = true;

int rawflow = 0; int rawpressure = 0;

String sflowrate; int flowrate = 0; int ipwm = 0; int oldipwm = 0; float flowdifference; int actualflowrate = 0; int actualpressure = 0; boolean bnewGasIn;

boolean bovershoot = false; int imode;

const int led = LED_BUILTIN; const int PWMPIN = 10; // 10 bit should work with pin 9 as well

int PRESSUREPIN = A6;

const int RESET = 1; const int SYSTEM_IDLE = 6; const int GASIN = 2; const int FADEIN = 13; const int FADEOUT = 14; const int RAWGASIN = 15;

const int STEP1 = 100; const int STEP0 = 10; const int STEP2 = 200; const int STEP3 = 300; const int STEP4 = 400; const int STEP5 = 500;

const int MMIN = 16; const int MMAX = 17; const int MRUN = 18;

int SYSTEM_STATE; int TEST_STATE;

volatile bool FLOW_MODE = true; float newpflow; unsigned long previousMillis = 0; int initialflowrate; long interval = 0; int hbcounter = 0; int buttonState = 0; char buf[20]; int incomingLen; int sblen; byte c; boolean bOkNum; float ymin; float ymax; float xmax; int pressureOffset; int targetpressure; int zeropressure; unsigned long startMillis; // int address = 0; struct flowDataStruct { int deltaP[51]; int zeropressure; }; int tdeltaP[51]; flowDataStruct flowData[3]; int itarget; int iindex; int ncages;

void setup() {

//int iret = EEPROM.readBlock(address, flowData,3);

SYSTEM_STATE = SYSTEM_IDLE; Timer1.initialize(1000); // msec so 2500=400Hz pinMode(PWMPIN, OUTPUT);

Serial.begin(9600); mySerial.begin(9600); for (int thisReading = 0; thisReading < numReadings; thisReading++) { readings[thisReading] = 0; } EEPROM.readBlock(address, flowData,3); Serial.println("Prodigy Calibration"); }

void loop() {

if (Serial.available()) { c = Serial.read(); // Serial.print("incoming char: "); // Serial.println(c); if (c==63) { Serial.println("Prodigy Calibration 001"); Serial.println("1,2,3 to start"); Serial.println("W to save to eeprom"); Serial.println("any char to abort"); } else

if ((c==87)||(c==119)) { Serial.println("Calibration data"); Serial.print("one cage: zeroPressure="); Serial.println(flowData[0].zeropressure); Serial.print( flowData[0].deltaP[0]); for (int i=1;i<51;i++) { Serial.print(","); Serial.print( flowData[0].deltaP[i]); } Serial.println(" "); Serial.println("two cages: zeroPressure="); Serial.println(flowData[1].zeropressure); Serial.print( flowData[1].deltaP[0]); for (int i=0;i<51;i++) { Serial.print(","); Serial.print( flowData[1].deltaP[i]); } Serial.println(" "); Serial.println("three cages: zeroPressure="); Serial.println(flowData[2].zeropressure); Serial.print( flowData[2].deltaP[0]); for (int i=0;i<51;i++) { Serial.print(","); Serial.print( flowData[2].deltaP[i]); } Serial.println(" "); int iret = EEPROM.writeBlock(address, flowData,3); Serial.println("Saved.");

} else

if (c == 49) { for (int i=0;i<51;i++) { tdeltaP[i] = 0; } itarget = 50; iindex = 0; iENDPOINT = 1000; ipwm = 0; //start pwm Timer1.pwm(PWMPIN, ipwm); Serial.println("Starting one cage calibration."); SYSTEM_STATE = GASIN; TEST_STATE = STEP0; 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); Serial.println("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); Serial.println("Starting three cage calibration."); SYSTEM_STATE = GASIN; TEST_STATE = STEP0; ncages = 2; zeropressure = analogRead(PRESSUREPIN); flowData[ncages].zeropressure = zeropressure;

} else { Serial.println("Stopping."); SYSTEM_STATE = FADEOUT; } }

unsigned long currentMillis = millis(); unsigned 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)); //Serial.print("Start initial p = "); //Serial.println(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; //Serial.print("actualpressure="); //Serial.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); //Serial.print(" p0="); //Serial.println(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) {

// Serial.print("zp="); Serial.print(zeropressure); // Serial.print( " pressure="); Serial.print(","); Serial.print(actualpressure); // Serial.print( " pwm="); Serial.print(","); Serial.print(ipwm); // tp = analogRead(A5); // tp = ((float)tp*(float)5.0)/(float)1023.0; //// Serial.print( " Fv="); Serial.print(","); mySerial.write('F'); mySerial.write(' '); delay(500);

incomingLen = mySerial.available(); if (incomingLen > 0) { // Serial.println(" "); // Serial.print("newchar: incomingLen="); // Serial.print(incomingLen); // Serial.print("-"); sblen=0; bOkNum = false; boolean bcont = true; for (int i=0;i

} // Serial.println("#"); if (bOkNum) { String stemp = String((char *) buf); tp = stemp.toFloat(); // Serial.print(" flow="); Serial.println(tp); // Serial.println("#"); } else tp = -1.00;

// Serial.println(tp); int itemp1 = (int)(tp*100.0); if (itemp1 >= itarget) { // Serial.print("target found: target="); // Serial.print(itarget); // Serial.print(" deltaP="); // Serial.println(actualpressure); tdeltaP[iindex++] = actualpressure; if (itarget==iENDPOINT) { SYSTEM_STATE=FADEOUT; Serial.println(""); Serial.println("Completed. Array is");

for (int i=0;i<51;i++) { flowData[ncages].deltaP[i]=tdeltaP[i]; Serial.print( tdeltaP[i]); Serial.print(","); } Serial.println(" ");

} itarget+=25;

} } // if (((unsigned 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; Serial.println(""); Serial.println("Completed. Array is");

for (int i=0;i<51;i++) { flowData[ncages].deltaP[i]=tdeltaP[i]; Serial.print( tdeltaP[i]); Serial.print(","); } Serial.println(" "); } Timer1.pwm(PWMPIN, ipwm); //10bit startMillis = millis(); } break;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

Students also viewed these Databases questions