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.
10 years, 11 months ago.
i have a error in my compiler,but i dont know why,please teach me ,thank you
- include "mbed.h" DigitalOut myled(LED1); Serial pc(USBTX, USBRX); AnalogIn tempPin1(p15); AnalogIn tempPin2(p16); PwmOut pwm(p21);
- define start 500;
- define end 2200; float tempTotal1; float tempTotal2; uint32_t tempDiv1; uint32_t tempDiv2;
- define MAX_TEMP_AVG 10 void sampleTemperature(); void sampleTemperatures(); void resetTemperatureSamples(); float getAvgTemperature(); void sampleTemperature() { tempTotal1 += tempPin1; tempDiv1++; tempTotal2 += tempPin2; tempDiv2++; } void sampleTemperatures() { for(int i=0;i<MAX_TEMP_AVG;i++) { sampleTemperature(); wait(0.1); } } void resetTemperatureSamples() { tempTotal1 = 0.0; tempDiv1 = 0; tempTotal2 = 0.0; tempDiv2 = 0; } float getAvgTemperature1() { float tempAvg1 = (((tempTotal1)*3300/(float)tempDiv1)-400.0)/19.5; return tempAvg1; } float getAvgTemperature2() { float tempAvg2 = (((tempTotal2)*3300/(float)tempDiv2)-400.0)/19.5; return tempAvg2; }
int main() { tempDiv1 = 0; tempDiv2 = 0; pc.printf("MBED ready..\r\n"); sampleTemperatures(); pc.printf("Temp1: %.2f Temp2: %.2f \r\n",getAvgTemperature1(),getAvgTemperature2()); while(1){ sampleTemperature(); myled = 1; wait(0.5); myled = 0; wait(0.5); }
while(1) { float j = getAvgTemperature1()/getAvgTemperature2() ; if(j>0){ for(int p = start; p < end; p += 20) { pwm.pulsewidth_us(p); wait_ms(40); }} else{ for(int p = end; p >=start; p -= 20) { pwm.pulsewidth_us(p); wait_ms(40);
} } }
1 Answer
10 years, 11 months ago.
Remark: 
For improved readability, when you copy source code into a question, use the <<code>> ... <</code>> formatters.
Keep both formatters on separate lines and put your code between the two markers.
include "mbed.h"
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);
AnalogIn tempPin1(p15);
AnalogIn tempPin2(p16);
PwmOut pwm(p21);
define start 500;
define end 2200;
float tempTotal1;
float tempTotal2;
uint32_t tempDiv1;
uint32_t tempDiv2;
define MAX_TEMP_AVG 10
void sampleTemperature();
void sampleTemperatures();
void resetTemperatureSamples();
float getAvgTemperature();
void sampleTemperature()
{
    tempTotal1 += tempPin1;
    tempDiv1++;
    tempTotal2 += tempPin2;
    tempDiv2++;
}
void sampleTemperatures()
{
    for(int i=0; i<MAX_TEMP_AVG; i++) {
        sampleTemperature();
        wait(0.1);
    }
}
void resetTemperatureSamples()
{
    tempTotal1 = 0.0;
    tempDiv1 = 0;
    tempTotal2 = 0.0;
    tempDiv2 = 0;
}
float getAvgTemperature1()
{
    float tempAvg1 = (((tempTotal1)*3300/(float)tempDiv1)-400.0)/19.5;
    return tempAvg1;
}
float getAvgTemperature2()
{
    float tempAvg2 = (((tempTotal2)*3300/(float)tempDiv2)-400.0)/19.5;
    return tempAvg2;
}
int main()
{
    tempDiv1 = 0;
    tempDiv2 = 0;
    pc.printf("MBED ready..\r\n");
    sampleTemperatures();
    pc.printf("Temp1: %.2f Temp2: %.2f \r\n",getAvgTemperature1(),getAvgTemperature2());
    while(1) {
        sampleTemperature();
        myled = 1;
        wait(0.5);
        myled = 0;
        wait(0.5);
    }
    while(1) {
        float j = getAvgTemperature1()/getAvgTemperature2() ;
        if(j>0) {
            for(int p = start; p < end; p += 20) {
                pwm.pulsewidth_us(p);
                wait_ms(40);
            }
        } else {
            for(int p = end; p >=start; p -= 20) {
                pwm.pulsewidth_us(p);
                wait_ms(40);
            }
        }
    }
As for the errors:
- Include and define need to start with a #
 
- Don't end the values you define with a semicolon as this will be included when the macro is used in the code.
 
- A closing brace } is missing at the end of the code.
 
- Your main function has two while(1) loops, the second loop will never execute.
 
 
                            
int main() { tempDiv1 = 0; tempDiv2 = 0; pc.printf("MBED ready..\r\n"); sampleTemperatures(); pc.printf("Temp1: %.2f Temp2: %.2f \r\n",getAvgTemperature1(),getAvgTemperature2()); while(1){ sampleTemperature(); myled = 1; wait(0.5); myled = 0; wait(0.5); }
while(1) { float j = getAvgTemperature1()/getAvgTemperature2() ; if(j>0){ for(int p = start; p < end; p += 20) { pwm.pulsewidth_us(p); wait_ms(40); }} else{ for(int p = end; p >=start; p -= 20) { pwm.pulsewidth_us(p); wait_ms(40);
} } }
posted by jyo tetsu 29 Nov 2014Please use "code" tag to show your program source list.
posted by Yoshi Mimura 29 Nov 2014Please specify what kind of error you got.