Variable within while loop

Hello,
I am fairly new to c++ and am trying to code a FRDM mbed board. My question is how do I define the variable period, and frequency for that matter, to be used outside of my while loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "mbed.h"

DigitalOut gpo(D0);
DigitalOut led(LED_BLUE);

Serial pc(USBTX, USBRX);

InterruptIn InPin (D7);
bool OutPin1 (D3);
bool OutPin2 (D4);


volatile float period = 0;
Timer periodTimer;

void interruptHandler(){
    period=periodTimer.read();
    periodTimer.reset();
    }


int main()
{
    periodTimer.start();
    pc.baud(115200);
    InPin.rise(&interruptHandler);
    while(true){
        printf("Period = %f Freq = %f\n",period,1/period);
}
    
volatile float freq = 1/period; 

if (freq < 550)
       OutPin1 = true;

if (freq > 6950)
        OutPin2 = true;
    

}
1
2
float period = 0.0;
float freq = 0.0;


I don't know the volatile keyword, but you have both variables as global variables, so they should work in the loop. You have a lot of code outside of your main, is that where it's supposed to be?
Last edited on
Topic archived. No new replies allowed.