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.

64 lines
1.1 KiB

  1. #define maxDistance 400
  2. int trig;
  3. int echo;
  4. //ISR for PCINT2
  5. ISR(PCINT2_vect) {
  6. if((PIND & 64) > 0) {
  7. pulseStart = micros();
  8. } else {
  9. startNewMeasurement = true;
  10. pulseLength = micros() - pulseStart;
  11. newResult = true;
  12. }
  13. PCIFR = 0;
  14. }
  15. void setEchoPins(int pin1, int pin2){
  16. trig = pin1;
  17. echo = pin2;
  18. PCIFR = 0;
  19. PCICR |= 0b00000100;
  20. PCMSK2 |= (1 << echo);
  21. // Initializing Trigger Output and Echo Input
  22. pinMode(trig, OUTPUT);
  23. pinMode(echo, INPUT);
  24. // Reset the trigger pin and wait a half a second
  25. digitalWrite(trig, LOW);
  26. delayMicroseconds(500);
  27. }
  28. int16_t calculateDistance(){
  29. int16_t result = 1;
  30. if(pulseLength > 0) {
  31. result = (int16_t)(pulseLength / 58);
  32. }
  33. if(result > maxDistance){
  34. result = maxDistance;
  35. }
  36. newResult = false;
  37. //Serial.println(result);
  38. return result;
  39. }
  40. void measureDistance(){
  41. digitalWrite(trig, HIGH);
  42. // ... wait for 10 µs ...
  43. delayMicroseconds(10);
  44. // ... put the trigger down ...
  45. digitalWrite(trig, LOW);
  46. //Serial.println("messe...");
  47. startNewMeasurement = false;
  48. }