Hello Arduinoeers!
I'm currently heading a project that drives a solenoid. I implemented a bit-bang PWM for very low frequencies and variable duty cycle. I'm hung up on calculating the millisecond interval of the high level of the duty cycle. It seems as if the arduino speaks different math... ![]()
Here is my code. The function getDutyCycleDelay() should return 500 when frequency is 60 pulses/min at 50% duty cycle; but it returns 0. ![]()
Please Help.
int frequency; //Cycles per minute F/Min
int dutyCycle; //Values = 0-100; Represents %
const int pinOutCTR=9;Â //Pin driving solenoid
const int pinOutLED=13;Â //Solenoid Status via LED on board
const int minFreq=3;
const int maxFreq=60;
void setup()
{
 pinMode(pinOutCTR, OUTPUT);
 pinMode(pinOutLED, OUTPUT);
 frequency=60;        //Pulses per minute --> At 60 pulses per minute, that's 1000 ms.
 dutyCycle=50;        //Percent --> at 50% and 60 pulses per minute, the duty cycles should be high for 500 milliseconds.
 Serial.begin(9600);     // start serial for output
}
void loop()
{
Â
   digitalWrite(pinOutCTR, HIGH);
   digitalWrite(pinOutLED, HIGH);
   delay(getDutyCycleDelay());
   digitalWrite(pinOutCTR, LOW);
   digitalWrite(pinOutLED, LOW);
   delay(getFrequencyDelay()-getDutyCycleDelay());
}
long getDutyCycleDelay()
{
 ///*
 long returne=getFrequencyDelay()*(dutyCycle/100); //This calculates time solenoid should be on in respect to frequency **Should return 500 when frequency is 60 pulses/min.
 Serial.println(returne);
 return returne;
 //*/
 //return 500;
}
long getFrequencyDelay()
{
 return (60000/frequency);  //Translates (cycles/min) to microsecond intervals between pulses. **Should return 1000 when frequency is set to 60 pulses per minute. (Interval between pulses is 1000ms)
}