basic version
Dependencies: C12832_lcd USBHost mbed
Diff: main.cpp
- Revision:
- 8:b22ef9f44549
- Parent:
- 7:0fa7430ab812
- Child:
- 9:88d70b0c1b8b
--- a/main.cpp Mon Jan 26 14:58:21 2015 +0000 +++ b/main.cpp Mon Jan 26 23:01:46 2015 +0000 @@ -7,59 +7,59 @@ float servoPosition; //Mutex -Mutex sonarDistance_mutex; //Init Mutex for sensor value -Mutex servoPosition_mutex; //Init Mutex for servo value -Mutex LED_mutex; +Mutex sonarDistance_mutex; //Init Mutex for sensor value +Mutex servoPosition_mutex; //Init Mutex for servo value //I/O -AnalogIn sonarPin(p17); //Assign pin to read sonar sensor -PwmOut servoPin(p21); //Assign pin for servo output -DigitalOut myled1(LED1); -DigitalOut myled2(LED2); -DigitalOut myled3(LED3); -DigitalOut myled4(LED4); +AnalogIn sonarPin(p17); //Assign pin to read sonar sensor +PwmOut servoPin(p21); //Assign pin for servo output +PwmOut myled1(LED1); //Assign pin for LED1 +PwmOut myled2(LED2); //Assign pin for LED2 +PwmOut myled3(LED3); //Assign pin for LED3 +PwmOut myled4(LED4); //Assign pin for LED4 //LCD Setup -C12832_LCD lcd; //setup LCD screen +C12832_LCD lcd; //setup LCD screen void sonarSensor(void const *args){ while(true){ - sonarDistance_mutex.lock(); //Mutex lock for the Servo value - sonarDistance = sonarPin.read(); //Read analog voltage and store in variable - sonarDistance_mutex.unlock(); //Mutex unlock for the Servo value - Thread::wait(10); //Pause thread for 10msec + sonarDistance_mutex.lock(); //Mutex lock for the Servo value + sonarDistance = sonarPin.read(); //Read analog voltage and store in variable + sonarDistance_mutex.unlock(); //Mutex unlock for the Servo value + Thread::wait(10); //Pause thread for 10msec } //End of While loop } //End of Thread void servoControl(void const *args){ while(true){ - servoPosition_mutex.lock(); //Mutex lock for the Servo value - servoPin.write(servoPosition); //Write servo value to servo pin - servoPosition_mutex.unlock(); //Mutex unlock for the Servo value + servoPosition_mutex.lock(); //Mutex lock for the Servo value + servoPin.write(servoPosition/2); //Write servo value to servo pin + servoPosition_mutex.unlock(); //Mutex unlock for the Servo value } //end of while loop } //end of thread void logic(void const *args) { - int Size=12; - float Average_4[12]; //number of value in the array + int Size=16; + float Average_4[16]; //number of value in the array float Average_Sum=0; while(true) { for(int i=0;i<Size;i++) { - Average_Sum = Average_Sum-Average_4[i]; //Remove the 4th oldest value from the average sum - - sonarDistance_mutex.lock(); //Mutex lock for the Sonar value - Average_4[i]= sonarDistance; //Add the new value to the array - sonarDistance_mutex.unlock(); //Mutex unlock for the Sonar value - Average_Sum = Average_Sum + Average_4[i]; //Add the new array value to the sum + Average_Sum = Average_Sum-Average_4[i]; //Remove the 4th oldest value from the average sum + + sonarDistance_mutex.lock(); //Mutex lock for the Sonar value + Average_4[i]= sonarDistance; //Add the new value to the array + sonarDistance_mutex.unlock(); //Mutex unlock for the Sonar value - servoPosition_mutex.lock(); //Mutex lock for the servo value - servoPosition = Average_Sum/Size; //Divide the array by the number of element in the array - servoPosition_mutex.unlock(); //Mutex unlock for the servo value + Average_Sum = Average_Sum + Average_4[i]; //Add the new array value to the sum + + servoPosition_mutex.lock(); //Mutex lock for the servo value + servoPosition = Average_Sum/Size; //Divide the array by the number of element in the array + servoPosition_mutex.unlock(); //Mutex unlock for the servo value }//end for loop @@ -68,62 +68,62 @@ void display(void const *args){ while(true){ - sonarDistance_mutex.lock(); - servoPosition_mutex.lock(); - lcd.cls(); - lcd.locate(0,0); - lcd.printf("Sonar : %3.2f \nServo : %3.2f \nSCIENCE!",sonarDistance,servoPosition); - sonarDistance_mutex.unlock(); - servoPosition_mutex.unlock(); - Thread::wait(200); + sonarDistance_mutex.lock(); //Mutex lock for the sonar value + servoPosition_mutex.lock(); //Mutex lock for the servo value + lcd.cls(); //Clear the lcd + lcd.locate(0,0); //Cursor postion on the lcd + lcd.printf("Sonar : %3.6f \nServo : %3.6f \nSCIENCE!",sonarDistance,servoPosition); + sonarDistance_mutex.unlock(); //Mutex unlock for the sonar value + servoPosition_mutex.unlock(); //Mutex unlock for the servo value + Thread::wait(200); //Thread Wait } } void LED_meter(void const *args) { - float LED_distance = 0; + float LED_distance = 0; //Create var to store sonar distance while(true) { - sonarDistance_mutex.lock(); - LED_distance=sonarDistance; - sonarDistance_mutex.unlock(); + sonarDistance_mutex.lock(); //Mutex lock for the sonar value + LED_distance=sonarDistance; //Store value of sonar sensor in LED_distance + sonarDistance_mutex.unlock(); //Mutex unlock for the sonar value - if(LED_distance<=0.2) + if(LED_distance<=0.15) { - myled1 = 1; + myled1.write(((LED_distance-0.15)*-1)*20); //PWM the LED from 0% to 100% } else { - myled1 = 0; + myled1 = 0; //Turn off LED } - if(LED_distance<=0.15) + if(LED_distance<=0.10) { - myled2 = 1; + myled2.write(((LED_distance-0.10)*-1)*20); //PWM the LED from 0% to 100% } else { - myled2 = 0; + myled2 = 0; //Turn off LED } - if(LED_distance<=0.1) + if(LED_distance<=0.05) { - myled3 = 1; + myled3.write(((LED_distance-0.05)*-1)*40); //PWM the LED from 0% to 100% } else { - myled3 = 0; + myled3 = 0; //Turn off LED } - if(LED_distance<=0.05) + if(LED_distance<=0.025) { - myled4 = 1; + myled4.write(((LED_distance-0.025)*-1)*40); //PWM the LED from 0% to 100% //s2=0.1; } else { - myled4 = 0; + myled4 = 0; //Turn off LED //s2=1; } }