Advertisement
Guest User

csci45 proj1

a guest
Jan 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <wiringPi.h>
  2. #include <iostream>
  3. #include <sys/time.h>
  4. #include <softTone.h>
  5. #include <ctime>
  6. #include <softPwm.h>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. #define uchar unsigned char
  12. #define LaserPin 0
  13. #define LEDPin   1
  14. #define BtnPin   3
  15. #define BuzzPin  22
  16. #define TrigPin  7
  17. #define EchoPin  4
  18. #define LedPinRed   24
  19. #define LedPinGreen 26
  20. #define LedPinBlue  27
  21.  
  22. #define  CL1  131
  23. #define  CL2  147
  24. #define  CL3  165
  25. #define  CL4  175
  26. #define  CL5  196
  27. #define  CL6  221
  28. #define  CL7  248
  29.  
  30. #define  CM1  262
  31. #define  CM2  294
  32. #define  CM3  330
  33. #define  CM4  350
  34. #define  CM5  393
  35. #define  CM6  441
  36. #define  CM7  495
  37.  
  38. #define  CH1  525
  39. #define  CH2  589
  40. #define  CH3  661
  41. #define  CH4  700
  42. #define  CH5  786
  43. #define  CH6  882
  44. #define  CH7  990
  45.  
  46.  
  47. void ledColorSet(uchar r_val, uchar g_val, uchar b_val)
  48. {
  49.     softPwmWrite(LedPinRed,   r_val);
  50.     softPwmWrite(LedPinGreen, g_val);
  51.     softPwmWrite(LedPinBlue,  b_val);
  52. }
  53.  
  54.  
  55. float disMeasure(void)
  56. {
  57.     struct timeval tv1;
  58.     struct timeval tv2;
  59.     long time1, time2;
  60.     float dis;
  61.  
  62.     digitalWrite(TrigPin, LOW);
  63.     delayMicroseconds(2);
  64.  
  65.     digitalWrite(TrigPin, HIGH);
  66.     delayMicroseconds(10);    
  67.     digitalWrite(TrigPin, LOW);
  68.  
  69.     while(!(digitalRead(EchoPin) == 1));
  70.     gettimeofday(&tv1, NULL);        
  71.  
  72.     while(!(digitalRead(EchoPin) == 0));
  73.     gettimeofday(&tv2, NULL);    
  74.  
  75.     time1 = tv1.tv_sec * 1000000 + tv1.tv_usec;
  76.     time2  = tv2.tv_sec * 1000000 + tv2.tv_usec;
  77.  
  78.     dis = (float)(time2 - time1) / 1000000 * 34000 / 2;  //求出距离
  79.  
  80.     return dis * 1 / 2.54 / 12.0;
  81. }
  82.  
  83. int main() {
  84.     bool led_status = 0;
  85.     if(wiringPiSetup() == -1) {
  86.         cout << "FAILED" << endl;
  87.         return 1;
  88.     }
  89.  
  90.     if(softToneCreate(BuzzPin) == -1) {
  91.         return 1;
  92.     }
  93.  
  94.     pinMode(LEDPin, OUTPUT);
  95.     pinMode(BtnPin, INPUT);
  96.     pinMode(EchoPin, INPUT);
  97.     pinMode(TrigPin, OUTPUT);
  98.     pinMode(LaserPin, OUTPUT);
  99.     softPwmCreate(LedPinRed,  0, 100);
  100.     softPwmCreate(LedPinGreen,0, 100);
  101.     softPwmCreate(LedPinBlue, 0, 100); 
  102.  
  103.     delay(500);
  104.     while(1) {
  105.         digitalWrite(LaserPin, HIGH);
  106.         if(0 == digitalRead(BtnPin)) {
  107.             led_status = !led_status;
  108.             cout << "Button pressed.." << endl;
  109.             if(!led_status) {
  110.                 cout << "/tLED OFF" << endl;
  111.                 digitalWrite(LEDPin, LOW);
  112.                 delay(500);
  113.                 cout << "Waiting to turn on the laser." << endl;
  114.                 continue;
  115.             }
  116.             srand(time(NULL));
  117.             cout << "\tWe areonna kill" << endl;   
  118.             digitalWrite(LEDPin, HIGH);
  119.             delay(500);
  120.             cout << "\tRANGE SENSOR ON" << endl;
  121.             while(led_status) {
  122.                 float distance = 0.0;
  123.                 distance = disMeasure();
  124.                 if(distance <= .5 || distance >= 7) {
  125.                     softToneWrite(BuzzPin, 0);
  126.                     continue;
  127.                 }
  128.  
  129.                 cout << distance << "ft" << endl;
  130.                 delay(100);
  131.                 int note = ((int)distance * 200) % 1000;
  132.                 softToneWrite(BuzzPin, note);
  133.                 digitalWrite(LEDPin, HIGH);
  134.                 digitalWrite(LaserPin, LOW);
  135.                 delay(50);
  136.                 digitalWrite(LaserPin, HIGH);
  137.                 delay(50);
  138.                 softToneWrite(BuzzPin, 0);
  139.  
  140.                 digitalWrite(LaserPin, LOW);
  141.                 delay(50);
  142.                 softToneWrite(BuzzPin, note+100);
  143.                 digitalWrite(LaserPin, HIGH);
  144.                 delay(50);
  145.                 softToneWrite(BuzzPin, 0);
  146.                 digitalWrite(LEDPin, LOW);
  147.                 int r = note;
  148.                 int g = note * 82 % 256;
  149.                 int b = abs(note - 512) % 256;
  150.                 ledColorSet(r,g,b);
  151.                
  152.                 if(0 == digitalRead(BtnPin)) {
  153.                     led_status = !led_status;
  154.                     cout << "BROKE ULTRASONIC RANGE LOOP VIA BUTTON PRESS." << endl;
  155.                     delay(300);
  156.                     digitalWrite(LEDPin, LOW);
  157.                     softToneWrite(BuzzPin, 0); 
  158.                
  159.                 }
  160.             }
  161.         }
  162.     }
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement