I want a loop to be executed "x" times.
const byte x = 8;
After this execution I want to suspend execution during time "interval1" and after that resume, etc..
int long interval1 = 5000; // 5 seconds
However the code below is being executed without any suspended period.
What is wrong here?
// https://forum.arduino.cc/index.php?topic=410853.15
// http://forum.arduino.cc/index.php?topic=507719.0
// Test PWM period and width measurement (connect pin 3 to pin 8)
// Measurement timing diagram: http://i.imgur.com/NqV4nXe.png
// code without the PWM generating part
volatile word timerValue[4];
volatile byte testState = 0;
boolean newValuesAvailable = false;
float pwmPeriod, pwmWidth, pwmDuty, pwmFrequency;
const byte x = 8; // loop value: loop amount = x+2 because min and max values are removed
unsigned long previousMillis1 = 0; // timer 1
const long interval1 = 5000; // interval between readings
int long interval; // intermediate storage for interval
unsigned int dutyCycleFinal; // final result for duty cycle
void setup()
{
// TIMSK0 = 0; // for testing with timer0 disable
Serial.begin(115200);
}
void loop()
{
int repeat1 = 0;
unsigned int signalMax1 = 0; // max value for duty cycle
unsigned long int signalMin1 = 100; // min value for duty cycle
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis1) >= interval)
{
previousMillis1 = currentMillis;
getPwmValues();
if (newValuesAvailable)
{
while (repeat1 < x)
{
repeat1++;
newValuesAvailable = false;
Serial.print("pwmPeriod ");
Serial.print(pwmPeriod, 3);
Serial.println(" us");
Serial.print("pwmWidth ");
Serial.print(pwmWidth, 3);
Serial.println(" us");
Serial.print("pwmDuty ");
Serial.print(pwmDuty, 3);
Serial.println(" %");
Serial.print("pwmFrequency ");
Serial.print(pwmFrequency, 3);
Serial.println(" kHz");
Serial.println();
}
}
// delay(500000);
}
}
void pwmMeasureBegin()
{
TCCR1A = 0; // normal operation mode
TCCR1B = 0; // stop timer clock (no clock source)
TCNT1 = 0; // clear counter
TIFR1 = bit (ICF1) | bit (TOV1); // clear flags
testState = 0; // clear testState
TIMSK1 = bit (ICIE1); // interrupt on input capture
TCCR1B = bit (CS10) | bit (ICES1);// start clock with no prescaler, rising edge on pin D8
}
ISR (TIMER1_CAPT_vect)
{
switch (testState) {
case 0: // first rising edge
timerValue[0] = ICR1;
testState = 1;
break;
case 1: // second rising edge
timerValue[1] = ICR1;
TCCR1B &= ~bit (ICES1); // capture on falling edge (pin D8)
testState = 2;
break;
case 2: // first falling edge
testState = 3;
break;
case 3: // second falling edge
timerValue[2] = ICR1;
testState = 4;
break;
case 4: // third falling edge
timerValue[3] = ICR1;
testState = 5; // all tests done
TCCR1B = 0; //stop timer
break;
}
}
void getPwmValues()
{
static boolean measurementInProcess = false;
if (!measurementInProcess)
{
pwmMeasureBegin();
measurementInProcess = true;
}
else if (testState == 5)//measurement complete
{
calculatePwmValues();
measurementInProcess = false; //reset
}
}
void calculatePwmValues()
{
word periodValue = timerValue[3] - timerValue[2];
word widthValue = timerValue[2] - timerValue[1];
word diffValue = widthValue - periodValue;
pwmPeriod = (periodValue * 0.0625);
pwmWidth = diffValue * .0625;
pwmDuty = (pwmWidth / pwmPeriod) * 100;
pwmFrequency = 1000 / pwmPeriod;
newValuesAvailable = true;
}