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.

115 lines
1.9 KiB

  1. #include <OneWire.h>
  2. #define maxDistance 400
  3. int trig = 0;
  4. int echo = 0;
  5. //int distance = 0;
  6. int distance2 = 0;
  7. //ISR for PCINT20
  8. ISR(PCINT2_vect) {
  9. distance = pulseIn(echo, HIGH);
  10. if(distance > 0){
  11. distance2 = distance;
  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. int calculateDistance(){
  22. int result = distance2/ 58;
  23. if(result > maxDistance){
  24. result = maxDistance;
  25. }
  26. return result;
  27. }
  28. void measureDistance(){
  29. PORTD |= (1<<trig);
  30. // ... wait for 10 µs ...
  31. delayMicroseconds(10);
  32. // ... put the trigger down ...
  33. PORTD &= ~(1<<trig);
  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. result += data[i];
  50. }
  51. result = result/2;
  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. sei();
  70. }
  71. void runMeasurements(){
  72. long run_time = micros();
  73. if(millis() - timer >= 100){
  74. measureDistance();
  75. timer = millis();
  76. }
  77. Serial.print("Temperatur: ");
  78. Serial.println(dallas(2, 0));
  79. Serial.print("Distanz: ");
  80. Serial.print(calculateDistance());
  81. Serial.println("cm");
  82. run_time = micros() - run_time;
  83. Serial.print("Zeit für Durchlauf: ");
  84. Serial.println(run_time);
  85. Serial.println();
  86. delay(1000);
  87. }