Waiting Long Time Before Doing Section Inside of Function

Hi all,

I am a school student working on a project that controls a aircon via IR (it also uses current and temperature sensors)

I have a problem though where it starts the Function called "Mode"

/*PUT AIRCON INTO COOL MODE*/
void Mode() {
  nVPP = getVPP();//read current of aircon
  nCurrThruResistorPP = (nVPP / 200.0) * 1000.0; //read current of aircon
  nCurrThruResistorRMS = nCurrThruResistorPP * 0.707; //read current of aircon
  nCurrentThruWire = nCurrThruResistorRMS * 1000; //read current of aircon

  if (nCurrentThruWire > triggerCurrentAircon) { //aircon in stand in cool,dry,auto etc
    delayMicroseconds(44060);
    pulseIR(9240);
    delayMicroseconds(4600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delay (10000);

The above code repeats multiple times........
Before getting to the below section


  delay(10000);
  nVPP = getVPP(); //read current of aircon
  nCurrThruResistorPP = (nVPP / 200.0) * 1000.0; //read current of aircon
  nCurrThruResistorRMS = nCurrThruResistorPP * 0.707; //read current of aircon
  nCurrentThruWire = nCurrThruResistorRMS * 1000; //read current of aircon

  if (nCurrentThruWire < triggerCurrentAircon) { //aircon in in heat mode put into cool mode
    Serial.print ("Entering cool mode!");
    delayMicroseconds(44060);
    pulseIR(9240);
    delayMicroseconds(4600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
  }
  delay(10000);

And the code repeats again (cut that bit out to reduce characters)
  }

I all goes perfect until it gets to this section in the "Mode" function

delay(10000);
  nVPP = getVPP(); //read current of aircon
  nCurrThruResistorPP = (nVPP / 200.0) * 1000.0; //read current of aircon
  nCurrThruResistorRMS = nCurrThruResistorPP * 0.707; //read current of aircon
  nCurrentThruWire = nCurrThruResistorRMS * 1000; //read current of aircon

  if (nCurrentThruWire < triggerCurrentAircon) { //aircon in in heat mode put into cool mode
    Serial.print ("Entering cool mode!");
    delayMicroseconds(44060);
    pulseIR(9240);
    delayMicroseconds(4600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(600);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
    delayMicroseconds(1720);
    pulseIR(600);
  }
  delay(10000);
And the above code repeats again (cut that bit out to reduce characters)

The problem is it waits a long time before doing this section (30/60 seconds).

Just to clarify this bit of code does what it should apart from waiting so long before implementing it!

Any help with this would be amazing!

Thanks,

Zeb

P.S. Full code attached:)

IR-Factory-Air-Con-Controller.ino (35.3 KB)

I wonder if this line has anything to do with delaying.

delay(10000);

Hi jremington,

That lines delays it, but shouldn't it only delay it for 10 seconds not 30/60 seconds?

Thanks,

Zeb

Take out that line and see.

A vague statement like "30/60 seconds" does not inspire confidence in your measurement of time.

OK I will take out that line and if it still delays I will measure the delay exactly!

Thanks,

Zeb

I suggest to also study how the function getVPP() works, and determine how long it takes to execute.

It may be that the original programmer put in that 10 second delay for a good reason, for example to ensure that the machine function and current flow is stable.

Without delay line of code = 55 seconds
With delay line of code = 1 minute and 5 seconds

I don't think it's the "getVPP" function because I use that multiple time throughout the "Mode" function with a 10 second delay as well and it works great.

Any ideas?

Thanks for your help,

Zeb

You will need to study the code to figure out what is taking so long. The getVPP() function takes slightly more than 1 second to execute, for example.

But then, as a glance at the code reveals, the mode() function has not one, but many

delay(10000);

statements scattered through it.

.....This is the original code for measuring current:

int sensorTA12 = A0; // Analog input pin that sensor is attached to

float nVPP;   // Voltage measured across resistor
float nCurrThruResistorPP; // Peak Current Measured Through Resistor
float nCurrThruResistorRMS; // RMS current through Resistor
float nCurrentThruWire;     // Actual RMS current in Wire

void setup()
{
  Serial.begin(9600);
  pinMode(sensorTA12, INPUT);
}


void loop()
{


  nVPP = getVPP();

  /*
    Use Ohms law to calculate current across resistor
    and express in mA
  */

 nCurrThruResistorPP = (nVPP / 200.0) * 1000.0;

  /*
    Use Formula for SINE wave to convert
    to RMS
  */

 nCurrThruResistorRMS = nCurrThruResistorPP * 0.707;

  /*
    Current Transformer Ratio is 1000:1...

    Therefore current through 200 ohm resistor
    is multiplied by 1000 to get input current
  */

  nCurrentThruWire = nCurrThruResistorRMS * 1000;


  Serial.print("Volts Peak : ");
  Serial.println(nVPP, 3);


  Serial.print("Current Through Resistor (Peak) : ");
  Serial.print(nCurrThruResistorPP, 3);
  Serial.println(" mA Peak to Peak");

  Serial.print("Current Through Resistor (RMS) : ");
  Serial.print(nCurrThruResistorRMS, 3);
  Serial.println(" mA RMS");

  Serial.print("Current Through Wire : ");
  Serial.print(nCurrentThruWire, 3);
  Serial.println(" mA RMS");

  Serial.println();




}


/************************************
  In order to calculate RMS current, we need to know
  the peak to peak voltage measured at the output across the
  200 Ohm Resistor

  The following function takes one second worth of samples
  and returns the peak value that is measured
*************************************/


float getVPP()
{
  float result;
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  uint32_t start_time = millis();
  while ((millis() - start_time) < 1000) //sample for 1 Sec
  {
    readValue = analogRead(sensorTA12);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
  }

  // Convert the digital data to a voltage
  result = (maxValue * 5.0) / 1024.0;

  return result;
}

It has no 10 second delays, the 10 second delays are what I have added so it changes the modes on the aircon slowly!

Is it possible it's exiting the "Mode" function and running this line at the top?

timeSinceAirconTouched = millis(); //remember the current millis

And then waiting until:

  if (DHT11.temperature > turnAirconOnTemp && millis() - timeSinceAirconTouched >= setTimeSinceAirconTouched && nCurrentThruWire > triggerCurrentAircon) { //if the temperature gets to hot and it has been over ? minutes since we have touched/controlled the aircon

Thanks,

Zeb

jremington:
You will need to study the code to figure out what is taking so long. The getVPP() function takes more than 1 second to execute, for example.

As a quick test, I would put several "Serial.println(millis())" statements in the code, to time various sections.

Thank you, will do!

EDIT: PUT WRONG LINE OF CODE (if DHT11.temperature > turn....)

Hi jremington,

All fixed!

It was a simple problem of putting all the delay 10 seconds outside of the "if" statements.

Thanks for all your help!

Thanks again,

Zeb