Browse Source

Temperature measurement now worls more reliable.

master
nickg 5 years ago
parent
commit
48327ec5c4
  1. 33
      Code/miniRobot/miniRobot.ino
  2. 23
      Code/miniRobot/temperatureDistance.ino
  3. 13
      Code/miniRobotRC/fahrsteuerung_old.ino
  4. 2
      Code/miniRobotRC/funkInit.ino
  5. 4
      Code/miniRobotRC/miniRobotRC.ino

33
Code/miniRobot/miniRobot.ino

@ -12,9 +12,9 @@
RF24 radio(A0, 3); // CE, CSN
byte commands[32]; //byte 0 = command
long timer;
unsigned long timer;
int16_t temperature = 0;
int16_t distance = 0;
uint16_t distance = 50;
void inline clearCommands() {
for(uint8_t i=0; i<32; i++) {
@ -70,30 +70,36 @@ void setup() {
// motorB.setPWM16(2,RESOLUTION);
radio.begin();
radio.openWritingPipe(address2);
radio.openReadingPipe(0, address);
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
clearCommands();
//Temperatur- und Abstandsmessung
setEchoPins(16, 6); //16: A2, 5: D5
setEchoPins(16, 6); //16: A2, 6: D6
tempDistSetup();
timer = millis();
}
void loop() {
//Temperatur- und Abstandsmessung
Serial.println(temperature);
Serial.println(distance);
temperature = dallas(4, 0);
//Serial.println(temperature);
//Serial.println(distance);
if((unsigned long)(millis() - timer) >= 100){
unsigned long currentMillis = millis();
if((unsigned long)(currentMillis - timer) >= 100){
temperature = dallas(4, 0);
measureDistance();
timer = millis();
distance = calculateDistance();
Serial.println(distance);
timer = currentMillis;
}
distance = calculateDistance();
if (radio.available()) {
@ -108,7 +114,7 @@ void loop() {
if(distance < 20){
Serial.println("Achtung!");
//Serial.println("Achtung!");
if(pwmA < 0 && pwmB < 0){
pwmA = 0;
pwmB = 0;
@ -166,9 +172,10 @@ void commandInterpretation() {
break;
}
case getTemp : {
Serial.println("Senden!");
//Serial.println("Senden!");
radio.stopListening();
radio.write(&temperature, sizeof(int16_t));
int16_t sendData = temperature;
radio.write(&sendData, sizeof(int16_t));
radio.startListening();
break;
}

23
Code/miniRobot/temperatureDistance.ino

@ -5,14 +5,15 @@
int trig;
int echo;
//int distance = 0;
int distance2 = 0;
uint16_t distance3 = 0;
uint16_t distance2 = 0;
//ISR for PCINT20
ISR(PCINT2_vect) {
distance = pulseIn(echo, HIGH);
if(distance > 0){
distance2 = distance;
distance3 = pulseIn(echo, HIGH);
if(distance3 > 0){
distance2 = distance3;
}
delayMicroseconds(10);
@ -26,12 +27,11 @@ void setEchoPins(int pin1, int pin2){
}
int16_t calculateDistance(){
int16_t result = distance2/ 58;
if(result > maxDistance){
result = maxDistance;
int16_t result2 = distance2/ 58;
if(result2 > maxDistance){
result2 = maxDistance;
}
return result;
return result2;
}
void measureDistance(){
@ -89,6 +89,7 @@ void tempDistSetup(){
// Reset the trigger pin and wait a half a second
digitalWrite(trig, LOW);
delayMicroseconds(500);
Serial.println(echo);
sei();
@ -102,7 +103,7 @@ void runMeasurements(){
}
Serial.print("Temperatur: ");
Serial.println(dallas(2, 0));
Serial.println(dallas(4, 0));
Serial.print("Distanz: ");
Serial.print(calculateDistance());

13
Code/miniRobotRC/fahrsteuerung_old.ino

@ -51,26 +51,31 @@ void manualDigitalDrive() {
}
//Temperatur <3
if((unsigned long)(millis() - temp_time) >= 1000){ // jede Sekunden
unsigned long currentMillis = millis();
if((unsigned long)(currentMillis - temp_time) >= 1000){ // jede Sekunden
temp_time = millis();
bool err = false;
clearCommands();
commands[0] = getTemp;
radio.write(&commands, sizeof(commands) && !err);
unsigned long start = micros();
radio.startListening();
while(!radio.available()){
//Serial.println("nix");
if((unsigned long)(micros()- start) >= 1){
unsigned long currentMicros = micros();
if((unsigned long)(currentMicros - start) >= 1){
err = true;
}
}
if(!err){
radio.read(&temperature, sizeof(int16_t));
int16_t readData;
radio.read(&readData, sizeof(int16_t));
temperature = readData;
}
radio.stopListening();
clearCommands();
}
// }

2
Code/miniRobotRC/funkInit.ino

@ -2,7 +2,7 @@ void funkInit() {
radio.begin();
radio.openWritingPipe(address);
radio.openReadingPipe(0, address2);
radio.openReadingPipe(1, address2);
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();
}

4
Code/miniRobotRC/miniRobotRC.ino

@ -39,8 +39,8 @@ bool forwardB = true;
bool driveOn = false;
uint16_t driveTimeout = 0;
int16_t distance;
int16_t temperature = 0;
uint8_t distance;
uint8_t temperature = 0;
unsigned long temp_time = millis();

Loading…
Cancel
Save