Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BUTTON_PIN 2
- #include <cmath>
- const int n = 12;
- int arr[n]={10,12,15,18,22,27,33,39,47,56,68,82};
- bool buttonWasUp = true; // была ли кнопка отпущена?
- int near(int N)
- {
- int item = 0;
- int result = abs(N - arr[0]);
- for(int i = 0; i < n; i++)
- {
- if(abs(N - arr[i]) < result)
- {
- result = abs(N - arr[i]);
- item = arr[i];
- }
- }
- return item;
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(BUTTON_PIN,INPUT_PULLUP);
- }
- void loop()
- {
- bool buttonIsUp = digitalRead(BUTTON_PIN);
- if (!buttonWasUp && buttonIsUp) {
- delay(10);
- buttonIsUp = digitalRead(BUTTON_PIN);
- if (!buttonIsUp) {
- while(!digitalRead(BUTTON_PIN));
- float v = analogRead(A0);
- float r = (10.0*v/(1023.0-v));
- Serial.println(near(r));
- }else{
- Serial.println(1023);
- }
- }
- buttonWasUp = buttonIsUp;
- }
Advertisement
Add Comment
Please, Sign In to add comment