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
2.0 KiB

4 years ago
  1. #include <OneWire.h>
  2. #define trig PD3
  3. #define echo PD4
  4. long distance = 0;
  5. int count = 0;
  6. // ISR For Timer
  7. ISR(TIMER0_COMPA_vect){
  8. //set count depending on tcnt0 and the prescaler
  9. if(count == 96){
  10. count = 0;
  11. TCNT0 = 0;
  12. digitalWrite(trig, HIGH);
  13. // ... wait for 10 µs ...
  14. delayMicroseconds(10);
  15. // ... put the trigger down ...
  16. digitalWrite(trig, LOW);
  17. PCICR |= 0b00000100;
  18. PCMSK2 |= 0b00010000;
  19. } else {
  20. count++;
  21. }
  22. }
  23. //ISR for PCINT20
  24. ISR(PCINT2_vect) {
  25. distance = pulseIn(echo, HIGH);
  26. PCICR &= ~0b00000100;
  27. PCMSK2 &= ~0b00010000;
  28. delayMicroseconds(10);
  29. }
  30. long calculateDistance(){
  31. return (long) (((float) distance/ 58.0));
  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 initS(){
  58. TCNT0 = 0; // reset timer
  59. TCCR0A |= 0b00000010;
  60. TCCR0B |= 0b0000011; //set prescaler to 64 becasue 256 does not work
  61. OCR0A = 255;
  62. TIMSK0 |= 0b00000010;
  63. sei();
  64. }
  65. void setup(){
  66. Serial.begin(9600);
  67. dallas(2, 1);
  68. // Initializing Trigger Output and Echo Input
  69. pinMode(trig, OUTPUT);
  70. pinMode(echo, INPUT);
  71. // Reset the trigger pin and wait a half a second
  72. digitalWrite(trig, LOW);
  73. delayMicroseconds(500);
  74. initS();
  75. }
  76. void loop(){
  77. long run_time = micros();
  78. Serial.print("Temperatur: ");
  79. Serial.println(dallas(2, 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. }