Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
In the following code I am testing for the value of float s so that if the function DriveForwardFullSpeed is called and it has already set the value of s to 1.0 the function is dropped out and returns back to main. So the while loop keeps reading the srf08 and tests the value. Whats happening is the loop keeps ramping up the motors as if the value of s is being overwritten but I cant see how? Any ideas?
#include "mbed.h" #include "SRF08.h" #include "TextLCD.h" SRF08 srf08(p9, p10, 0xE0); // Define SDA, SCL pin and I2C address // TMP102 temperature(p9, p10, 0x90); Use later PwmOut MotorRightWheel(p23); // pwm PwmOut MotorLeftWheel(p25); // pwm DigitalOut DirectionR=p27; DigitalOut DirectionL=p29; DigitalOut BrakeR=p28; DigitalOut BrakeL=p30; // TMP102 temperature(p9, p10, 0x90); TextLCD lcd(p15, p16, p17, p18, p19, p20); float RangeReading() { float value; value = srf08.read(); return (value); } void DriveForwardFullSpeed() {float s; if (s > 0.9) return; for (s= 0.0; s < 1.0 ; s += 0.01) { DirectionR=1;//Set the direction Right motor DirectionL=1;//Set the direction Left motor BrakeR = BrakeL = 0; MotorRightWheel=s; MotorLeftWheel=s; wait(0.2); } } void DriveForwardSlow() { for (float s= 1.0; s > 0.5 ; s -= 0.01) { DirectionR=1;//Set the direction Right motor DirectionL=1;//Set the direction Left motor BrakeR = BrakeL = 0; MotorRightWheel=s; MotorLeftWheel=s; wait(0.2); } } void DriveReverse() { for (float s= 0.0; s < 1.0 ; s += 0.01) { DirectionR=0;//Set the direction Right motor DirectionL=0;//Set the direction Left motor BrakeR = BrakeL = 0; MotorRightWheel=s; MotorLeftWheel=s; wait(0.2); } } void ZeroSpeedForward() { for (float s = 0.5; s > 0.0; s -= 0.01) { DirectionR=1;//Set the direction Right motor DirectionL=1;//Set the direction Left motor BrakeR = BrakeL = 0; MotorRightWheel=s; MotorLeftWheel=s; wait(0.2); } } void ZeroSpeedReverse() { for (float s = 1.0; s > 0.0; s -= 0.01) { DirectionR=0;//Set the direction Right motor DirectionL=0;//Set the direction Left motor BrakeR = BrakeL = 0; MotorRightWheel=s; MotorLeftWheel=s; wait(0.2); } } int main() { float distance; while (1) { lcd.cls(); distance = RangeReading(); lcd.printf("Range: %2.f cm\n",distance); wait(0.2); // lcd.printf("Temp: %.2f degC\n", temperature.read()); if (distance >100 ) DriveForwardFullSpeed(); // else if (distance > 80 || distance < 100) // DriveForwardSlow(); // else if (distance < 50) // ZeroSpeedForward(); // wait(1); // DriveReverse(); // wait(1); // ZeroSpeedReverse(); // wait(5); } }