gringotopies

ARDUINO

Apr 9th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <TM1638.h>
  2.  
  3. #define trigPin 12
  4. #define echoPin 11
  5.  
  6. void setup() {
  7.   Serial.begin (9600);
  8.   pinMode(trigPin, OUTPUT); //Pin, do którego podłączymy trig jako wyjście
  9.   pinMode(echoPin, INPUT); //a echo, jako wejście
  10. }
  11.  
  12. void loop() {  
  13.   Serial.print(zmierzOdleglosc());
  14.   Serial.println(" cm");
  15.  
  16.   delay(500);
  17. }
  18.  
  19. int zmierzOdleglosc() {
  20.   long czas, dystans;
  21.  
  22.   digitalWrite(trigPin, LOW);
  23.   delayMicroseconds(2);
  24.   digitalWrite(trigPin, HIGH);
  25.   delayMicroseconds(10);
  26.   digitalWrite(trigPin, LOW);
  27.  
  28.   czas = pulseIn(echoPin, HIGH);
  29.   dystans = czas / 0.58;
  30.  
  31.   return dystans;
  32. }
  33.  
  34.  
  35. const int strobe = 7;
  36. const int clock = 9;
  37. const int dat
  38.  
  39. /*
  40.  * REUK.co.uk - February 2016
  41.  * Useful function to test if one of the eight user input buttons
  42.  * on a TM1638 module is currently being pressed.
  43.  */
  44.  
  45. // The byte buttons is the value returned by the TM1638 to indicate
  46. // which buttons are currently being pressed.
  47. byte buttons;
  48.  
  49.  
  50.  
  51. // define a module on data pin 8, clock pin 9 and strobe pin 7
  52. TM1638 module(8, 9, 7);
  53.  
  54. void setup(){}
  55.  
  56. void loop(){
  57.  // The buttons S1 to S8 have the following values:
  58.  // S1 = 1, S2 = 2, S3 = 4, S4 = 8, S5 = 16, S6 = 32, S7 = 64, S8 = 128
  59.  // If multiple buttons are pressed simultaneously, add their values together.
  60.  
  61.  // DEMONSTRATION - Loop through the 8 buttons, testing each to see if it is
  62.  // currently being pressed. If a button is being pressed, show its number (1-8)
  63.  // on the LED display...Leave it displayed until a different button is pressed.
  64.  for(int buttonToTest = 1; buttonToTest < 9; buttonToTest++){
  65.    // Let the TM1638 process the button inputs
  66.    buttons = module.getButtons();
  67.    if(isButtonBeingPressed(buttonToTest)){
  68.      // This button (buttonToTest) has been found to be pressed, so display it's number S1-S8
  69.      module.setDisplayToDecNumber(buttonToTest, 0, false);
  70.    }
  71.  }
  72. }
  73.  
  74. // This function will return true if a particular button n is currently being pressed.
  75. boolean isButtonBeingPressed(int n){
  76.  // Button 1 status shown by bit0 of the byte buttons returned by module.getButtons()
  77.  // Button 2 status shown by bit1 or the byte buttons ...
  78.  // Button 3 status shown by bit2...etc
  79.  
  80.  // n - the number of the button to be tested) should be an integer from 1 to 8
  81.  if(n < 1 or n > 8) return false;
  82.  
  83.  // Read in the value of getButtons from the TM1638 module.
  84.  buttons = module.getButtons();
  85.  
  86.  // Which bit must we test for this button?
  87.  int bitToLookAt = n - 1;
  88.  
  89.  // Read the value of the bit - either a 1 for button pressed, or 0 for not pressed.
  90.  byte theValueOfTheBit = bitRead(buttons, bitToLookAt);
  91.  
  92.  // If the button is pressed, return true, otherwise return false.
  93.  if(theValueOfTheBit == 1)
  94.    return true;
  95.  else
  96.    return false;
  97. }
Add Comment
Please, Sign In to add comment