Advertisement
EmptySet5150

Final - Range finder

Aug 20th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <TM1637.h> // include display header file
  2. #include <Ultrasonic.h> // include ultrasonic.h header
  3. const int CLK = 2; // set clk pin
  4. const int DIO = 3; // set dio pin
  5. const int Echo = 4; // set echo pin
  6. const int Trig = 5; // set trig pin
  7. const int SO = 9; // set serial out control pin
  8. const int led = 13; // set board led pin
  9. TM1637 tm1637(CLK,DIO); // initalize display and pins
  10. Ultrasonic ultrasonic(Trig,Echo); // initalize rangefinder pins
  11. int seg0 = 0; // setting var to be used later
  12. int seg1 = 0;
  13. int seg2 = 0;
  14. void setup() {
  15.   Serial.begin(9600); // set serial display
  16.   tm1637.init(); // initalize display
  17.   tm1637.set(2); // set brightness of display
  18.   pinMode(led, OUTPUT); // set led pin to output
  19.   pinMode(SO, INPUT_PULLUP); // set SO as input pin
  20. }
  21. void loop() {
  22.   digitalWrite(led, HIGH); // turn on led
  23.   int SR = ultrasonic.Ranging(INC); // set SR to the sensor reading INC is for inch measurment
  24.   if (SR >= 100) { // setting seg0-2 if SR is over 100
  25.     seg0 = SR / 100; // SR devided by 100 gives first digit
  26.     seg1 = SR / 10; // SR devided by 10 givese second digit
  27.     seg2 = SR % 10; // SR % 10 gives last digit
  28.   }  
  29.   else if (SR <= 99) { // setting seg0-2 if SR is under 100 but over 10
  30.     seg0 = 0; // set first digit to 0
  31.     seg1 = SR / 10; // SR devided by 10 givese second digit
  32.     seg2 = SR % 10; // SR % 10 givese second digit
  33.   }
  34.   else {// setting seg0-2 if SR is under 10
  35.     seg0 = 0; // setting first digit to 0
  36.     seg1 = 0; // setting second digit to 0
  37.     seg2 = SR; // SR %10 givese second digit
  38.   }
  39.   tm1637.display(0,seg0); // display first range digit on segment 0
  40.   tm1637.display(1,seg1); // display second range digit on segment 1
  41.   tm1637.display(2,seg2); // display third range digit on segment 2
  42.   tm1637.display(3,16); // display - on segment 3
  43.   if (digitalRead(SO) == LOW) { // if SO is high we will start a serial display
  44.     Serial.print(ultrasonic.Ranging(INC)); // read sensor
  45.     Serial.println(" Sensor Reading ");
  46.     Serial.print(SR); // read stored value from sensor
  47.     Serial.println(" SR var set ");
  48.     Serial.print(seg0); //print all seg to serial
  49.     Serial.print(seg1);
  50.     Serial.print(seg2);
  51.     Serial.println(" Display ");
  52.     Serial.print(digitalRead(SO));
  53.     Serial.println(" Serial Output ");
  54.   }
  55.   else ;
  56.   digitalWrite(led, LOW); // turn off led
  57.   delay(5000); // 5 second display
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement