From ab93aa6db1bd743a27d4e429e558405d68c85e63 Mon Sep 17 00:00:00 2001 From: Nick Gnoevoj Date: Wed, 15 Jan 2020 21:59:11 +0100 Subject: [PATCH] =?UTF-8?q?Temperatur=20und=20Abstandsmessung=20hinzugef?= =?UTF-8?q?=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Temperatur_und_Abstandsmessung.ino | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Code/Temperatur_und_Abstandsmesung/Temperatur_und_Abstandsmessung.ino diff --git a/Code/Temperatur_und_Abstandsmesung/Temperatur_und_Abstandsmessung.ino b/Code/Temperatur_und_Abstandsmesung/Temperatur_und_Abstandsmessung.ino new file mode 100644 index 0000000..60cd80e --- /dev/null +++ b/Code/Temperatur_und_Abstandsmesung/Temperatur_und_Abstandsmessung.ino @@ -0,0 +1,115 @@ +#include + +#define trig PD3 +#define echo PD4 + +long distance = 0; +int count = 0; + +// ISR For Timer +ISR(TIMER0_COMPA_vect){ + + //set count depending on tcnt0 and the prescaler + if(count == 96){ + count = 0; + TCNT0 = 0; + + digitalWrite(trig, HIGH); + // ... wait for 10 µs ... + + delayMicroseconds(10); + // ... put the trigger down ... + digitalWrite(trig, LOW); + + PCICR |= 0b00000100; + PCMSK2 |= 0b00010000; + + } else { + count++; + } +} + +//ISR for PCINT20 +ISR(PCINT2_vect) { + distance = pulseIn(echo, HIGH); + + PCICR &= ~0b00000100; + PCMSK2 &= ~0b00010000; + delayMicroseconds(10); +} + +long calculateDistance(){ + return (long) (((float) distance/ 58.0)); +} + +int16_t dallas(int x, byte start){ + + OneWire ds(x); + byte i; + byte data[2]; + int16_t result; + + do{ + ds.reset(); + ds.write(0xCC); //skip Command + ds.write(0xBE); //Read 1st 2 bytes of Scratchpad + result = 0; + for(i = 0; i < 2; i++){ + data[i] = ds.read(); + result += data[i]; + } + + result = result/2; + ds.reset(); + ds.write(0xCC); + ds.write(0x44, 1); //start conversion + if(start){ + delay(1000); + } + } while(start--); + return result; +} + +void initS(){ + + TCNT0 = 0; // reset timer + TCCR0A |= 0b00000010; + TCCR0B |= 0b0000011; //set prescaler to 64 becasue 256 does not work + OCR0A = 255; + TIMSK0 |= 0b00000010; + sei(); +} + +void setup(){ + Serial.begin(9600); + dallas(2, 1); + + // Initializing Trigger Output and Echo Input + pinMode(trig, OUTPUT); + pinMode(echo, INPUT); + + // Reset the trigger pin and wait a half a second + digitalWrite(trig, LOW); + delayMicroseconds(500); + + initS(); +} + +void loop(){ + + long run_time = micros(); + + Serial.print("Temperatur: "); + Serial.println(dallas(2, 0)); + + Serial.print("Distanz: "); + Serial.print(calculateDistance()); + Serial.println("cm"); + + run_time = micros() - run_time; + Serial.print("Zeit für Durchlauf: "); + Serial.println(run_time); + Serial.println(); + delay(1000); + +}