Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TM1638.h>
- #define trigPin 12
- #define echoPin 11
- void setup() {
- Serial.begin (9600);
- pinMode(trigPin, OUTPUT); //Pin, do którego podłączymy trig jako wyjście
- pinMode(echoPin, INPUT); //a echo, jako wejście
- }
- void loop() {
- Serial.print(zmierzOdleglosc());
- Serial.println(" cm");
- delay(500);
- }
- int zmierzOdleglosc() {
- long czas, dystans;
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- czas = pulseIn(echoPin, HIGH);
- dystans = czas / 0.58;
- return dystans;
- }
- const int strobe = 7;
- const int clock = 9;
- const int dat
- /*
- * REUK.co.uk - February 2016
- * Useful function to test if one of the eight user input buttons
- * on a TM1638 module is currently being pressed.
- */
- // The byte buttons is the value returned by the TM1638 to indicate
- // which buttons are currently being pressed.
- byte buttons;
- // define a module on data pin 8, clock pin 9 and strobe pin 7
- TM1638 module(8, 9, 7);
- void setup(){}
- void loop(){
- // The buttons S1 to S8 have the following values:
- // S1 = 1, S2 = 2, S3 = 4, S4 = 8, S5 = 16, S6 = 32, S7 = 64, S8 = 128
- // If multiple buttons are pressed simultaneously, add their values together.
- // DEMONSTRATION - Loop through the 8 buttons, testing each to see if it is
- // currently being pressed. If a button is being pressed, show its number (1-8)
- // on the LED display...Leave it displayed until a different button is pressed.
- for(int buttonToTest = 1; buttonToTest < 9; buttonToTest++){
- // Let the TM1638 process the button inputs
- buttons = module.getButtons();
- if(isButtonBeingPressed(buttonToTest)){
- // This button (buttonToTest) has been found to be pressed, so display it's number S1-S8
- module.setDisplayToDecNumber(buttonToTest, 0, false);
- }
- }
- }
- // This function will return true if a particular button n is currently being pressed.
- boolean isButtonBeingPressed(int n){
- // Button 1 status shown by bit0 of the byte buttons returned by module.getButtons()
- // Button 2 status shown by bit1 or the byte buttons ...
- // Button 3 status shown by bit2...etc
- // n - the number of the button to be tested) should be an integer from 1 to 8
- if(n < 1 or n > 8) return false;
- // Read in the value of getButtons from the TM1638 module.
- buttons = module.getButtons();
- // Which bit must we test for this button?
- int bitToLookAt = n - 1;
- // Read the value of the bit - either a 1 for button pressed, or 0 for not pressed.
- byte theValueOfTheBit = bitRead(buttons, bitToLookAt);
- // If the button is pressed, return true, otherwise return false.
- if(theValueOfTheBit == 1)
- return true;
- else
- return false;
- }
Add Comment
Please, Sign In to add comment