I'm working on a project that involves making a four wheeled vehicle that utilizes proximity detection to navigate. I'm utilizing an Arduino Uno and some infrared proximity sensors(Infrared Proximity Sensor Long Range - Sharp GP2Y0A02YK0F - SEN-08958 - SparkFun Electronics). I have two sensors mounted on the front left and right side and my goal is to adjust the speed of the motors depending on whether there is some in front of me or not. For example, with nothing in front of me the motors would be going at 100%, but if an object was detected on the right side the left motors would gradually slow down until the vehicle has turned enough so that the object is no longer in range.
I have tested out my sensors to ensure they work and get a good feel for how they work. Individually they work pretty well and put together in a simple sketch they work about the same. Where I run into trouble is when they are put together and trying to adjust the motor speeds. The code I utilize is below:
void loop(){
int L_proxim = 0;
int R_proxim = 0;
for(int i = 0; i < 5; i ++){
L_proxim += analogRead(Lprox);
}
L_proxim /= 5;
for(int i = 0; i < 5; i ++){
R_proxim += analogRead(Rprox);
}
R_proxim /= 5;
L_speed = leftMotorSpeed(R_proxim);
R_speed = rightMotorSpeed(L_proxim);
move(L_speed, R_speed);
}
int leftMotorSpeed(int proxL){
float motorPer = (theta_prox_L - proxL)/delta_prox_L;
Serial.print("Left perc: ");
Serial.println(motorPer);
Serial.print("Left reading: ");
Serial.println(proxL);
return MAX_SPEED * motorPer; //MAX_SPEED is constant between 0 and 255
}
int rightMotorSpeed(int proxR){
Serial.print("Right reading: ");
Serial.println(proxR);
return MAX_SPEED * (theta_prox_R - proxR)/delta_prox_R; //theta is the middle of the sensor voltage range
//delta is the sensor voltage range
}
void move(int Lspeed, int Rspeed){
boolean Ldirect = HIGH?(Lspeed >= 0):LOW;
boolean Rdirect = HIGH?(Rspeed >= 0):LOW;
digitalWrite(AIN1, Ldirect);
digitalWrite(AIN2, !Ldirect);
digitalWrite(BIN1, Rdirect);
digitalWrite(BIN2, !Rdirect);
analogWrite(PWMA, abs(Lspeed));
analogWrite(PWMB, abs(Rspeed));
return;
}
When I do get some legitimate values I can see that this code works pretty well. The problem is that only about 1/100 values are legitimate. When everything is changing so fast, I figure each loop iteration is around 1ms, give or take. The values coming from the ADC can be a little sporadic, though I have calmed them down a bit utilizing some filtering caps, every once in a while I'll see a value that is way off mark, on the order of +/- 50. This doesn't seem to be the entire problem though, most of the time the math being performed is either way off or doesn't seem to be performed at all, i.e. I can see the sensor was read with nothing in front of it and therefore should make the motor go 100% but instead is sets it to 0.
I can't really see where I'm going wrong with this, I have checked my formula for determining speed over and over and that is good, and from what I've read about the ADC it should be able to handle these conversions with no problem. I've looked all over trying to find anything about doing multiple "simultaneous" conversions on the arduino and haven't come across anything that's helpful. I dug through the wiring_analog.c file and determined that an analogRead(int) will indeed wait for a conversion to finish before it returns so there shouldn't be any issue doing multiple conversions back to back.
Anyone out there have any ideas or have run into similar issues? Is the Uno even capable of performing these kind of calculations fast enough to be viable?