|
| 1 | +/* |
| 2 | + STLTest |
| 3 | +
|
| 4 | + Test abs/min/max/round called from C or C++ |
| 5 | +
|
| 6 | + Creation 10 Oct 2017 |
| 7 | + by Frederic Pillon <[email protected]> |
| 8 | +
|
| 9 | + This example code is in the public domain. |
| 10 | +*/ |
| 11 | + |
| 12 | +#include "test_stl.h" |
| 13 | +static uint32_t count_pressed = 0; |
| 14 | +static uint32_t random_val = 0; |
| 15 | + |
| 16 | +int pushButton = USER_BTN; |
| 17 | + |
| 18 | +// the setup routine runs once when you press reset: |
| 19 | +void setup() { |
| 20 | + // initialize serial communication at 115200 bits per second: |
| 21 | + Serial.begin(115200); |
| 22 | + // make the pushbutton's pin an input: |
| 23 | + pinMode(pushButton, INPUT); |
| 24 | + // if analog input pin 0 is unconnected, random analog |
| 25 | + // noise will cause the call to randomSeed() to generate |
| 26 | + // different seed numbers each time the sketch runs. |
| 27 | + // randomSeed() will then shuffle the random function. |
| 28 | + randomSeed(analogRead(0)); |
| 29 | +} |
| 30 | + |
| 31 | +// the loop routine runs over and over again forever: |
| 32 | +void loop() { |
| 33 | + // read the input pin: |
| 34 | + int buttonState = digitalRead(pushButton); |
| 35 | + random_val = random(300); |
| 36 | + if (buttonState == LOW) |
| 37 | + { |
| 38 | + count_pressed++; |
| 39 | + } |
| 40 | + |
| 41 | + Serial.print("count pressed: "); |
| 42 | + Serial.print(count_pressed); |
| 43 | + Serial.print("\trandom: "); |
| 44 | + Serial.println(random_val); |
| 45 | + |
| 46 | + Serial.print("C++\tmin: "); |
| 47 | + Serial.print(min(random_val,count_pressed)); |
| 48 | + Serial.print("\tmax: "); |
| 49 | + Serial.print(max(random_val,count_pressed)); |
| 50 | + Serial.print("\tabs: "); |
| 51 | + Serial.print(abs(count_pressed)); |
| 52 | + Serial.print("\tround (count_pressed/random_val): "); |
| 53 | + Serial.println(round(count_pressed/random_val)); |
| 54 | + |
| 55 | + Serial.print("C\tmin: "); |
| 56 | + Serial.print(test_c(random_val,count_pressed, MIN)); |
| 57 | + Serial.print("\tmax: "); |
| 58 | + Serial.print(test_c(random_val,count_pressed, MAX)); |
| 59 | + Serial.print("\tabs: "); |
| 60 | + Serial.print(test_c(count_pressed, 0, ABS)); |
| 61 | + Serial.print("\tround (count_pressed/random_val) is "); |
| 62 | + Serial.println(test_c(count_pressed, random_val, ROUND)); |
| 63 | + |
| 64 | + Serial.println(); |
| 65 | + delay(100); // delay in between reads for stability |
| 66 | +} |
0 commit comments