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.

114 lines
1.9 KiB

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