Arduino PWM

Hi i am using arduino PWM output to drive Proportional inductor coil.

My code is as mentioned below.

Here i am reading analog input from analog channel 0 and mapping it to PWM duty cycle between min and max setting.

My circuit and code is working good.

Here i want to implement another function.

If my analog input rise from 0-5V suddenly assume 10-15 ms my First PWM pulse should be on for 20ms and then it should follow normal below mentioned code.

Please help me in resolving this.

float analogOutPin= 3;
float analogInPin1= A0;
float analogInPin2= A2;
float analogInPin3= A3;
float inputvalue=0;
float minvalue=0;
float maxvalue=0;
float outputvalue=0;
float minsetting=0;
float maxsetting=0;
const int Ready= 5;
void setup()
{
TCCR2B=TCCR2B & 0b11111000 | 0x04;// PWM Frequency Selection (0x01=31372.55) (0x02=3921.16) (0x03=980.39) (0x04=490.20) <--DEFAULT (0x05=245.10) (0x06=122.55) (0x07=30.64)
pinMode(Ready, OUTPUT);
}
void loop()
{
digitalWrite(Ready,HIGH);
inputvalue=analogRead(analogInPin1);
minvalue=analogRead(analogInPin2);
minsetting=map (minvalue,0,1023,0,120);
maxvalue=analogRead(analogInPin3);
maxsetting=map (maxvalue,0,1023,0,120);
outputvalue=map(inputvalue, 0, 1023, minsetting,maxsetting);
analogWrite(analogOutPin, outputvalue);
delay(2);
}

Instead of analogWrite(analogOutPin, outputvalue); you could put something like...

if(suddenRiseConditionIsMet(inputvalue)) digitalWrite(analogOutPin); else  analogWrite(analogOutPin, outputvalue);

The tricky part is that you don't control the cycle of the PWM. So even if you precisely timed the 20ms, you might end that period during the PWM on-time, so the pin is held on 'too long'. If that is important to you, then you need to dig deeper into the registers and reset timer 2 when the 20ms is over.

The function which does all the work would look something like...

bool suddenRiseConditionIsMet(int input) {
  const int lowThreshold = 20; //pick your own numbers here
  const int highThreshold = 500; 
  const int riseTime = 10; //milliseconds, rises shorter than this will be considered to be 'sudden'
  const int holdTime = 20; //milliseconds, time to hold the output on when the input did rise suddenly
  static unsigned long lastTimeBelowLow;
  static unsigned long conditionMetTime;
  static bool conditionWasMet = false;

  if(conditionWasMet) {
    if(millis() - conditionMetTime > holdTime) {
      //period has expired - return false
      conditionWasMet = false;
      return false;
    } else {
      //we're inside the 20ms timing period - return true
      return true;
    }
  }

  //we can only get here if we are waiting for a sudden rise
  if(input < lowThreshold) {
    lastTimeBelowLow = millis();
    return false;
  }

  if(input > highThreshold && millis()-lastTimeBelowLow <= riseTime) {
    conditionWasMet = true;
    conditionMetTime = millis();
    return true;
  }

  //we get here when we're above the low threshold but below high OR the rise wasn't sudden
  return false;
}

Thanks for your reply.

The problem i am facing here is to detect sudden rise in anlaog input pin.
How to detect sudden rise in analog pin.

Thanks

mohan_hv:
Thanks for your reply.

The problem i am facing here is to detect sudden rise in anlaog input pin.
How to detect sudden rise in analog pin.

Thanks

Hi,

It sounds easy: divide the voltage delta by the time delta and compare it to what you deem to be "sudden".

Regards.

Thank for reply.

I am not much good in programming.

A sample code will be helpful.

Thanks