Advertisement
Maderdash

Trubbled code

Apr 26th, 2022
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 17.63 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5. #include <Keypad.h>
  6.  
  7. Adafruit_SSD1306 display(128, 64, &Wire, 4);
  8.  
  9. //int pTrig = A0;
  10. //int pEcho = A1;
  11. //int pEcho2 = A2;
  12. //int pEcho3 = A3;
  13. //int pEcho4 = 2;
  14.  
  15. int digit;
  16.  
  17.  
  18.  
  19. char keys[4][4] = {
  20.   {'1', '2', '3', 'A'},
  21.   {'4', '5', '6', 'B'},
  22.   {'7', '8', '9', 'C'},
  23.   {'*', '0', '#', 'D'}
  24. };
  25.  
  26. byte pinyRadku[4] = {12, 11, 10, 9};
  27. byte pinySloupcu[4] = {8, 7, 6, 4};
  28.  
  29. Keypad klavesnice = Keypad( makeKeymap(keys), pinyRadku, pinySloupcu, 4, 4);
  30.  
  31. void(* resetFunc) (void) = 0;
  32.  
  33. char First = '.';   // Creating variables to store the history of 4 last pressed key on the keypad.
  34. char Second = '.';
  35. char Third = '.';
  36. char Fourth = '.';
  37.  
  38. char keyFirst = '9';  // The pincode is set here: You can use any of the character from the chart below.
  39. char keySecond = '9';
  40. char keyThird = '1';
  41. char keyFourth = '1';
  42.  
  43. int menu;
  44.  
  45. int us1;
  46. int us2;
  47. int us3;
  48. int us4;
  49.  
  50. bool state;
  51. int red_dis = 20;
  52. int orange_dis = 40;
  53.  
  54. long odezva, vzdalenost;
  55. float u1, u2, u3, u4;
  56.  
  57. void setup()
  58. {
  59.   // Serial.begin(9600);
  60.  
  61.  
  62.   pinMode(A0, OUTPUT);
  63.  
  64.   pinMode(A1, INPUT);
  65.   pinMode(A2, INPUT);
  66.   pinMode(A3, INPUT);
  67.   pinMode(2, INPUT);
  68.  
  69.   pinMode(5, OUTPUT);
  70.   pinMode(3, OUTPUT);
  71.   pinMode(13, OUTPUT);
  72.  
  73.  
  74.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
  75.     digitalWrite(5, HIGH);
  76.     for (;;); // Don't proceed, loop forever
  77.   }
  78.  
  79.   display.clearDisplay();
  80.   display.setTextSize(2);
  81.   display.setTextColor(WHITE);
  82.  
  83.  
  84.   if (readDistanceCM() > 3) us1 = HIGH;
  85.   else u1 = 999;
  86.   delay(200);
  87.   if (readDistanceCM2() > 3) us2 = HIGH;
  88.   else u2 = 999;
  89.   delay(200);
  90.   if (readDistanceCM3() > 1) us3 = HIGH;
  91.   else u3 = 999;
  92.   delay(200);
  93.   if (readDistanceCM4() > 1) us4 = HIGH;
  94.   else u4 = 999;
  95.   delay(200);
  96.  
  97.   display.setCursor(0, 0);
  98.   display.println("Ultrasonic");
  99.   display.print(us1);
  100.   display.print(us2);
  101.   display.print(us3);
  102.   display.print(us4);
  103.   display.display();
  104.   delay(2000);
  105.  
  106.   lock();
  107. }
  108.  
  109. void loop()
  110. {
  111.   char klavesa = klavesnice.getKey();
  112.  
  113.   if (menu == 0) {
  114.  
  115.     if (u1 != 999) u1 = readDistanceCM();
  116.     delay(50);
  117.     if (u2 != 999) u2 = readDistanceCM2();
  118.     delay(50);
  119.     if (u3 != 999) u3 = readDistanceCM3();
  120.     delay(50);
  121.     if (u4 != 999) u4 = readDistanceCM4();
  122.     delay(50);
  123.  
  124.     if (u1 < red_dis) {
  125.       analogWrite(5, 255); //RED
  126.       analogWrite(3, 0); //GREEN
  127.       digitalWrite(1, LOW);
  128.       tone(13, 1000);
  129.     }
  130.     else if (u2 < red_dis) {
  131.       analogWrite(5, 255); //RED
  132.       analogWrite(3, 0); //GREEN
  133.       digitalWrite(1, LOW);
  134.       tone(13, 1000);
  135.     }
  136.     else if (u3 < red_dis) {
  137.       analogWrite(5, 255); //RED
  138.       analogWrite(3, 0); //GREEN
  139.       digitalWrite(1, LOW);
  140.       tone(13, 1000);
  141.     }
  142.     else if (u4 < red_dis) {
  143.       analogWrite(5, 255); //RED
  144.       analogWrite(3, 0); //GREEN
  145.       digitalWrite(1, LOW);
  146.       tone(13, 1000);
  147.     }
  148.     else if (u1 < orange_dis) {
  149.       analogWrite(5, 255); //RED
  150.       analogWrite(3, 165); //GREEN
  151.       digitalWrite(1, LOW);
  152.       noTone(13);
  153.     }
  154.     else if (u2 < orange_dis) {
  155.       analogWrite(5, 255); //RED
  156.       analogWrite(3, 165); //GREEN
  157.       digitalWrite(1, LOW);
  158.       noTone(13);
  159.     }
  160.     else if (u3 < orange_dis) {
  161.       analogWrite(5, 255); //RED
  162.       analogWrite(3, 165); //GREEN
  163.       digitalWrite(1, LOW);
  164.       noTone(13);
  165.     }
  166.     else if (u4 < orange_dis) {
  167.       analogWrite(5, 255); //RED
  168.       analogWrite(3, 165); //GREEN
  169.       digitalWrite(1, LOW);
  170.       noTone(13);
  171.     }
  172.     else {
  173.       analogWrite(5, 0); //RED
  174.       analogWrite(3, 255); //GREEN
  175.       digitalWrite(3, HIGH);
  176.       noTone(13);
  177.     }
  178.  
  179.   }
  180.   if (klavesa) {
  181.     if (menu == 0) {
  182.       //  Serial.print("Pressed key: ");
  183.       //  Serial.println(klavesa);
  184.       if (digit == 0) First = klavesa;
  185.       if (digit == 1) Second = klavesa;
  186.       if (digit == 2) Third = klavesa;
  187.       if (digit == 3) Fourth = klavesa;
  188.       digit++;
  189.  
  190.       //float distance = readDistanceCM();
  191.  
  192.       //  Serial.print("Measured distance: ");
  193.       // Serial.println(readDistanceCM());
  194.       // delay(100);
  195.  
  196.       display.clearDisplay();
  197.       display.setCursor(0, 0);
  198.       display.print("PIN:");
  199.       display.print(First);
  200.       display.print(Second);
  201.       display.print(Third);
  202.       display.print(Fourth);
  203.       display.display();
  204.  
  205.       if (digit == 4) {
  206.         if (keyFirst == First && keySecond == Second && keyThird == Third && keyFourth == Fourth) {
  207.           display.clearDisplay();
  208.           display.setCursor(0, 0);
  209.           display.print("A -RED LED");
  210.           display.setCursor(0, 15);
  211.           display.print("B -ORANGE");
  212.           display.setCursor(0, 30);
  213.           display.print("C -C PIN");
  214.           display.setCursor(0, 45);
  215.           display.print("D -LOCK");
  216.           display.display();
  217.  
  218.  
  219.   if (readDistanceCM() > 3) u1 = 90;
  220.   else u1 = 999;
  221.   delay(200);
  222.   if (readDistanceCM2() > 3) u2 = 90;
  223.   else u2 = 999;
  224.   delay(200);
  225.   if (readDistanceCM3() > 1) u3 = 90;
  226.   else u3 = 999;
  227.   delay(200);
  228.   if (readDistanceCM4() > 1) u4 = 90;
  229.   else u4 = 999;
  230.   delay(200);
  231.  
  232.  
  233.           delay(500);
  234.           menu = 1;
  235.           digit = 0;
  236.         } else {
  237.           digit = 0;
  238.  
  239.           display.clearDisplay();
  240.           First = '.';
  241.           Second = '.';
  242.           Third = '.';
  243.           Fourth = '.';
  244.           display.setCursor(0, 0);
  245.           display.print("PIN:");
  246.           display.print(First);
  247.           display.print(Second);
  248.           display.print(Third);
  249.           display.print(Fourth);
  250.           display.setCursor(0, 14);
  251.           display.println("Incorrect!");
  252.           display.display();
  253.           delay(500);
  254.         }
  255.       }
  256.       display.display();
  257.  
  258.     }
  259.   }
  260.  
  261.  
  262.   if (menu == 1) {
  263.  
  264.     if (klavesa == 'A') {
  265.       display.clearDisplay();
  266.       display.setCursor(0, 0);
  267.       display.setTextSize(2);
  268.       display.print("Min dis.10\nMax dis.99\nSet:");
  269.       display.print(red_dis);
  270.       display.setTextSize(2);
  271.       display.display();
  272.  
  273.       while (state == false) {
  274.         char klavesa = klavesnice.getKey();
  275.         if (klavesa) {
  276.           if (klavesa == '1' || klavesa == '2' || klavesa == '3' || klavesa == '4' || klavesa == '5' || klavesa == '6' || klavesa == '7' || klavesa == '8' || klavesa == '9') {
  277.             First = klavesa;
  278.             state = true;
  279.           }
  280.         }
  281.       }
  282.  
  283.       display.clearDisplay();
  284.       display.setCursor(0, 0);
  285.       display.print(First);
  286.       display.print(" cm");
  287.       display.display();
  288.       state = false;
  289.  
  290.       if (First == '1') red_dis = 10;
  291.       if (First == '2') red_dis = 20;
  292.       if (First == '3') red_dis = 30;
  293.       if (First == '4') red_dis = 40;
  294.       if (First == '5') red_dis = 50;
  295.       if (First == '6') red_dis = 60;
  296.       if (First == '7') red_dis = 70;
  297.       if (First == '8') red_dis = 80;
  298.       if (First == '9') red_dis = 90;
  299.  
  300.       //Serial.println(red_dis);
  301.  
  302.       while (state == false) {
  303.         char klavesa = klavesnice.getKey();
  304.         if (klavesa) {
  305.           if (klavesa == '1' || klavesa == '2' || klavesa == '3' || klavesa == '4' || klavesa == '5' || klavesa == '6' || klavesa == '7' || klavesa == '8' || klavesa == '9' || klavesa == '0') {
  306.             Second = klavesa;
  307.             state = true;
  308.           }
  309.         }
  310.       }
  311.  
  312.       display.clearDisplay();
  313.       display.setCursor(0, 0);
  314.       display.print(First);
  315.       display.print(Second);
  316.       display.print(" cm");
  317.       display.setCursor(0, 15);
  318.       display.print("Saved!");
  319.       display.display();
  320.       state = false;
  321.  
  322.       if (Second == '1') red_dis = red_dis + 1;
  323.       if (Second == '2') red_dis = red_dis + 2;
  324.       if (Second == '3') red_dis = red_dis + 3;
  325.       if (Second == '4') red_dis = red_dis + 4;
  326.       if (Second == '5') red_dis = red_dis + 5;
  327.       if (Second == '6') red_dis = red_dis + 6;
  328.       if (Second == '7') red_dis = red_dis + 7;
  329.       if (Second == '8') red_dis = red_dis + 8;
  330.       if (Second == '9') red_dis = red_dis + 9;
  331.       delay(2000);
  332.  
  333.       //Serial.println(red_dis);
  334.       lock();
  335.     }
  336.  
  337.     if (klavesa == 'B') {
  338.       display.clearDisplay();
  339.       display.setTextSize(2);
  340.       display.setCursor(0, 0);
  341.       display.print("Min dis.10\nMax dis.99\nSet:");
  342.       display.print(orange_dis);
  343.       display.setTextSize(2);
  344.       display.display();
  345.  
  346.       while (state == false) {
  347.         char klavesa = klavesnice.getKey();
  348.         if (klavesa) {
  349.           if (klavesa == '1' || klavesa == '2' || klavesa == '3' || klavesa == '4' || klavesa == '5' || klavesa == '6' || klavesa == '7' || klavesa == '8' || klavesa == '9') {
  350.             First = klavesa;
  351.             state = true;
  352.           }
  353.         }
  354.       }
  355.  
  356.       display.clearDisplay();
  357.       display.setCursor(0, 0);
  358.       display.print(First);
  359.       display.print(" cm");
  360.       display.display();
  361.       state = false;
  362.  
  363.       if (First == '1') orange_dis = 10;
  364.       if (First == '2') orange_dis = 20;
  365.       if (First == '3') orange_dis = 30;
  366.       if (First == '4') orange_dis = 40;
  367.       if (First == '5') orange_dis = 50;
  368.       if (First == '6') orange_dis = 60;
  369.       if (First == '7') orange_dis = 70;
  370.       if (First == '8') orange_dis = 80;
  371.       if (First == '9') orange_dis = 90;
  372.  
  373.       //Serial.println(orange_dis);
  374.  
  375.       while (state == false) {
  376.         char klavesa = klavesnice.getKey();
  377.         if (klavesa) {
  378.           if (klavesa == '1' || klavesa == '2' || klavesa == '3' || klavesa == '4' || klavesa == '5' || klavesa == '6' || klavesa == '7' || klavesa == '8' || klavesa == '9' || klavesa == '0') {
  379.             Second = klavesa;
  380.             state = true;
  381.           }
  382.         }
  383.       }
  384.  
  385.       display.clearDisplay();
  386.       display.setCursor(0, 0);
  387.       display.print(First);
  388.       display.print(Second);
  389.       display.print(" cm");
  390.       display.setCursor(0, 15);
  391.       display.print("Saved!");
  392.       display.display();
  393.       state = false;
  394.  
  395.       if (Second == '1') orange_dis = orange_dis + 1;
  396.       if (Second == '2') orange_dis = orange_dis + 2;
  397.       if (Second == '3') orange_dis = orange_dis + 3;
  398.       if (Second == '4') orange_dis = orange_dis + 4;
  399.       if (Second == '5') orange_dis = orange_dis + 5;
  400.       if (Second == '6') orange_dis = orange_dis + 6;
  401.       if (Second == '7') orange_dis = orange_dis + 7;
  402.       if (Second == '8') orange_dis = orange_dis + 8;
  403.       if (Second == '9') orange_dis = orange_dis + 9;
  404.       delay(2000);
  405.  
  406.       //Serial.println(red_dis);
  407.       lock();
  408.     }
  409.     if (klavesa == 'D') {
  410.       lock();
  411.     }
  412.    
  413.     if (klavesa == '0')
  414.     {
  415.       for (int i = 0; i <= 25500; i++)
  416.       {
  417.         digitalWrite(3, LOW);
  418.         digitalWrite(5, LOW);
  419.         digitalWrite(13, LOW);
  420.        display.clearDisplay();
  421.        display.display();
  422.  
  423.        if (klavesa == '1')
  424.        {
  425.          
  426.          digitalWrite(3, HIGH);
  427.          digitalWrite(5, HIGH);
  428.          digitalWrite(13, HIGH);
  429.          i=25501;
  430.          lock();
  431.        }
  432.       }
  433.  
  434.  
  435.     }
  436.  
  437.     if (klavesa == '#')
  438.     {
  439.       resetFunc();
  440.     }
  441.  
  442.     if (klavesa == '*')
  443.     {
  444.     for (int i = 0; i <= 25500; i++)
  445.     {
  446. #define echoPin1 A1
  447. #define echoPin2 A2
  448. #define echoPin3 A3
  449. #define echoPin4 2
  450. #define trigPin A0
  451. char klavesa = klavesnice.getKey();
  452.  
  453. long duration;
  454. int distance;
  455.  
  456. long duration2;
  457. int distance2;
  458.  
  459. long duration3;
  460. int distance3;
  461.  
  462. long duration4;
  463. int distance4;
  464.  
  465. pinMode(trigPin, OUTPUT);
  466. pinMode(echoPin1, INPUT);
  467. pinMode(echoPin2, INPUT);
  468. pinMode(echoPin3, INPUT);
  469. pinMode(echoPin4, INPUT);
  470.  
  471.   display.clearDisplay();
  472.   display.setCursor(0, 0);
  473.   digitalWrite(trigPin, LOW);
  474.   delayMicroseconds(2);
  475.   digitalWrite(trigPin, HIGH);
  476.   delayMicroseconds(10);
  477.   digitalWrite(trigPin, LOW);
  478.   duration = pulseIn(echoPin1, HIGH);
  479.   distance = duration * 0.034 / 2;
  480.  
  481.  
  482.   digitalWrite(trigPin, LOW);
  483.   delayMicroseconds(2);
  484.   digitalWrite(trigPin, HIGH);
  485.   delayMicroseconds(10);
  486.   digitalWrite(trigPin, LOW);
  487.   duration2 = pulseIn(echoPin2, HIGH);
  488.   distance2 = duration2 * 0.034 / 2;
  489.  
  490.  
  491.   digitalWrite(trigPin, LOW);
  492.   delayMicroseconds(2);
  493.   digitalWrite(trigPin, HIGH);
  494.   delayMicroseconds(10);
  495.   digitalWrite(trigPin, LOW);
  496.   duration3 = pulseIn(echoPin3, HIGH);
  497.   distance3 = duration3 * 0.034 / 2;
  498.  
  499.   digitalWrite(trigPin, LOW);
  500.   delayMicroseconds(2);
  501.   digitalWrite(trigPin, HIGH);
  502.   delayMicroseconds(10);
  503.   digitalWrite(trigPin, LOW);
  504.   duration4 = pulseIn(echoPin4, HIGH);
  505.   distance4 = duration4 * 0.034 / 2;
  506.  
  507.  
  508.   display.print("Dis.1:");
  509.   display.print(distance);
  510.   display.print("\n");
  511.  
  512.   display.print("Dis.2:");
  513.   display.print(distance2);
  514.   display.print("\n");
  515.  
  516.   display.print("Dis.3:");
  517.   display.print(distance3);
  518.   display.print("\n");
  519.  
  520.   display.print("Dis.4:");
  521.   display.print(distance4);
  522.   display.print("\n");
  523.  
  524.   display.display();
  525.  
  526.  
  527.   if (klavesa == '*')
  528.   {
  529.     i = 25501;
  530.     lock();
  531.   }
  532.     }
  533.     }
  534.  
  535.     if (klavesa == 'C') {
  536.       state = false;
  537.  
  538.       First = '.';
  539.       Second = '.';
  540.       Third = '.';
  541.       Fourth = '.';
  542.       writenew();
  543.       delay(300);
  544.  
  545.       while (state == false) {
  546.         char klavesa = klavesnice.getKey();
  547.         if (klavesa) {
  548.           First = klavesa;
  549.           state = true;
  550.         }
  551.       }
  552.  
  553.       state = false;
  554.       writenew();
  555.  
  556.       while (state == false) {
  557.         char klavesa = klavesnice.getKey();
  558.         if (klavesa) {
  559.           Second = klavesa;
  560.           state = true;
  561.         }
  562.       }
  563.  
  564.       state = false;
  565.       writenew();
  566.  
  567.       while (state == false) {
  568.         char klavesa = klavesnice.getKey();
  569.         if (klavesa) {
  570.           Third = klavesa;
  571.           state = true;
  572.         }
  573.       }
  574.  
  575.       state = false;
  576.       writenew();
  577.  
  578.       while (state == false) {
  579.         char klavesa = klavesnice.getKey();
  580.         if (klavesa) {
  581.           Fourth = klavesa;
  582.           state = true;
  583.         }
  584.       }
  585.  
  586.       state = false;
  587.  
  588.       writenew();
  589.  
  590.       keyFirst = First;
  591.       keySecond = Second;
  592.       keyThird = Third;
  593.       keyFourth = Fourth;
  594.  
  595.       display.setCursor(0, 15);
  596.       display.print("Pin set");
  597.       display.display();
  598.       delay(1000);
  599.       lock();
  600.  
  601.     }
  602.  
  603.   }
  604.  
  605. }
  606.  
  607. void writenew() {
  608.   display.clearDisplay();
  609.   display.setCursor(0, 0);
  610.   display.print("NEW:");
  611.   display.print(First);
  612.   display.print(Second);
  613.   display.print(Third);
  614.   display.print(Fourth);
  615.   display.display();
  616. }
  617.  
  618. void loop_distance()
  619. {
  620. #define echoPin1 A1
  621. #define echoPin2 A2
  622. #define echoPin3 A3
  623. #define echoPin4 2
  624. #define trigPin A0
  625. char klavesa = klavesnice.getKey();
  626.  
  627. long duration;
  628. int distance;
  629.  
  630. long duration2;
  631. int distance2;
  632.  
  633. long duration3;
  634. int distance3;
  635.  
  636. long duration4;
  637. int distance4;
  638.  
  639. pinMode(trigPin, OUTPUT);
  640. pinMode(echoPin1, INPUT);
  641. pinMode(echoPin2, INPUT);
  642. pinMode(echoPin3, INPUT);
  643. pinMode(echoPin4, INPUT);
  644.  
  645.   display.clearDisplay();
  646.   display.setCursor(0, 0);
  647.   digitalWrite(trigPin, LOW);
  648.   delayMicroseconds(2);
  649.   digitalWrite(trigPin, HIGH);
  650.   delayMicroseconds(10);
  651.   digitalWrite(trigPin, LOW);
  652.   duration = pulseIn(echoPin1, HIGH);
  653.   distance = duration * 0.034 / 2;
  654.  
  655.  
  656.   digitalWrite(trigPin, LOW);
  657.   delayMicroseconds(2);
  658.   digitalWrite(trigPin, HIGH);
  659.   delayMicroseconds(10);
  660.   digitalWrite(trigPin, LOW);
  661.   duration2 = pulseIn(echoPin2, HIGH);
  662.   distance2 = duration2 * 0.034 / 2;
  663.  
  664.  
  665.   digitalWrite(trigPin, LOW);
  666.   delayMicroseconds(2);
  667.   digitalWrite(trigPin, HIGH);
  668.   delayMicroseconds(10);
  669.   digitalWrite(trigPin, LOW);
  670.   duration3 = pulseIn(echoPin3, HIGH);
  671.   distance3 = duration3 * 0.034 / 2;
  672.  
  673.   digitalWrite(trigPin, LOW);
  674.   delayMicroseconds(2);
  675.   digitalWrite(trigPin, HIGH);
  676.   delayMicroseconds(10);
  677.   digitalWrite(trigPin, LOW);
  678.   duration4 = pulseIn(echoPin4, HIGH);
  679.   distance4 = duration4 * 0.034 / 2;
  680.  
  681.  
  682.   display.print("Dis.1:");
  683.   display.print(distance);
  684.   display.print("\n");
  685.  
  686.   display.print("Dis.2:");
  687.   display.print(distance2);
  688.   display.print("\n");
  689.  
  690.   display.print("Dis.3:");
  691.   display.print(distance3);
  692.   display.print("\n");
  693.  
  694.   display.print("Dis.4:");
  695.   display.print(distance4);
  696.   display.print("\n");
  697.  
  698.   display.display();
  699.   delay(100);
  700.   return;
  701. }
  702.  
  703. void standby()
  704. {
  705.   char klavesa = klavesnice.getKey();
  706.   digitalWrite(3, LOW);
  707.   digitalWrite(5, LOW);
  708.   digitalWrite(13, LOW);
  709.   display.clearDisplay();
  710.   display.display();
  711.  
  712.   if (klavesa == '1')
  713.   {
  714.     lock();
  715.   }
  716. }
  717.  
  718.  
  719.  
  720.  
  721. void lock() {
  722.   menu = 0;
  723.   display.clearDisplay();
  724.   First = '.';
  725.   Second = '.';
  726.   Third = '.';
  727.   Fourth = '.';
  728.   display.setCursor(0, 0);
  729.   display.print("PIN:");
  730.   display.print(First);
  731.   display.print(Second);
  732.   display.print(Third);
  733.   display.print(Fourth);
  734.   display.display();
  735.   delay(300);
  736. }
  737.  
  738. int readDistanceCM() {
  739.   digitalWrite(A0, LOW);
  740.   delayMicroseconds(2);
  741.   digitalWrite(A0, HIGH);
  742.   delayMicroseconds(10);
  743.   digitalWrite(A0, LOW);
  744.   int duration = pulseIn(A1, HIGH);
  745.   return duration / 58.31;
  746. }
  747. int readDistanceCM2() {
  748.   digitalWrite(A0, LOW);
  749.   delayMicroseconds(2);
  750.   digitalWrite(A0, HIGH);
  751.   delayMicroseconds(10);
  752.   digitalWrite(A0, LOW);
  753.   int duration = pulseIn(A2, HIGH);
  754.   return duration / 58.31;
  755. }
  756. int readDistanceCM3() {
  757.   digitalWrite(A0, LOW);
  758.   delayMicroseconds(2);
  759.   digitalWrite(A0, HIGH);
  760.   delayMicroseconds(10);
  761.   digitalWrite(A0, LOW);
  762.   int duration = pulseIn(A3, HIGH);
  763.   return duration * 0.034 / 2;
  764. }
  765. int readDistanceCM4() {
  766.   digitalWrite(A0, LOW);
  767.   delayMicroseconds(2);
  768.   digitalWrite(A0, HIGH);
  769.   delayMicroseconds(10);
  770.   digitalWrite(A0, LOW);
  771.   int duration = pulseIn(2, HIGH);
  772.   return duration * 0.034 / 2;
  773. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement