Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. #define SRF_TXRX        0x05
  4. #define SRF_ADDRESS     0x01
  5. #define GETRANGE        0x54
  6.  
  7. SoftwareSerial srf01 = SoftwareSerial(SRF_TXRX, SRF_TXRX);
  8. SoftwareSerial srf02 = SoftwareSerial(0x04, 0x04);
  9.  
  10. #include <Adafruit_NeoPixel.h>
  11. #ifdef __AVR__
  12. #include <avr/power.h>
  13. #endif
  14.  
  15. #define PIN 6
  16.  
  17. // Parameter 1 = number of pixels in strip
  18. // Parameter 2 = Arduino pin number (most are valid)
  19. // Parameter 3 = pixel type flags, add together as needed:
  20. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  21. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  22. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  23. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  24. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  25. Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, PIN, NEO_GRB + NEO_KHZ800);
  26.  
  27. void setup() {
  28.   srf01.begin(9600);
  29.   srf02.begin(9600);
  30.   Serial.begin(9600);
  31.   strip.begin();
  32.   strip.show();
  33.   delay(200);
  34.   Serial.println("SRF01 fgdgfsfsdfds");
  35. }
  36.  
  37. void loop() {
  38.   int range = getRange1();
  39.   int range2 = getRange2();
  40.   turnOff();
  41.   if ( range > 0 && range < 100 ) {
  42.     for ( int i = 0; i < (strip.numPixels()/3); i++) {
  43.       strip.setPixelColor(i, strip.Color(50, 0, 0));
  44.     }
  45.   }
  46.   if ( range2 > 0 && range2 < 100) {
  47.     for ( int i = strip.numPixels()/3*2; i < strip.numPixels(); i++) {
  48.       strip.setPixelColor(i, strip.Color(50, 0, 0));
  49.     }
  50.   }
  51.   if ( range > 0 && range < 100 && range2 > 0 && range2 < 100 ) {
  52.     for ( int i = strip.numPixels()/3; i < strip.numPixels()/3*2; i++) {
  53.       strip.setPixelColor(i, strip.Color(50, 0, 0));
  54.     }
  55.   }
  56.   strip.show();
  57. }
  58.  
  59. int getRange1() {
  60.   srf01.listen();
  61.   byte hByte, lByte;
  62.   SRF01_Cmd(SRF_ADDRESS, GETRANGE);                       // Get the SRF01 to perform a ranging and send the data back to the arduino
  63.   while (srf01.available() < 2);
  64.   hByte = srf01.read();                                   // Get high byte
  65.   lByte = srf01.read();                                   // Get low byte
  66.   int range = ((hByte << 8) + lByte);                     // Put them together
  67.   Serial.print("range1\t");
  68.   Serial.println(range);
  69.   return range;
  70. }
  71. int getRange2() {
  72.   srf02.listen();
  73.   byte hByte, lByte;
  74.   SRF02_Cmd(SRF_ADDRESS, GETRANGE);                       // Get the SRF01 to perform a ranging and send the data back to the arduino
  75.   while (srf02.available() < 2);
  76.   hByte = srf02.read();                                   // Get high byte
  77.   lByte = srf02.read();                                   // Get low byte
  78.   int range = ((hByte << 8) + lByte);                     // Put them together
  79.   Serial.print("range2\t");
  80.   Serial.println(range);
  81.   return range;
  82. }
  83.  
  84. void SRF01_Cmd(byte Address, byte cmd) {              // Function to send commands to the SRF01
  85.   pinMode(SRF_TXRX, OUTPUT);
  86.   digitalWrite(SRF_TXRX, LOW);                        // Send a 2ms break to begin communications with the SRF01
  87.   delay(2);
  88.   digitalWrite(SRF_TXRX, HIGH);
  89.   delay(1);
  90.   srf01.write(Address);                               // Send the address of the SRF01
  91.   srf01.write(cmd);                                   // Send commnd byte to SRF01
  92.   pinMode(SRF_TXRX, INPUT);
  93.   int availbleJunk = srf01.available();               // As RX and TX are the same pin it will have recieved the data we just sent out, as we dont want this we read it back and ignore it as junk before waiting for useful data to arrive
  94.   for (int x = 0;  x < availbleJunk; x++) byte junk = srf01.read();
  95. }
  96. void SRF02_Cmd(byte Address, byte cmd) {              // Function to send commands to the SRF01
  97.   pinMode(0x04, OUTPUT);
  98.   digitalWrite(0x04, LOW);                        // Send a 2ms break to begin communications with the SRF01
  99.   delay(2);
  100.   digitalWrite(0x04, HIGH);
  101.   delay(1);
  102.   srf02.write(Address);                               // Send the address of the SRF01
  103.   srf02.write(cmd);                                   // Send commnd byte to SRF01
  104.   pinMode(0x04, INPUT);
  105.   int availbleJunk = srf02.available();               // As RX and TX are the same pin it will have recieved the data we just sent out, as we dont want this we read it back and ignore it as junk before waiting for useful data to arrive
  106.   for (int x = 0;  x < availbleJunk; x++) byte junk = srf02.read();
  107. }
  108.  
  109. void turnOff()
  110. {
  111.   for (uint16_t i = 0; i < strip.numPixels(); i++)
  112.   {
  113.     strip.setPixelColor(i, strip.Color(0, 0, 0));
  114.   }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement