Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.0 KiB

  1. #include <OneWire.h>
  2. #define maxDistance 400
  3. int trig;
  4. int echo;
  5. uint16_t distance3 = 0;
  6. uint16_t distance2 = 0;
  7. //ISR for PCINT20
  8. ISR(PCINT2_vect) {
  9. distance3 = pulseIn(echo, HIGH);
  10. if(distance3 > 0){
  11. distance2 = distance3;
  12. }
  13. delayMicroseconds(10);
  14. PCICR &= ~0b00000100;
  15. PCMSK2 &= ~0b00010000;
  16. }
  17. void setEchoPins(int pin1, int pin2){
  18. trig = pin1;
  19. echo = pin2;
  20. }
  21. int16_t calculateDistance(){
  22. int16_t result2 = distance2/ 58;
  23. if(result2 > maxDistance){
  24. result2 = maxDistance;
  25. }
  26. return result2;
  27. }
  28. void measureDistance(){
  29. digitalWrite(trig, HIGH);
  30. // ... wait for 10 µs ...
  31. delayMicroseconds(10);
  32. // ... put the trigger down ...
  33. digitalWrite(trig, LOW);
  34. PCICR |= 0b00000100;
  35. PCMSK2 |= 0b00010000;
  36. }
  37. int16_t dallas(int x, byte start){
  38. OneWire ds(x);
  39. byte i;
  40. byte data[2];
  41. int16_t result;
  42. do{
  43. ds.reset();
  44. ds.write(0xCC); //skip Command
  45. ds.write(0xBE); //Read 1st 2 bytes of Scratchpad
  46. result = 0;
  47. for(i = 0; i < 2; i++){
  48. data[i] = ds.read();
  49. }
  50. result = (data[1]<<8)|data[0];
  51. result = result/16; //DS1820 2 DS18B20 16
  52. ds.reset();
  53. ds.write(0xCC);
  54. ds.write(0x44, 1); //start conversion
  55. if(start){
  56. delay(1000);
  57. }
  58. } while(start--);
  59. return result;
  60. }
  61. void tempDistSetup(){
  62. dallas(4, 1);
  63. // Initializing Trigger Output and Echo Input
  64. pinMode(trig, OUTPUT);
  65. pinMode(echo, INPUT);
  66. // Reset the trigger pin and wait a half a second
  67. digitalWrite(trig, LOW);
  68. delayMicroseconds(500);
  69. Serial.println(echo);
  70. sei();
  71. }
  72. void runMeasurements(){
  73. long run_time = micros();
  74. if(millis() - timer >= 100){
  75. measureDistance();
  76. timer = millis();
  77. }
  78. Serial.print("Temperatur: ");
  79. Serial.println(dallas(4, 0));
  80. Serial.print("Distanz: ");
  81. Serial.print(calculateDistance());
  82. Serial.println("cm");
  83. run_time = micros() - run_time;
  84. Serial.print("Zeit für Durchlauf: ");
  85. Serial.println(run_time);
  86. Serial.println();
  87. delay(1000);
  88. }