Browse Source

Update 'Autodriving/motortest_first.ino'

master
Gab_S 5 years ago
parent
commit
a62584b0df
  1. 277
      Autodriving/motortest_first.ino

277
Autodriving/motortest_first.ino

@ -1,127 +1,150 @@
#include "Motorz.h"; //very crude libraries with objects for motor and tandrive system (2 motors no steering)
#include "tankdrive.h"; //tandrive is an object that you attach the motor objects you make bellow made some functions
#include <NewPing.h> //in hope it will be more easier to read etc.
#include <DHT.h>
#include "Motorz.h";
#define DHTPIN 2 #include "tankdrive.h";
#define iterations 3 #include <NewPing.h>
#define SONAR_L_PIN 3 #include <DHT.h>
#define SONAR_M_PIN 4
#define SONAR_R_PIN 5 #define DHTPIN 2 // temp humidity sensor pin (why? sound speed changes according to temp/humidity)
#define MAX_DISTANCE 400 #define iterations 3 // can be used with function ping_median from new ping (takes median value ,unused)
#define SONAR_L_PIN 3 //left sonar sensor 3 pin configuration (trig&echo) are bound to 1 pin
NewPing sonarL(SONAR_L_PIN,SONAR_L_PIN, MAX_DISTANCE); #define SONAR_M_PIN 4 // middle sonar sensor
NewPing sonarM(SONAR_M_PIN,SONAR_M_PIN, MAX_DISTANCE); #define SONAR_R_PIN 5 // right sonar sensor
NewPing sonarR(SONAR_R_PIN,SONAR_R_PIN, MAX_DISTANCE); #define MAX_DISTANCE 400 //max sonar dinstance according to the type of sensor i use Hc-SR04
DHT dht(DHTPIN, DHT11);
NewPing sonarL(SONAR_L_PIN,SONAR_L_PIN, MAX_DISTANCE);
float soundcm; NewPing sonarM(SONAR_M_PIN,SONAR_M_PIN, MAX_DISTANCE);
float temp=20.0; NewPing sonarR(SONAR_R_PIN,SONAR_R_PIN, MAX_DISTANCE);
float hum=50.0;
float duration; DHT dht(DHTPIN, DHT11);
unsigned long temp_startcnt; float soundcm;
unsigned long temp_currcnt; float temp=20.0;
int i; float hum=50.0;
int dist[3]; float duration;
int enr=10;
int in1=11; //calculating speed of sound uses floats and multiplications we don't want to bog down
int in2=12; // our already slow and weak cpu so i use a the variables bellow along with function millis()
int directr=1; // to take measurments every half minute (still too often in my opinion)
int enl=9;
int in3=7; unsigned long temp_startcnt;
int in4=8; unsigned long temp_currcnt;
int directl=0;
int i;
Motorz motorR(enr,in1,in2,directr); int dist[3]; //table that holds the distances the sensors measured
Motorz motorL(enl,in3,in4,directl);
TankDrive mytank(&motorR,directr,&motorL,directl); //using an L298N motor driver module
void setup(){ int enr=10; // enable pin for motor r (must be pwm)
temp_startcnt=millis(); int in1=11; // pin for one of the two connections of the mottor
dht.begin(); int in2=12;// other connection (these two are digital pins)
Serial.begin(115200); // depends on how you end up wiring and placing the motors it will spin a certain way
Serial.println("Setup"); // and another way when you reverse the voltage , direct holds (0,1) depending on how you
motorR.SetSpeed(200); // wired the motor to go "forwards" what ever that means for your project
motorL.SetSpeed(60); int directr=1;
soundcm = 331.4 + (0.606 * temp) + (0.0124 * hum) ; int enl=9;
soundcm=soundcm/10000; int in3=7;
int in4=8;
} int directl=0;
Motorz motorR(enr,in1,in2,directr);
void loop() Motorz motorL(enl,in3,in4,directl);
{
temp_currcnt=millis(); TankDrive mytank(&motorR,directr,&motorL,directl);
if((temp_currcnt-temp_startcnt)>=30000){
GetTemps(); void setup(){
Serial.println(); temp_startcnt=millis(); //temp&humidity counter
Serial.println(); dht.begin(); //it needs this
Serial.print("temps aquaried "); Serial.begin(115200); //serial monitor for debugging
Serial.print(millis()); Serial.println("Setup");
Serial.println(); motorR.SetSpeed(200); //im using diffrent motors (trying to simulate wheels so i figure things out )
Serial.println(); motorL.SetSpeed(60); // so that explains the diffrent speed in each motor
} soundcm = 331.4 + (0.606 * temp) + (0.0124 * hum) ;
soundcm=soundcm/10000; //speed of sound per cm
GetDistances();
if(dist[1]>5){ }
Serial.print("Moving forward\n");
mytank.MoveForward();
} void loop()
else{ {
Serial.print("Object detected\n"); temp_currcnt=millis();
mytank.Stop(); if((temp_currcnt-temp_startcnt)>=30000){
} GetTemps();
Serial.println();
/* Serial.println();
mytank.MoveForward(); Serial.print("temps aquaried ");
delay(5000); Serial.print(millis());
mytank.Stop(); Serial.println();
delay(2000); Serial.println();
mytank.TurnRight(); }
delay(5000); //for now it only detects things from the front sensor but
mytank.Stop(); // with only few lines and ifs you can make it better
delay(2000); // moves forward until there's something 5cm in front of it
mytank.MoveBack(); // carefull this sonar sensors aren't that accurate in < 6 cm distances in my opinion
delay(5000);
mytank.Stop(); GetDistances();
delay(2000); if(dist[1]>5){
*/ Serial.print("Moving forward\n");
} mytank.MoveForward();
}
void GetDistances(){ else{
/*duration = sonarL.ping_median(iterations); Serial.print("Object detected\n");
dist[0]=(duration / 2) * soundcm; mytank.Stop();
duration = sonarM.ping_median(iterations); }
dist[1]=(duration / 2) * soundcm;
duration = sonarR.ping_median(iterations); /*
dist[2]=(duration / 2) * soundcm; */ mytank.MoveForward();
duration = sonarL.ping(); delay(5000);
dist[0]=(duration / 2) * soundcm; mytank.Stop();
delay(100); delay(2000);
duration = sonarM.ping(); mytank.TurnRight();
dist[1]=(duration / 2) * soundcm; delay(5000);
delay(100); mytank.Stop();
duration = sonarR.ping(); delay(2000);
dist[2]=(duration / 2) * soundcm; mytank.MoveBack();
delay(100); delay(5000);
Serial.print("Distance L:"); mytank.Stop();
Serial.print(dist[0]); delay(2000);
Serial.print(" M:"); */
Serial.print(dist[1]); }
Serial.print(" R:");
Serial.print(dist[2]); void GetDistances(){
Serial.print(" Temp:"); /*duration = sonarL.ping_median(iterations);
Serial.print(temp); dist[0]=(duration / 2) * soundcm;
Serial.print(" Humidity:"); duration = sonarM.ping_median(iterations);
Serial.print(hum); dist[1]=(duration / 2) * soundcm;
Serial.println(); duration = sonarR.ping_median(iterations);
} dist[2]=(duration / 2) * soundcm; */
void GetTemps(){ duration = sonarL.ping(); //sending supersonic sound wave and measuring the time it takes to come back
hum = dht.readHumidity(); // Get Humidity value dist[0]=(duration / 2) * soundcm; // devide by 2 (hit object came back ) and multiply by speed of sound
temp= dht.readTemperature(); // Get Temp value delay(100); //the delay is crusial since we're firing one sensor after another
temp_startcnt=temp_currcnt; duration = sonarM.ping(); //we don't want noise from one sonar waver of one sensors affecting another
dist[1]=(duration / 2) * soundcm;
} delay(100);
duration = sonarR.ping(); /
dist[2]=(duration / 2) * soundcm;
delay(100);
Serial.print("Distance L:");
Serial.print(dist[0]);
Serial.print(" M:");
Serial.print(dist[1]);
Serial.print(" R:");
Serial.print(dist[2]);
Serial.print(" Temp:");
Serial.print(temp);
Serial.print(" Humidity:");
Serial.print(hum);
Serial.println();
}
void GetTemps(){
hum = dht.readHumidity(); // Get Humidity value
temp= dht.readTemperature(); // Get Temp value
temp_startcnt=temp_currcnt;
soundcm = 331.4 + (0.606 * temp) + (0.0124 * hum) ;
soundcm=soundcm/10000; //speed of sound per cm
}

Loading…
Cancel
Save