SHOW:
|
|
- or go back to the newest paste.
1 | #define led 13 | |
2 | ||
3 | enum {sample_state,high_state,low_state}; //Enumerate(give values to) the variables so they are 0,1,2 respectivly | |
4 | unsigned int state=sample_state; | |
5 | unsigned int val1,val2,val3,mVal,hiVal,loVal; | |
6 | unsigned int timer; | |
7 | ||
8 | /*----------------------------------------Main loop--------------------------------------*/ | |
9 | void setup() { | |
10 | Serial.begin(115200); | |
11 | pinMode(13,OUTPUT); | |
12 | } | |
13 | ||
14 | void loop() { | |
15 | switch(state){ //There are 3 different states that take place, take sample from inputs, set high duration, set low duration | |
16 | case sample_state: | |
17 | val1 = analogRead(pwmPin); | |
18 | val2 = 1023 -analogRead(pwmPin); | |
19 | val3 = analogRead(freqPin); | |
20 | ||
21 | mVal = map(Val3,0,1023,60,2000); //Mapped from 60-2000millisec | |
22 | hiVal = map(Val1,0,1023,0,mVal); | |
23 | loVal = map(Val2,0,1023,0,mVal) +1; | |
24 | ||
25 | bpm = (2000 / mVal) * tempoMin; // tempoMin = 30 | |
26 | Serial.print(bpm); | |
27 | ||
28 | state=high_state; //We have inputs, next we go on to high state mode | |
29 | digitalWrite(led,HIGH); //Set the pin high as the next state is just a timer | |
30 | timer=millis(); //Set timer to the time when this code block finishes | |
31 | break; | |
32 | case high_state: | |
33 | if(millis()>=timer+hiVal){ //Sit in this state until the timer threashold reaches the end of the high state | |
34 | state=state_low; //Move on to low state tmer | |
35 | digitalWrite(led,LOW); //Set pin low | |
36 | timer=millis(); //Reset timer | |
37 | } | |
38 | break; | |
39 | case low_state: | |
40 | if(millis()>=timer+loVal){ //Sit in this state until the timer threashold reaches end of the low state | |
41 | state=state_sample; //Cycle completed, go back to taking a new sample and repeat cycle | |
42 | } | |
43 | break; | |
44 | default: //This should never happen | |
45 | Serial.println("state variable not valid"); | |
46 | while(1); | |
47 | break; | |
48 | } | |
49 | //The rest of your code can go here and will be executed every loop cycle | |
50 | } |