I have this code that I can't seem to get to work. It is a little confusing if you don't know what it does so I will try to explain it as best as I can.
VRx/y are inputs from an analog joystick
SW is the button from pushing down on the joystick
EnableL/R and DirL/R control the speed and direction of rotation of a motor respectively.
The Enable uses analogWrite to do PWM to turn the motor, and the Dir uses digitalWrite to control forward or reverse.
The other define is for a sensor (keyes KY-015 Temp/humidity sensor)
The joystick controls two motors that allow a robot to drive around, and the sensor collects the temp/humidity whenever you click the joystick.
The problem is, if I comment out all of my code that controls the joystick (except for the button) and motors, the temp/humidity readings work just fine whenever I click the button. And if I comment out all of my code that controls the temp/humidity readings then my movement works just fine.
But if I put it all together, only the movement works. If I try to click the button to get the temp/humidity reading, most of the time it just stops the code. Not all of the time though, some times I can get 2-3 readings before it freezes. I have put in Serial.println(); throughout my code and ran it multiple times to try and figure out where it stops working and I have narrowed it down to one loop.
for (i=0; i<5; i++) {
dht_dat = read_dht_dat();
- }*
that loop calls this next one
byte read_dht_dat(){ - byte i = 0;*
- byte result = 0;*
- for(i = 0; i < 8; i++){*
- while(digitalRead(dht_dpin)==LOW);*
- delayMicroseconds(30);*
- if (digitalRead(dht_dpin)==HIGH) {*
- result |= (1<<(7-i));*
- }*
- while (digitalRead(dht_dpin)==HIGH);*
- }*
- return result;*
}
Whenever it breaks it goes through those loops 2-3 times before freezing up and not continuing on. The rest of my code is below (I attached it as well) if you want to take a look, and if you have any questions just let me know and I will answer as best as I can.
const int VRx = A0;
const int VRy = A1;
const int SW = 7; //Use pullup Resistor
#define dht_dpin A2
byte bGlobalErr;
byte dht_dat[5];
int Y = 0;
int X = 0;
int S = 0;
int SpeedL = 255;
int SpeedR = 255;
const int EnableL = 10;
const int EnableR = 11;
const int DirL = 4;
const int DirR = 2;
void setup() { - pinMode(VRx, INPUT);*
- pinMode(VRy, INPUT);*
- pinMode(SW, INPUT);*
- InitDHT();*
- delay(1000);*
- pinMode(EnableL, OUTPUT);*
- pinMode(EnableR, OUTPUT);*
- pinMode(DirL, OUTPUT);*
- pinMode(DirR, OUTPUT);*
- Serial.begin(57600); *
}
void loop() { - S = digitalRead(SW);*
- if (S == 0) {*
- ReadDHT();*
- switch (bGlobalErr){*
- case 0:*
- Serial.print("Current humdity = ");*
- Serial.print(dht_dat[0], DEC);*
- Serial.print(".");*
- Serial.print(dht_dat[1], DEC);*
- Serial.print("% ");*
- Serial.print("temperature = ");*
- Serial.print(dht_dat[2], DEC);*
- Serial.print(".");*
- Serial.print(dht_dat[3], DEC);*
- Serial.println("C ");*
- break;*
- case 1:*
- Serial.println("Error 1: DHT start condition 1 not met.");*
- break;*
- case 2:*
- Serial.println("Error 2: DHT start condition 2 not met.");*
- break;*
- case 3:*
- Serial.println("Error 3: DHT checksum error.");*
- break;*
- default:*
- Serial.println("Error: Unrecognized code encountered.");*
- break;*
- }*
- delay(800);*
- }*
- else {*
- X = map(analogRead(VRx), 0, 1023, 0, 510); //0-L 252-M 510-R*
- Y = map(analogRead(VRy), 0, 1023, 0, 510); //0-T 259-M 510-B*
- if (Y <= 259) {*
- digitalWrite(DirL, LOW);*
- digitalWrite(DirR, LOW);*
- Y = constrain(Y, 0, 255);*
- }*
- else {*
- digitalWrite(DirL, HIGH);*
- digitalWrite(DirR, HIGH);*
- Y = map(Y, 260, 510, 255, 0);*
- Y = constrain(Y, 0, 255);*
- }*
- if (X <= 252) {*
- X = map(X, 0, 252, 0, 255);*
- X = constrain(X, 0, 255);*
- SpeedL = Y;*
- SpeedR = Y + X;*
- SpeedR = map(SpeedR, 0, 507, 0, 255);*
- SpeedR = constrain(SpeedR, 0, 255);*
- }*
- else {*
- X = map(X, 253, 510, 255, 0);*
- X = constrain(X, 0, 255);*
- SpeedL = Y + X;*
- SpeedL = map(SpeedL, 0, 507, 0, 255);*
- SpeedL = constrain(SpeedL, 0, 255);*
- SpeedR = Y;*
- }*
- analogWrite(EnableL, SpeedL);*
- analogWrite(EnableR, SpeedR);*
- } *
}
void InitDHT(){ - pinMode(dht_dpin,OUTPUT);*
- digitalWrite(dht_dpin,HIGH);*
}
void ReadDHT(){ - bGlobalErr=0;*
- byte dht_in;*
- byte i;*
- digitalWrite(dht_dpin,LOW);*
- delay(20);*
- digitalWrite(dht_dpin,HIGH);*
- delayMicroseconds(40);*
- pinMode(dht_dpin,INPUT);*
- dht_in=digitalRead(dht_dpin);*
- if(dht_in){*
- bGlobalErr=1;*
- return;*
- }*
- delayMicroseconds(80);*
- dht_in=digitalRead(dht_dpin);*
- if(!dht_in){*
- bGlobalErr=2;*
- return;*
- }*
- delayMicroseconds(80);*
- for (i=0; i<5; i++) {*
dht_dat = read_dht_dat();
* }*
* pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
byte dht_check_sum = dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
if(dht_dat[4]!= dht_check_sum) {
_ {bGlobalErr=3;}_
_ }_
_};_
byte read_dht_dat(){
_ byte i = 0;_
_ byte result = 0;_
_ for(i = 0; i < 8; i++){_
while(digitalRead(dht_dpin)==LOW);
_ delayMicroseconds(30);_
if (digitalRead(dht_dpin)==HIGH) {
_ result |= (1<<(7-i));_
_ }_
while (digitalRead(dht_dpin)==HIGH);
_ }_
_ return result;_
_}_
_JoystickBot.ino (3.43 KB)*_