kidi3
Full Member level 1
I at the moment trying to extract a value that gets incremented inside of a ISR, but can't seem to get the final value, or the correct value.
here is the code:
How do i extract the newest value from the ISR?
code example:
And outside component changes the state of the bool activate, and depending of the state of activate variable would the the function `process` provide a certain output. Problem is that when `Activate = true` , process isn't outputting the wanted value, I know the value it has to be, and have confirmed it using serial monitor which should be 1000, but the output i am receiving is 991. I tested the value with serial.print inside the ISR and have seen that the last value it becomes is 1000. So I am not able to get the last value.. what am I doing wrong?
here is the code:
How do i extract the newest value from the ISR?
code example:
Code:
volatile speed_prof profile;
bool activate = false;
void process(int a)
{
// do something
}
void loop()
{
while(activate == false)
{
process(profile.moved_steps);
}
OCR1A = 10;
// Set Timer/Counter to divide clock by 8
TCCR1B |= ((0 << CS12) | (1 << CS11) | (0 << CS10));
while(activate == true)
{
cli();
int temp = profile.moved_steps;
sei();
process(profile.moved_steps);
}
}
ISR(TIMER1_COMPA_vect)
{
// Holds next delay period.
unsigned int new_first_step_delay;
// Remember the last step delay used when accelrating.
static int last_accel_delay;
static unsigned int rest = 0;
OCR1A = profile.first_step_delay;
switch (profile.run_state)
{
case STOP:
activation = false;
rest = 0;
TCCR1B &= ~((1 << CS12) | (1 << CS11) | (1 << CS10)); // Stop the timer, No clock source
break;
case ACCEL:
profile.moved_steps++;
profile.accel_count++;
new_first_step_delay = profile.first_step_delay - (((2 * (long)profile.first_step_delay) + rest) / (4 * profile.accel_count + 1));
rest = ((2 * (long)profile.first_step_delay) + rest) % (4 * profile.accel_count + 1);
if(profile.moved_steps >= profile.decel_start)
{
profile.accel_count = profile.decel_length;
profile.run_state = DECEL;
}
else if (new_first_step_delay <= profile.min_time_delay)
{
last_accel_delay = new_first_step_delay;
new_first_step_delay = profile.min_time_delay;
rest = 0;
profile.run_state = RUN;
}
break;
case RUN:
profile.moved_steps++;
new_first_step_delay = profile.min_time_delay;
if (profile.moved_steps >= profile.decel_start)
{
profile.accel_count = profile.decel_length;
new_first_step_delay = last_accel_delay;
profile.run_state = DECEL;
}
break;
case DECEL:
profile.moved_steps++;
profile.accel_count++;
new_first_step_delay = profile.first_step_delay - (((2 * (long)profile.first_step_delay) + rest) / (4 * profile.accel_count + 1));
rest = ((2 * (long)profile.first_step_delay) + rest) % (4 * profile.accel_count + 1);
if (profile.accel_count >= 0)
{
profile.run_state = STOP;
}
break;
}
profile.first_step_delay = new_first_step_delay;
}
And outside component changes the state of the bool activate, and depending of the state of activate variable would the the function `process` provide a certain output. Problem is that when `Activate = true` , process isn't outputting the wanted value, I know the value it has to be, and have confirmed it using serial monitor which should be 1000, but the output i am receiving is 991. I tested the value with serial.print inside the ISR and have seen that the last value it becomes is 1000. So I am not able to get the last value.. what am I doing wrong?