Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. //UltrasonicDemo.ino
  2. #include "Ultrasonic.h"
  3.                
  4. void setup(){
  5.     Serial.begin(SerialBaudRate);   //115200
  6.     while (!Serial) {delay(10);} //wait for serial to wake up
  7.     UltrasonicSetup();
  8. }
  9.  
  10. void loop(){
  11.   UltrasonicLoop();
  12. }
  13.  
  14. *********************************************
  15. //Excerpts from Ultrasonic.cpp
  16.  
  17. void UltrasonicSetup() {
  18.    Serial.begin(SerialBaudRate);    //115200
  19.    while (!Serial) {delay(10);}      //wait for serial to wake up
  20.    D_show_cursor(false);            //disable cursor on display
  21.    D_cls();
  22.    D_position(0, 0);
  23.    Serial.print(F("Ultrasonic library loaded"));
  24.    
  25.    pinMode(TRIGGER_PIN, OUTPUT);   //TRIG(speaker pin)
  26.    pinMode(ECHO_PIN, INPUT);       //ECHO(microphone pin)
  27.    attachInterrupt (digitalPinToInterrupt (ECHO_PIN), USInterruptTimer, CHANGE); //interrupt routine setup
  28.    //attachPCINT(digitalPinToPCINT(ECHO_PIN), InterruptTimer, CHANGE); //when using PinChangeInterrupt.h  
  29.    delay(1000);
  30.    D_cls();
  31. }
  32.  
  33. *********************************************
  34.  
  35. void UltrasonicLoop(){
  36.  
  37.    SendPulse();   //send a pulse to the ultrasonic sensor
  38.    average = RunningAverage(average, echoTime);
  39.  
  40.    char keystroke = checkSerial();
  41.  
  42.    if (keystroke != -1){            //for key tracking
  43.     D_position(0,68);
  44.     Serial.print(F("          "));
  45.     D_position(0,68);
  46.     Serial.write(keystroke);
  47.     Serial.print(F("   "));
  48.     Serial.print(int(keystroke));
  49.   }
  50.  
  51.   D_position(1,68);                    //display current distance reading in centimeters
  52.   Serial.print(F("     "));
  53.   D_position(1, 68);
  54.   Serial.print(USGetDist());
  55.   D_position(1, 76);
  56.   Serial.print(F("Centimeters"));
  57.  
  58.    switch(state){                                  //state machine
  59.    case 0 : normal(keystroke);            break;   //normal operation
  60.    case 1 : Calibration(keystroke);       break;   //calibration
  61.    case 2 : calPoint(keystroke,1);        break;   //set calibration point 1
  62.    case 3 : calPoint(keystroke,2);        break;   //set calibration point 2
  63.    case 4 : calPoint(keystroke,3);        break;   //set calibration point 3
  64.    }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement